#!/usr/bin/python

# This script converts the zh art and puts it in the appropriate directories.

import sys, string, os, re

def execute( command ):
    print ( command );
    os.system( command );

print("Be sure to define this variable: GAMEROOT.  It points at the root dir for f0x");

DIR = os.environ['GAMEROOT'];
ASSETDIR = DIR + "/i10n/assets/"
TOOLSDIR = DIR + "/i10n/tools/";
ROOT = os.environ['ROOT'];
TMP = TOOLSDIR

def step2():
    # On-course textures 

    f = open( TOOLSDIR + "course-texture-list.txt" )

    lines = f.readlines();

    f.close();

    for line in lines:
        tokens = string.split( line );

#        if ( len( tokens ) > 3 ):
#            if ( tokens[3] == "alpha" ):
#                execute( TOOLSDIR + "/rgb2rgba-black.pl " + ASSETDIR + "courses/zh/" + tokens[0] + " " + ASSETDIR + "courses/zh/" + tokens[0] + ".rgba"  )
#                tokens[ 0 ] = tokens[ 0 ] + ".rgba"

        # for reasons known only to itself, you have to convert this to rgb firs
        execute( "/usr/X11R6/bin/convert  "+ASSETDIR + "courses/zh/"+ tokens[ 0 ] + " sgi:" + ASSETDIR + "courses/zh/"  + tokens[ 0 ] + ".rgb" );
        
        execute( ROOT + "/usr/sbin/rgb2c -F -l 0,0,0 -h 255,255,255 -t I -f I -s 4 -m " + tokens[ 2 ] + " " + ASSETDIR + "courses/zh/"+ tokens[ 0 ] + ".rgb >  " + DIR + "IMAGES/zh/"  + tokens[ 1 ]);


# WD: I think we don't need step3 any more. 
def step3():
    # Replacing textures in the .o from courses

    # Converts the .sgi files to
    # texture files that can be compiled.
    

    # Make a copy of the offending .o files
    execute( "cp " + DIR + "/ot_sseffect.o " + DIR + "/ot_sseffect_zh.o; chmod 666 " + DIR + "/ot_sseffect_zh.o " );
    execute( "cp " + DIR + "/ot_sseffect2.o " + DIR + "/ot_sseffect2_zh.o;  chmod 666 " + DIR + "/ot_sseffect2_zh.o " );
    

    f = open( TOOLSDIR + "orecord-texture-list.txt" )

    lines = f.readlines();

    f.close();

    for line in lines:
        tokens = string.split( line );

        invert = "-F";
        bits = tokens[ 5 ];
        if ( tokens[ 6 ] == "N" ): invert = "";
        color = tokens[ 7 ];
       
        execute( ROOT + "/usr/sbin/rgb2c " + invert + " -t " + color + " -f RGBA -s "+ bits +" -m " + tokens[ 2 ] + " " + ASSETDIR + "courses/zh/"+ tokens[ 0 ] + " >  " + DIR + "IMAGES/zh/"  + tokens[ 1 ]);    
    
        if ( len( tokens ) < 5  or tokens[3] != "oreplace" ):
            continue;

        Cfilename = DIR + "IMAGES/zh/"  + tokens[ 1 ];
        OFile = DIR + "/"  + tokens[ 4 ];
        Sname = tokens[ 2 ]
        
        execute(TOOLSDIR + "replaceTexture.py " + Cfilename + " " + OFile + " " + Sname );

mini_counter = 1

if ( string.find( sys.argv[1], "2" ) != -1 ):
    print "--------Step2-----------------------"
    step2()
    sys.exit(0);        

if ( string.find( sys.argv[1], "3" ) != -1 ):
    print "--------Step2-----------------------"
    step3()
    sys.exit(0);

    




