#!/usr/bin/python

import sys, string, os, re

lines = sys.stdin.readlines();

def execute( command ):
    print ( command );
    if ( os.system( command ) != 0 ):
        print("FAILED!: " + command )
        sys.exit( -1 )



colorTableStart = -1;
colorTableEnd = -1;

for i in range( 0, len(lines) ):
    line = lines[ i ]
    if ( string.find( line, "unsigned short texturetlut" ) != -1 ):
        colorTableStart = i + 1;
        continue;
    if ( colorTableStart != -1 and string.find( line, "0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000," ) != -1 ):
        colorTableEnd = i + 1;
        break;

#print "Color table start = " + str( colorTableStart ) + " and end " + str( colorTableEnd ) 

if ( colorTableStart == -1 or colorTableEnd == -1 ):
    print("Couldn't find color table in output!");
    sys.exit( -1 )

f = open( os.environ['GAMEROOT'] + "/IMAGES/dai_me_color_zh.c", "w+" )

f.write("static Gfx dai_m_color_C_dummy_aligner1[] = { gsSPEndDisplayList() };\nunsigned short dai_m_color[] = {\n")

#print lines[ colorTableStart ]

#Make black transparent
lines[ colorTableStart ] = lines[ colorTableStart ][0:5] + "0" + lines[colorTableStart][6:];
#print lines[ colorTableStart]

for i in range( colorTableStart, colorTableEnd ):
    f.write( lines[ i ] )

f.write("};")

f.close()
    


