#!/usr/bin/python

import os, sys, string

if ( len( sys.argv ) < 4 ):
    print("Usage: replaceTextureInSli.py <dot.s file> <outputFile> <slifile.compressed> [size-symbol] ")
    sys.exit( -1 )

def execute( command ):
    print ( command );
    os.system( command );


def getCommandOutput( command ):
    print ( command )
    temp = os.popen( command, "r" );
    lines = temp.readlines();
    temp.close();
    return lines;

dotS = sys.argv[ 1 ]
outputFileName = sys.argv[ 2 ]
slifile = sys.argv[ 3 ]

sizeSymbol = "NO"

if ( len( sys.argv ) > 4):
    sys.argv[ 4 ] = sizeSymbol

f = open( dotS )
lines = f.readlines();
f.close()

f = open( outputFileName,"w+" )

tmpLines = getCommandOutput( "wc " + slifile )
bobs = string.split( tmpLines[ 0 ] );
donorSize = string.atoi( bobs[ 2 ] )


for line in lines:
    tokens = string.split( line )

    if ( len( tokens ) < 1 ): continue

    if ( sizeSymbol != "NO" and tokens [ 0 ] == sizeSymbol ):
        f.write( sizeSymbol + " = " + str( donorSize ) )
        continue
    
    if ( tokens[ 0 ] == ".byte" ): break;

    f.write( line )

sli = open( slifile )

idx = 0;

f.write("\t.byte  ");

while ( idx < donorSize ):
    s = sli.read( 1 );

    if ( idx % 10 != 0 ): f.write(", ")

    f.write( hex( ord( s ) )  )

    idx = idx + 1;

    if ( ( idx % 10 ) == 0 ):
        f.write("\n\t.byte  ");
        
f.close()
