#!/bin/python
# -*- coding: utf-8 -*-
import os, sys
winex = sys.argv[2]
prefix = sys.argv[1]

home = os.popen("printf $HOME","r").read()
lines = open(home+"/.cedega/"+prefix+"/games.ini","r").readlines();
i = 0
last_file = ""
while(i < len(lines)):
	current_line=lines[i].replace("\n","")
	if(current_line[0] == "[" and current_line[len(current_line) -1] == "]"):
		my_file=current_line[1:len(current_line)-1]

	if(my_file != last_file):
		edit = open(home+"/.PlayOnLinux/tmp/cedega/"+my_file,"w")
		edit.writelines("#!/bin/bash\n");
		edit.writelines("export PATH=\"$HOME/.cedega/.winex_ver/"+winex+"/bin/:$PATH\"\n")
		edit.writelines("export GAME_FOLDER_ROOT=\"$HOME/.cedega\"\n")		
		edit.writelines("export WINEXINSTALLDIR=\"$HOME/.cedega/.winex_ver/"+winex+"/\"\n")
		edit.writelines("export WINEDEBUG=\"-all\"\n")
		edit.writelines("export WINEPREFIX=\""+home+"/.PlayOnLinux/wineprefix/"+prefix+"\"\n")
		table = ["cd","path"]
		lines_written = True
		last_file = my_file

	if("workdir=" in current_line):
		table[0] = current_line.replace("workdir=","cd \"")+"\"\n"
		lines_written = False
	if("path=" in current_line):
		table[1] = current_line.replace("path=","winex3 \"")+"\"\n"
	
	if(lines_written == False):
		edit.writelines(table[0])
		edit.writelines(table[1])		
		lines_written = True

	i += 1

