#!/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;

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
	print "<!entity $msgid \"``$msgstr''\">\n";
	$msgstr = "";
    }
}

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.9 2000/08/25 06:41:58 aph 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-Operating-System-Kernel-and-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-CONFIGURE-BASE "&Configure-the-Base-System">
    <!entity MSG-TZ-DIR "&Directories">
    <!entity MSG-TZ "&Timezones">
    <!entity MSG-DISK-BOOT "&Make-Linux-Bootable-Directly-From-Hard-Disk">
    <!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 -->
    <!entity MSG-BASE-SYSTEM "&Base-System">
EOF
}
