import os
from subprocess import PIPE, Popen
import ctypes
import time
process=['wmic product where name="SYSPRO Runtime Components" call uninstall /nointeractive','wmic product where name="SYSPRO 7 Client Update 1 (2016)" call uninstall /nointeractive','wmic product where name="SYSPRO Reporting Designer Addin for Crystal 2013" call uninstall /nointeractive']
class disable_file_system_redirection:
    try:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    except:
        pass

batch1="""
::SYSPRO Deployment Product Export
::SYSPRO 8 2020 R2
::Created: 1/13/2021 9:34:23 PM

::NOTE: Please ensure all machines to be installed have the correct prerequisites installed.

::SYSPRO RUNTIME
::To install the SYSPRO Runtime, uncomment the lines below.
\\VM-ETB-APP-01\SYSPRODeploy\syspro-8-2020-r2\3.0.1999\CS_30.exe -skipsafenet /Quiet /NoRestart

::SYSPRO RUNTIME PATCH
::To install the SYSPRO Runtime Patch, uncomment the lines below.
\\VM-ETB-APP-01\SYSPRODeploy\syspro-8-2020-r2\3.0.1999\cs_30_pu07_179037.exe /Quiet /NoRestart

::SYSPRO 8 Client Version: 8.06.0000
msiexec.exe /i \\VM-ETB-APP-01\SYSPRODeploy\syspro-8-2020-r2\8.06.0000\SYSPRO.8.Client.msi GROUPID=0b91e701-fc21-4791-9de3-603599c4bfbf RELEASEID=44f0b6e7-5438-49c8-a340-129fc2688cbe CONTACTID=28341 CUSTOMERCODE=USA-ETBR001 ENDPOINT=net.tcp://vm-etb-app-01:30250/SYSPRODiscoveryService/ MEDIASHAREFOLDER=\"\\VM-ETB-APP-01\SYSPRODeploy\" ACCEPTULA=Yes UNINSTALLSYSPRO7=0 WCFINSTANCE=0 HOSTNAME=VM-ETB-APP-01 SYSPROCOMMSPORT=30110 INSTALLDIR="C:\SYSPROClient" /qn


"""

batch2="""
::SYSPRO Deployment Reporting Client-Side Product Export
::Created: 1/13/2021 9:39:23 PM

::NOTE: Please ensure all machines to be installed have the correct prerequisites installed.

::Businesss Objects Runtime 2016 x86 Version: 14.2.8.3426
\\VM-ETB-APP-01\SYSPRODeploy\syspro-additional-software\crystal2016-client\Businesss_Objects_Runtime_2016_x86\setup.exe -r \\VM-ETB-APP-01\SYSPRODeploy\syspro-additional-software\crystal2016-client\Businesss_Objects_Runtime_2016_x86\response.ini REBOOT=ReallySuppress

::Java runtime Version: 1.8
msiexec.exe /i \\VM-ETB-APP-01\SYSPRODeploy\syspro-additional-software\crystal2016-client\jre1.8.0_66.msi /qn

::SYSPRO 8 Reporting Components Version: 8.06.0002
\\VM-ETB-APP-01\SYSPRODeploy\syspro-additional-software\crystal2016-client\SYSPRO.8.Reporting.Components.exe /s /V" /qn "
"""
with disable_file_system_redirection():
    print "Uninstalling SYSPRO 7"
    p1=Popen(process[0],stdout=PIPE,stderr=PIPE)
    r1,e1=p1.communicate()
    if e1:
        print e1
    else:
        print r1
    p2=Popen(process[1],stdout=PIPE,stderr=PIPE)
    r2,e2=p2.communicate()
    if e2:
        print e2
    else:
        print r2
    print "Uninstalling Crystal Reporting"
    p3=Popen(process[2],stdout=PIPE,stderr=PIPE)
    r3,e3=p3.communicate()
    if e3:
        print e3
    else:
        print r3
    print "Installing SYSPRO 8"
    path=os.environ['programdata']+"\\batch1.bat"
    with open(path,"w") as f:
        f.write(batch1)
    path1=os.environ['programdata']+"\\batch2.bat"
    with open(path1,"w") as f:
        f.write(batch2)
    try:
        process =Popen([path],stdout=PIPE,stderr=PIPE)
        r,e=process.communicate()
        if e:
            print e
        else:
            print r
        time.sleep(300)
        process1 =Popen([path1],stdout=PIPE,stderr=PIPE)
        r_1,e_1=process1.communicate()
        if e:
            print e_1
        else:
            print r_1
    except  Exception as E:
        print E
    
    
    