#!/usr/bin/perl 
#
#
# recode_po_file_to_utf reads the charset of
# po-file and calls 
#
#    "recode <charset>..utf-8 < po-file"
# or
#
#    "iconv -f <charset> -t utf-8 po-file"
#
# the recoded output is written to stdout.
#
# usage: recode_po_file_to_utf <po-file>
#
# Author: Michael Hager (mike@suse,de)
#

use strict;
use English;
use POSIX qw(strftime);

my $infile;

if ( $#ARGV == 0 )
{
    $infile = shift @ARGV;
}
else
{
   print "\n\nusage: recode_po_file_to_utf <po-file>\n\n"; 
   exit -1;
}

my $line_no	= 0;
my $line;

open ( KB_IN,      $infile  ) or die "FATAL: Can't open $infile";


while ( $line = <KB_IN> )
{
    $line_no++;
#    print "-$line";

    if ( $line =~ /charset=(.*)\\n/ )
    {
        my $args;
	my $charset = $1;
        if ( $charset =~ /euc/i || $charset =~ /2022/i ) {
	    # recode doesn't support Asian encodings like
	    # euc-kr, euc-jp, euc-cn, euc-tw, iso-2022-jp, iso-2022-kr ...
	    # Let's use iconv at least for these encodings:
	    # Wed Sep 13 10:50:58 2000  Mike Fabian  <mfabian@suse.de>

	    #print STDERR "iconv -f $charset -t utf-8 $infile";
	    #$args = "iconv -f $charset -t utf-8 $infile"; 
	    # We must say that the gettext should use utf-8
	    $args = "sed 's/charset=$charset/charset=utf-8/' < $infile | iconv -f $charset -t utf-8 -";
            system($args) == 0 or die "\n\n\nERROR $args failed\n\n\n";
	}
	else {
	    #print STDERR "recode $charset..utf-8 < $infile";
	    #$args = "recode $charset..utf-8 < $infile";
	    # We must say that the gettext should use utf-8
	    $args = "sed 's/charset=$charset/charset=utf-8/' < $infile | recode $charset..utf-8";
	    system($args) == 0 or die "\n\n\nERROR $args failed\n\n\n";
	}
	last;
    }
}


close ( KB_IN );


# EOF
