#!/usr/bin/perl -w
# Convert gettext .po files to SGML entity declarations.
# Copyright (c) Adam Di Carlo, under the terms of the GPL
#
# Copyright (c) 1996 Progiciels Bourbeau-Pinard inc.
# Franois Pinard <pinard@iro.umontreal.ca>

&prologue;

$lang = shift(@ARGV);

while (<>) {
    if (/^msgid "(.*)"$/) {
	$type = 'msgid';
	$msgid = $1;
    } elsif (/^msgstr "(.*)"$/) {
	$type = 'msgstr';
	$msgstr = $1;
    } elsif (/^\"(.*)\"$/) {
	if ($type eq 'msgid') {
	    $msgid .= $1;
	} else {
	    $msgstr .= $1;
	}
    } elsif (/^$/ || /^\#/) {
	&process_pair if $msgstr;
    }
}
&process_pair if $msgstr;

&epilogue;

exit 0;

sub process_pair {
    # condition the id
    $msgid =~ s/\\n/\n/g;
    $msgid =~ s/\W/-/g;		# remove non-word chars
    $msgid =~ s/["()%_]/-/g;	# remove non-word chars
    $msgid =~ s/--/-/g;		# remove dupe underscores
    $msgid =~ s/^-//g;		# remove leading
    $msgid =~ s/-$//g;		# remove trailing
    if ( $msgid =~ m/^[a-z]/i ) { # skip empty ids
    	# condition the msgstr
	$msgstr =~ s/\\n/\n/g;
	$msgstr =~ s/\\"/\&quot;/g; # remove quotations (not DTD portable) \\"
	$msgstr =~ s|%d|<var>num</var>|g; # variables
	$msgstr =~ s|%\S+|<var>var</var>|g; # variables

	# characters above 127 must be replaced with decimal SGML
        # reference
	while ( $msgstr =~ /([\x7F-\xFF])/ ) {
	    $repl = ord($1);
	    $msgstr =~ s/$1/&#${repl};/g || die;
	}

	print "<!entity $msgid \"".&add_quotes($msgstr)."\">\n";
	$msgstr = "";
    }
}

sub add_quotes {
    # add quotes according to current locale
    my $msg = shift;
    if ($lang eq 'fr') {
        #   We put quotes directly in text instead of here
        return $msg;
    } elsif ($lang eq 'da') {
        return '&quot;'.$msg.'&quot;';
    } else {
        # default
        return '``'.$msg."''";
    }
}

sub prologue {
    print <<EOF;
<!-- This file has been generated by \`$0\' DO NOT EDIT!
     Copyright  1996 Progiciels Bourbeau-Pinard inc.
     Franois Pinard <pinard\@iro.umontreal.ca>, 1996.
     Adam Di Carlo <aph\@debian.org>, 1999, 2000. -->
EOF
    print '<!-- $Id: po2sgml,v 1.24 2002/05/08 22:16:15 barbier Exp $ -->';
    print "\n";
}

sub epilogue {
    print <<EOF;

<!-- translation table from old catalog names -->
    <!entity MSG-NEXT "&Next;">
    <!entity MSG-YES "&Yes;">
    <!entity MSG-NO "&No;">
    <!entity MSG-OK "&No;">
    <!entity MSG-TITLE-MENU "&Debian-GNU-Linux-Installation-Main-Menu;">
    <!entity MSG-WAIT-STATE "&The-installation-program-is-determining-the-current-state-of-your-system-and-the-next-installation-step-that-should-be-performed;">
    <!entity MSG-CONFIGURE-KEY "&Configure-the-Keyboard;">
    <!entity MSG-PARTITION-DISK "&Partition-a-Hard-Disk;">
    <!entity MSG-INITIALIZE-SWAP "&Initialize-and-Activate-a-Swap-Partition;">
    <!entity MSG-DO-WITHOUT-SWAP "&Do-Without-a-Swap-Partition;">
    <!entity MSG-SELECT-ACTIVATE-SWAP-L "&Please-select-the-partition-to-activate-as-a-swap-device;">
    <!entity MSG-INITIALIZE-LINUX "&Initialize-a-Linux-Partition;">
    <!entity MSG-INSTALL-OS "&Install-Kernel-and-Driver-Modules;">
    <!entity MSG-MOUNT-LINUX "&Mount-a-Previously-Initialized-Partition;">
    <!entity MSG-CANCEL "&Cancel;">
    <!entity MSG-CONFIGURE-NET "&Configure-the-Network;">
    <!entity MSG-CONFIGURE-MODULES "&Configure-Device-Driver-Modules;">
    <!entity MSG-CHOOSE-HOST "&Choose-the-Host-name;">
    <!entity MSG-INSTALL-BASE "&Install-the-Base-System;">
    <!entity MSG-TZ-DIR "&Directories;">
    <!entity MSG-TZ "&Timezones;">
    <!entity MSG-DISK-BOOT "&Make-System-Bootable;">
    <!entity MSG-REBOOT "&Reboot-the-System;">
    <!entity MSG-CONFIGURE-PCMCIA "&Configure-PCMCIA-Support;">
    <!entity MSG-FLOPPY-BOOT "&Make-a-Boot-Floppy;">
    <!entity MSG-RESCUE-FLOPPY "&Make-a-Boot-Floppy;">
    <!entity MSG-DRIVERS-FLOPPY "&Drivers;">  <!-- probably really bad -->
    <!-- deprecated entities but provided for compatability, representing old 
         dbootstrap messages -->
    <!entity Make-Linux-Bootable-Directly-From-Hard-Disk "&Make-System-Bootable;">
EOF
}
