#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# $Id: MsgFormat.pl,v 1.3 2002/03/15 10:55:20 k_reger Exp $
#
# (C) 1998 Mike Shoyher
#



use Locale::PGetText;
use strict;

use vars qw{$locale_dir $lang %msg $path $s $msgid};

$locale_dir=$ARGV[0];
$lang=$ARGV[1];
my $path="$locale_dir/$lang";
printUsage() unless ($locale_dir && $lang);

dbmopen(%msg, $path, 0644) || die "Cannot create database $path";

for (<STDIN>)
{
	if (/^msgid\s+\"(.*)\"\s*$/)
	{
		if ($msgid) 
		{
			$msgid =~ s/\\n/\n/g;
			$s =~ s/\\n/\n/g;
			$s =~ s/\\"/"/g;

			print "MSGID: $msgid\nMSGSTR: $s";

			if ($msgid ne $s && !defined($msg{$msgid}))
			{
				$msg{$msgid}=$s;
				print " ok\n";
			}
			else
			{
				print " skipped\n";			
			}
		}

		$s=$1;
	}
	
	if (/^\"(.+)\"\s*$/)
	{
		$s.=$1;
	}
	if (/^msgstr\s+\"(.*)\"\s*$/)
	{
		$msgid = $s;
		$s = $1;
	}
}


$msgid=~s/\\n/\n/g;
$s=~s/\\n/\n/g;
$s=~s/\\"/"/g;
print "MSGID: $msgid\nMSGSTR: $s\n";
$msg{$msgid}=$s
    if $msgid ne $s && !defined($msg{$msgid});

dbmclose(%msg);


sub printUsage
{
print "Usage:  MsgFormat <LocaleDir> <Language> < myprog.po\n\n";
exit(100);
}


=head1 NAME

MsgFormat - messages database compiler for PGetText

=head1 SYNOPSIS

  MsgFormat <LocaleDir> <Language> < myprog.po


=head1 DESCRIPTION

MsgFormat creates database in <LocaleDir> directory with messages from <myprog.po> for language <Language>

=head1 SEE ALSO

Locale::PGetText

=cut

