#!/usr/bin/python

import sys
import string
import os
import re

HashTable = {};


FPS=160 # This is the frame-rate has already been rendered before

FPS2Render = 100  # we want to take the duplicate files out so that only those the files that are not rendered before are sent for rendering, In this case 100Hz has some files which might be common with 160Hz so want to remvoe the duplicates.

common_outfile = open('common.sh','w');

torender_outfile_name = 'POVRayposesfps_' + str(FPS2Render) + '.0RenderingCommands.sh';

print torender_outfile_name

count = 0;

while FPS>FPS2Render: 
        
#        if FPS >= 60:
#              break; 
        if count == 1:
              break;
              
                

	file2render = open(torender_outfile_name,'r');

	torender_outfile_name = str(FPS2Render) + '_2render_' + str(count) + '.sh' ;

	torender_outfile = open(torender_outfile_name,'w');
     
	file_has_rendered_name = 'POVRayposesfps_' + str(FPS) + '.0RenderingCommands.sh';

	file_has_renders_already = open(file_has_rendered_name,'r');


	for line in file_has_renders_already:
		#got the needed declare string;	
		Declare_string   = line.partition("wt20")[2].partition("+HIfirst")[0];
		img_name_string  = line.partition("fps/")[2].partition("+")[0]
		HashTable[Declare_string] = img_name_string



	for line in file2render:
		Declare_string   = line.partition("wt20")[2].partition("+HIfirst")[0];
		img_name_string  = line.partition("fps/")[2].partition("+")[0]
	
		if  HashTable.has_key(Declare_string): 
			print >> common_outfile, 'ln -s ../'+ str(FPS) + 'fps/' + HashTable.get(Declare_string), str(FPS2Render) + 'fps/'+ img_name_string
		else:
			print >> torender_outfile, line,	

	count = count + 1;

	#torender_outfile_name = str(FPS2Render) + '_2render_' + str(count) + '.sh' ;

	#FPS = FPS+20;

