# -*- coding: utf-8 -*-
from setuphelpers import *
import os
import shutil
uninstallkey = []

def install():
    def killsoft():
      for soft in ('WINWORD.EXE','POWERPNT.EXE','EXCEL.exe','MSPUB.EXE','MSACCESS.EXE','INFOPATH.EXE','lync.exe','ONENOTE.EXE','OUTLOOK.EXE','ONENOTEM.EXE','MSOSYNC.EXE','GROOVE.EXE'):
        if isrunning(soft):
           print ("closing %s" % soft)
           killalltasks(soft)

    visio2010 = 'Microsoft Office Visio 2010'
    visiopresent = installed_softwares(visio2010)
    if not visiopresent:
        print('Visio absent')
    else:
        print('désinstallation de visio')
        print('copie du fichier visio.xml')
        if isdir(r'C:\visio'):
            remove_tree(r'C:\visio')
        copytree2(r'visio',r'C:\visio')
        run(r'"C:\visio\visio.exe"')
        if isdir(r'C:\visio'):
            remove_tree(r'C:\visio')

    proplus2007 = 'Microsoft Office Professional Plus 2007'
    proplus07present = installed_softwares(proplus2007)
    if not proplus07present:
        print('Office proplus 2007 absent')
    else:
        print('désinstallation de ProPlus 2007')
        print('copie du fichier proplus.xml')
        if isdir(r'C:\2007'):
            remove_tree(r'C:\2007')
        copytree2(r'2007',r'C:\2007')
        run(r'"C:\2007\proplus07.exe"')
        if isdir(r'C:\2007'):
            remove_tree(r'C:\2007')

    proplus2010 = 'Microsoft Office Professionnel Plus 2010'
    propluspresent = installed_softwares(proplus2010)
    if not propluspresent:
        print('Office proplus absent')
    else:
        print('désinstallation de Proplus 2010')
        print('copie du fichier proplus.xml')
        if isdir(r'C:\2010'):
            remove_tree(r'C:\2010')
        copytree2(r'2010',r'C:\2010')
        run(r'"C:\2010\proplus.exe"')
        if isdir(r'C:\2010'):
            remove_tree(r'C:\2010')


    office2016 = 'Microsoft Office Professionnel Plus 2016'
    officepresent = installed_softwares(office2016)

    print('installing Office 2016')
    if not officepresent:
            cmd = '"office\setup.exe" /adminfile "pedago.msp"'
    else:
            cmd = 'msiexec.exe /p pedago.msp'
    killsoft()
    run(cmd,timeout=1200,accept_returncodes=[1641,3010,0])
    officepresent = installed_softwares(office2016)
    if not officepresent:
        error('End audit did not find the software.')

def uninstall():
    fichier = open("%s\pedago.msp.xml" % programfiles32, "x" )
    fichier.write('<Configuration Product="ProPlus"> \n')
    fichier.write('<Display Level="none" CompletionNotice="no" SuppressModal="yes" AcceptEula="yes" /> \n')
    fichier.write('   <Setting Id="SETUP_REBOOT" Value="NEVER" /> \n')
    fichier.write('</Configuration> \n')
    fichier.close()
    run('"%s\Common Files\microsoft shared\OFFICE16\Office Setup Controller\Setup.exe" /uninstall ProPlus /config "%s\pedago.msp.xml"' % (programfiles32,programfiles32),timeout=1200)
    remove_file(r'%s\pedago.msp.xml'% programfiles32)