from org.csstudio.opibuilder.scriptUtil import PVUtil
from org.csstudio.opibuilder.scriptUtil import ScriptUtil
val = PVUtil.getDouble(pvs[0])
if val == 1:
	val = 0
	pvs[0].setValue(val)
	filename = "/home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM.h"
	myfile = open(filename, 'w')
	myfile.write('#ifndef _')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text").upper())
	myfile.write('GAM_H_\n')
	myfile.write('#define\t')
	myfile.write('_')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text").upper())
	myfile.write('GAM_H_\n\n')
	myfile.write('#include "DDBInputInterface.h"\n')
	myfile.write('#include "DDBOutputInterface.h"\n')
	myfile.write('#include "GAM.h"\n')
	myfile.write('#include "File.h"\n')
	myfile.write('#include "Matrix.h"\n')
	myfile.write('#include "HtmlStream.h"\n')
	myfile.write('#include "HttpInterface.h"\n')
	myfile.write('#include "CDBExtended.h"\n\n')
	myfile.write('OBJECT_DLL(')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM)\n')
	myfile.write('class ')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM : public GAM, public HttpInterface {\n')
	myfile.write('private:\n\n')
	myfile.write('\tDDBInputInterface *SignalsInputInterface;\n')
	myfile.write('\tDDBOutputInterface *SignalsOutputInterface;\n\n')
	myfile.write('\tstruct InputInterfaceStruct {\n')
	myfile.write(display.getWidget("contentInLabel2").getPropertyValue("text"))
	myfile.write('\t};\n\n')
	myfile.write('\tstruct OutputInterfaceStruct {\n')
	myfile.write(display.getWidget("contentOutLabel2").getPropertyValue("text"))
	myfile.write('\t};\n\n')
	myfile.write(display.getWidget("contentVariableLabel2").getPropertyValue("text"))
	myfile.write('\tbool view_input_variables;\n')
	myfile.write('public:\n')
	myfile.write('\t// Default constructor\n\t')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM();\n\n')
	myfile.write('\t// Destructor\n')
	myfile.write('\tvirtual ~')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM();\n\n')
	myfile.write('\t// Initialise the module\n')
	myfile.write('\tvirtual bool Initialise(ConfigurationDataBase& cdbData);\n\n')
	myfile.write('\t// Execute the module functionalities\n')
	myfile.write('\tvirtual bool Execute(GAM_FunctionNumbers functionNumber);\n')
	myfile.write('\tvirtual bool ProcessHttpMessage(HttpStream &hStream);\n\n')
	myfile.write('\tOBJECT_DLL_STUFF(')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM)\n')
	myfile.write('};\n\n')
	myfile.write('#endif\n')
	myfile.close()
	
	filename = "/home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM.cpp"
	myfile = open(filename, 'w')
	myfile.write('#include "')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM.h"\n\n')
	myfile.write('OBJECTLOADREGISTER(')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM, "$Id: $")\n\n')
	myfile.write('//  ******** Default constructor ***********************************\n')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM::')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM(){\n')
	myfile.write('\tthis->SignalsInputInterface = NULL;\n')
	myfile.write('\tthis->SignalsOutputInterface = NULL;\n')
	myfile.write('}\n\n')
	myfile.write('// ********* Destructor ********************************************\n')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM::~')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM()\n')
	myfile.write('{\n')
	myfile.write('\t// if(this->SignalsInputInterface != NULL) delete[] this->SignalsInputInterface ;\n')
	myfile.write('\t// if(this->SignalsOutputInterface != NULL) delete[] this->SignalsOutputInterface;\n')
	myfile.write('}\n\n')
	myfile.write('//{ ********* Initialise the module ********************************\n')
	myfile.write('bool ')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM::Initialise(ConfigurationDataBase& cdbData){\n')
	myfile.write('\tCDBExtended cdb(cdbData);\n\n')
	myfile.write('\tif(!AddInputInterface(SignalsInputInterface,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAMInputInterface")){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s failed to add input interface",Name());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}\n')
	myfile.write('\t\n')
	myfile.write('\tif(!AddOutputInterface(SignalsOutputInterface,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAMInputInterface")){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s failed to add output interface",Name());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}\n')
	myfile.write('\tif(!cdb->Move("input_signals")){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s did not specify input_signals entry",Name());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}\n')
	myfile.write('\tint32 nOfSignals = cdb->NumberOfChildren();\n')
	myfile.write('\tif(nOfSignals < 1){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError, "'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s ObjectLoadSetup. '+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM expects the current time and voltage as inputs ",Name());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}\n')
	myfile.write('\tif(!SignalsInputInterface->ObjectLoadSetup(cdb,NULL)){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s ObjectLoadSetup Failed DDBInterface %s ",Name(),SignalsInputInterface->InterfaceName());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}   \n')
	myfile.write('\tcdb->MoveToFather();\n')
	myfile.write('\tif(!cdb->Move("output_signals")){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s did not specify output_signals entry",Name());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}\n')
	myfile.write('\tif(!SignalsOutputInterface->ObjectLoadSetup(cdb,NULL)){\n')
	myfile.write('\t\tAssertErrorCondition(InitialisationError,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s ObjectLoadSetup Failed DDBInterface %s ",Name(),SignalsOutputInterface->InterfaceName());\n')
	myfile.write('\t\treturn False;\n')
	myfile.write('\t}\n')
	myfile.write('\tnOfSignals = cdb->NumberOfChildren();\n')
	myfile.write('\tif(nOfSignals < 1){\n')
	myfile.write('\t\tAssertErrorCondition(Warning,"'+display.getWidget("FileNameTextInput2").getPropertyValue("text")+'GAM::Initialise: %s ObjectLoadSetup. No output of this GAM ",Name());\n')
	myfile.write('\t}\n')
	myfile.write('\tcdb->MoveToFather();\n')
	myfile.write('\treturn True;\n')
	myfile.write('}\n\n')
	myfile.write('//{ ********* Execute the module functionalities *******************\n')
	myfile.write('bool ')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM::Execute(GAM_FunctionNumbers functionNumber){\n')
	myfile.write('\tInputInterfaceStruct *inputstruct = (InputInterfaceStruct *) this->SignalsInputInterface->Buffer();\n')
	myfile.write('\tthis->SignalsInputInterface->Read();\n')
	myfile.write('\tOutputInterfaceStruct *outputstruct = (OutputInterfaceStruct *) this->SignalsOutputInterface->Buffer();\n')
	myfile.write('\tthis->SignalsOutputInterface->Write();\n')
	myfile.write('\treturn True;\n')
	myfile.write('}\n\n')
	myfile.write('bool ')
	myfile.write(display.getWidget("FileNameTextInput2").getPropertyValue("text"))
	myfile.write('GAM::ProcessHttpMessage(HttpStream &hStream){\n')
	myfile.write('HtmlStream hmStream(hStream);\n')
	myfile.write('int i;\n')
	myfile.write('hmStream.SSPrintf(HtmlTagStreamMode, "html>\\n"\n')
	myfile.write('\t"<head>\\n"\n')
	myfile.write('\t"<title>%s</title>\\n"\n')
	myfile.write('\t"</head>\\n"\n')
	myfile.write('\t"<body>\\n"\n')
	myfile.write('\t"<svg width=\\\"100&#37;\\\" height=\\\"100\\\" style=\\\"background-color: AliceBlue;\\\">\\n\"\n')
	myfile.write('\t"<image x=\\\"%d\\\" y=\\\"%d\\\" width=\\\"%d\\\" height=\\\"%d\\\" xlink:href=\\\"%s\\\" />\\n\"\n')
	myfile.write('\t"</svg", (char *) this->Name(),0, 0, 422, 87, "http://www.ipfn.ist.utl.pt/ipfnPortalLayout/themes/ipfn/_img_/logoIPFN_Topo_officialColours.png");\n')
	myfile.write('hmStream.SSPrintf(HtmlTagStreamMode, "br><br><text style=\\\"font-family:Arial;font-size:46\\\">%s</text><br", (char *) this->Name());\n')
	myfile.write('FString submit_view;\n')
	myfile.write('submit_view.SetSize(0);\n')
	myfile.write('if (hStream.Switch("InputCommands.submit_view")){\n')
	myfile.write('\thStream.Seek(0);\n')
	myfile.write('\thStream.GetToken(submit_view, "");\n')
	myfile.write('\thStream.Switch((uint32)0);\n')
	myfile.write('}\n')
	myfile.write('if(submit_view.Size() > 0) view_input_variables = True;\n')
	myfile.write('FString submit_hide;\n')
	myfile.write('submit_hide.SetSize(0);\n')
	myfile.write('if (hStream.Switch("InputCommands.submit_hide")){\n')
	myfile.write('\thStream.Seek(0);\n')
	myfile.write('\thStream.GetToken(submit_hide, "");\n')
	myfile.write('\thStream.Switch((uint32)0);\n')
	myfile.write('}\n')
	myfile.write('if(submit_hide.Size() > 0) view_input_variables = False;\n')
	myfile.write('hmStream.SSPrintf(HtmlTagStreamMode, "form enctype=\\\"multipart/form-data\\\" method=\\\"post\\\"");\n')
	myfile.write('if(!view_input_variables){\n')
	myfile.write('\thmStream.SSPrintf(HtmlTagStreamMode, "input type=\\\"submit\\\" name=\\\"submit_view\\\" value=\\\"View input variables\\\"");\n')
	myfile.write('}\n')
	myfile.write('hmStream.SSPrintf(HtmlTagStreamMode, "/form");\n')
	myfile.write('hmStream.SSPrintf(HtmlTagStreamMode, "/body>\\n</html");\n')
	myfile.write('hStream.SSPrintf("OutputHttpOtions.Content-Type","text/html;charset=utf-8");\n')
	myfile.write('hStream.WriteReplyHeader(True);\n')
	myfile.write('return True;\n')
	myfile.write('}\n')
	myfile.close()
	
	command001 = "cp -f /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/algorithms/Makefile.inc /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM"
	ScriptUtil.executeSystemCommand(command001,1)
	command002 = "cp -f /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/algorithms/Makefile.linux /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM"
	ScriptUtil.executeSystemCommand(command002,1)
	command003 = "cp -f /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/algorithms/MARTe3.sh /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM"
	ScriptUtil.executeSystemCommand(command003,1)
	command004 = "cp -f /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/algorithms/compile_gam.sh /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM"
	ScriptUtil.executeSystemCommand(command004,1)
	command01 = "sed -i 's/NewAlgorithm/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"/g'"+" /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM/"+"Makefile.inc"
	ScriptUtil.executeSystemCommand(command01,1)
	command02 = "sed -i 's/NewAlgorithm/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"/g'"+" /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM/"+"MARTe3.sh"
	ScriptUtil.executeSystemCommand(command02,1)
	command03 = "sed -i 's/NewAlgorithm/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"/g'"+" /home/opertok/CSS-Workspaces/sys-mng-opi/CSS/gams/"+display.getWidget("FileNameTextInput2").getPropertyValue("text")+"GAM/"+"compile_gam.sh"
	ScriptUtil.executeSystemCommand(command03,1)
	