LPARAMETERS tcProgram, tcVersiuneURL
*** IN LUCRU
* tcProgram: ROAEFACTURA
* tcVersiuneURL: https://www.romfast.ro/romfastsuport/files/suport.xml

#DEFINE CRLF CHR(13) + CHR(10)

lcProgram = IIF(!EMPTY(m.tcProgram), UPPER(ALLTRIM(m.tcProgram)), UPPER(ALLTRIM(SYS(16,0))))
lcServer = IIF(!EMPTY(m.tcVersiuneURL), m.tcVersiuneURL, [https://www.romfast.ro/romfastsuport/files/suport.xml])

llSucces = .F.
Try
	loHTTP = Createobject('winHTTP.winHTTPrequest.5.1')
	llSucces = .T.
CATCH TO loEx
	This.Log('Actualizare aplicatie - Eroare: ' + loEx.Message)
ENDTRY 

IF m.llSucces
	llSucces = .F.
	TRY 
		This.Log(m.lcServer)
		loHTTP.Open('GET', lcServer, .F.)
		loHTTP.setRequestHeader("Content-Type", "application/xml")
		loHTTP.Send()
		llSucces = .T.
	Catch To loEx
		This.Log('Actualizare aplicatie - Eroare: ' + loEx.Message)
	Endtry

ENDIF 

IF m.llSucces
	If loHTTP.Status <> 200
		This.Log('Actualizare aplicatie ' + loHTTP.StatusText)
		llSucces = .F.
	ELSE 
		goApp.Log('Actualizare aplicatie - versiunexml: SUCCES')
	Endif
ENDIF 


IF m.llSucces
	llSucces = .F.
	* Versiunea programului
	lcVersiuneClient = get_version(.F.,.T.)

	* Versiunea de pe server
	TRY 
		lcVersiuneXML = loHTTP.ResponseText
		XMLTOCURSOR(m.lcVersiuneXML, 'crsVersiuni')

		lcVersiuneServer = ''
		lcServer = ''
		SELECT crsVersiuni
		LOCATE FOR UPPER(ALLTRIM(program)) = m.lcProgram
		IF FOUND()
			lcVersiuneServer = ALLTRIM(UPPER(versiune))
			lcServer = ALLTRIM(fisier) && url aplicatie.exe
			llSucces = .T.
		ENDIF 
	CATCH TO loEx
		This.Log('Actualizare aplicatie - Eroare: ' + loEx.Message)
	ENDTRY 
ENDIF 

If m.llSucces AND m.lcVersiuneServer <> m.lcVersiuneClient AND !EMPTY(m.lcServer)
	llSucces = .F.
	TRY 
		This.Log(m.lcServer)
		loHTTP.Open('GET', lcServer, .F.)
		loHTTP.setRequestHeader("Content-Type", "application/vnd.microsoft.portable-executable")
		loHTTP.Send()
		llSucces = .T.
	Catch To loEx
		This.Log('Actualizare aplicatie - Eroare: ' + loEx.Message)
	Endtry
	IF m.llSucces 
		If loHTTP.Status <> 200
			This.Log('Actualizare aplicatie - Eroare: ' + loHTTP.StatusText)
			llSucces = .F.
		ELSE
 
			This.Log('Actualizare aplicatie - program.exe Succes')	
		ENDIF
	ENDIF 	
Endif


If !llDownloaded
	Return
ENDIF

lcBatFile = Addbs(Justpath(m.lcDestinationFile)) + 'UPDATE.BAT'
Set Textmerge On To (m.lcBatFile) Noshow
*!* wait 5 seconds for the application to quit
\@ping 127.0.0.1 -n 10 -w 1000 > nul
*!* rename original application
\REN "<<m.lcApplicationFile>>" "<<JUSTFNAME(m.lcBackupApplicationFile)>>"
*!* execute the new sfx archive - the sfx archive will start the application by itself
\START "" "<<m.lcDestinationFile>>"
\@ping 127.0.0.1 -n 5 -w 1000 > nul
*!* delete the archive
\DEL "<<m.lcDestinationFile>>"
Set Textmerge To

*!* DEZARHIVEZ APLICATIA DESCARCATA
llSucces = .F.
Try
	Cd (Justpath(m.lcApplicationFile))

	loProcess=Createobject("api_apprun", m.lcBatFile)

	goApp.Log("EXECUTARE PROGRAM;"+ " " + Alltrim(m.lcBatFile))
	loProcess.launchapp()
	llSucces = .T.

Catch To loException
	goApp.Log("EXECUTARE PROGRAM;"+ " " + Alltrim(m.lcBatFile) + " "  + CRLF + loEx.Message)
	Messagebox("EXECUTARE PROGRAM;"+ " " + Alltrim(m.lcBatFile) + " "  + CRLF + loEx.Message,0+16, _Screen.Caption)
Endtry
If llSucces
	On Error
	On Shutdown
	Set Procedure To
	Set Library To
	Set Classlib To
	Clear Events
	If _vfp.StartMode <> 0
		Quit
	Else
		Return .F.
	Endif
Endif
