#!/usr/bin/perl
# -*- perl -*-
open(FD,"/etc/chinese.conf");
open(EXEC,">cccsh");
while(<FD>)
{
    if ( /BEGIN[ \t]*([a-zA-Z0-9]*)/)
    {
	print "Parse $1 block\n";
	if ($1 eq "WINDOW")
	    {&translate_window($ARGV[0]);}
	elsif ($1 eq "INPUT")
	    {&translate_input($ARGV[0]);}
	elsif ($1 eq "FONT")
	    {&translate_font($ARGV[0]);}
	elsif ($1 eq "ENV")
	    {&translate_env($ARGV[0]);}
	else
	{
	    print STDERR "Error block $1 encounter\n";
	    while(<FD>) {if (/END/) {last;}}
	}
    }
}
print EXEC "ccc $1 $2 $3 $4\n";
close(EXEC);


sub translate_window 
{
    local($path) = @_;
    $sizex = 80;
    $sizey = 29;
    $fc	= 1;
    $bc = 0;
    $sc = 7;
    while(<FD>)
    {
	if ( /^#/) {loop;}
	if ( /SIZE[ \t]*([0-9]*)[ \t]*([0-9]*)/)
	{
	    $sizex = $1;
	    $sizey = $2;
	}
	elsif ( /FORECOLOR[ \t]*([0-9]*)/)
	{
	    $fc = $1;
	}
	elsif ( /BACKCOLOR[ \t]*([0-9]*)/)
	{
	    $bc = $1;
	}
	elsif ( /SYSTEMCOLOR[ \t]*([0-9]*)/)
	{
	    $sc = $1;
	}
	elsif ( /END/)
	{
	    last;
	}
    }
    system "echo $sizex $sizey $fc $bc $sc > ${path}ccc.conf";
}

sub translate_input
{
    local($path) = @_;
    open(MULTITAB,">/etc/multitab");
    $i = 1;
    while(<FD>)
    {
	if ( /^#/) {loop;}
	if ( /PHONETIC[ \t]*([^ \t\n]*)/)
	{
	    $ENV{'PHONETIC_TABLE'} = $1;
	}	
	elsif ( /MULTI[ \t]*([^ \t\n]*)/)
	{
	    printf MULTITAB "$i $1\n";
	    $i++;
	}
	elsif ( /END/)
	{ last;}
	else
	{
	    print STDERR "error line $_";
	}
    }
    close(MULTITAB);
}
	
sub translate_font
{
    local($path) = @_;
    while(<FD>)
    {
	if ( /^\#/) 
	    {loop;}  
	if ( /STYLE[ \t]*([^ \t\n]*)/)
	{
	    if ($1 eq "HBF")
	    { $ishbf = 1;}
	    else
	    { $ishbf = 0;}
	    last;
	}
        else		
	{
	    print STDERR "The first line of font block must be STYLE\n";
	}
    }
    open(FONT,">${path}kcfont");
    $has_ascii = 0;
    if ($ishbf)
	{print FONT "!!!HBF\n";}
    while(<FD>)
    {
	if ( /^\#/) {loop;}
	if ( /ASCII[ \t]*([^ \t]*)[ \t]*([0-9]*)[ \t]*([0-9]*)/)
	{
	    if ($has_ascii)
	    { print STDERR "Duplicte ASCII\n";}
	    else
	    {
		print FONT "$1 $2 $3\n";
	    }
	    $has_ascii = 1;
	}
	elsif( /CFONT[ \t]*([^ \t]*)[ \t]*([0-9]*)[ \t]*([0-9]*)/)
	{
	    if ($ishbf)
	    {loop;}
	    if ($has_ascii == 0)
	    { print STDERR "Must has ASCII before FONT\n";}
	    print FONT "$1 $2 $3\n";
	    $_ = <FD>;	
	    print FONT $_;	
	}
	elsif ( /HBF[ \t]*([^ \t\n]*)/)
	{
	    if ($ishbf) {print FONT "$1\n";}
	}			
	elsif ( /HBFPATH[ \t]*([^ \t\n]*)/)
	{
	    print EXEC "export CFONTDIR=$1\n";
	}
	elsif ( /PFONT([DKLM])[ \t]*([^ \t\n]*)/)
	{
	    if ($1 eq "L")
	    {system "ln -sf $2 cn24l.hbf";}
	    elsif ($1 eq "K")
	    {system "ln -sf $2 cn24k.hbf";}
	    elsif ($1 eq "M")
	    {system "ln -sf $2 cn24m.hbf";}
	    elsif ($1 eq "D")
	    {system "ln -sf $2 cn24j.hbf";}
	}
	elsif ( /END/)
	    {last;}
    }
    close(FONT);
}
	
sub translate_env
{
    local($path) = @_;
    open(PRO,">${path}ch_profile");
    while(<FD>)
    {
	if (/END/)
	{last;}
	print PRO $_;
    }
    close(PRO);
    chmod(755,"${path}ch_profile");
}

