;  Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre
;  Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
;
;  This file is part of ProInfoData Windows Installer
;
;  ProInfoData Windows Installer is free software you can redistribute it
;  and/or modify it under the terms of the GNU General Public License as
;  published by the Free Software Foundation; either version 3 of the
;  License, or (at your option) any later version.
;
;  This program is distributed in the hope that it will be useful,
;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;  GNU General Public License for more details.
;
;  You should have received a copy of the GNU General Public License
;  along with this program; if not, write to the Free Software
;  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;  USA.

;Used variables
; R0 -> General use
; R1 -> General use
; R2 -> Temp variable
; R3 -> Variable used to set the lock state of the "next" button in INEP page

!define PRODUCT_NAME "DataSID"
!define PRODUCT_VERSION "1.0.0"
!define PRODUCT_PUBLISHER "C3SL - UFPR"
!define PRODUCT_WEB_SITE "http://datasid.c3sl.ufpr.br/"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\\
Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_UNINST_NAME "Desinstalar ${PRODUCT_NAME}"
!define PRODUCT_TASK "$PROGRAMFILES\${PRODUCT_NAME}\bin\agent\datasidAgent.exe"
!define NETMON_TASK "$PROGRAMFILES\${PRODUCT_NAME}\bin\agent\netmon.exe"
!define MUI_FINISHPAGE_NOAUTOCLOSE

SetCompressor bzip2
RequestExecutionLevel admin
XPStyle on
BrandingText "DataSID - C3SL"

;!addplugindir "~/.wine/drive_c/Program Files/NSIS/Unicode/Plugins"

;Installer headers
!include "nsDialogs.nsh"
!include "MUI.nsh"
!include LogicLib.nsh

;Installer images
!define MUI_ICON "..\images\datasid.ico"
!define MUI_UNICON "..\images\datasid.ico"
!define MUI_WELCOMEFINISHPAGE_BITMAP "..\images\wizard.bmp"
!define MUI_WELCOMEFINISHPAGE_BITMAP_NOSTRETCH
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "..\images\headerLeft.bmp"

;MUI Settings
!define MUI_ABORTWARNING

;Installer pages
!insertmacro MUI_PAGE_WELCOME
!define MUI_LICENSEPAGE_CHECKBOX
!insertmacro MUI_PAGE_LICENSE "../COPYING"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

;Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

;Language files
!insertmacro MUI_LANGUAGE "PortugueseBR"

;Reserve files
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
;MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "..\build\${PRODUCT_NAME}.exe"
InstallDir "$PROGRAMFILES\${PRODUCT_NAME}"

Var ProxyHost
Var ProxyPort
Var ProxyUser
Var ProxyPass

;Installer init function
Function .onInit
	;Get Proxy info
	ClearErrors
	FileOpen $0 "$EXEDIR\proxy" r
	${If} ${Errors}
		StrCpy $ProxyHost ""
		StrCpy $ProxyPort ""
		StrCpy $ProxyUser ""
		StrCpy $ProxyPass ""
	${Else}
		FileRead $0 $ProxyHost
		FileRead $0 $ProxyPort
		FileRead $0 $ProxyUser
		FileRead $0 $ProxyPass
		FileClose $0
	${EndIf}

    ;Prevent the user from run multiple instances of this installer
    System::Call 'kernel32::CreateMutexA(i 0, i 0, t "Instalador \
    DataSID") i .r1 ?e'
    Pop $R0
    StrCmp $R0 0 +3
    MessageBox MB_OK|MB_ICONEXCLAMATION "Uma instância do instalador já \
    está em execução."
    Abort

    ;Kill datasidAgent.exe process
    StrCpy $0 "datasidAgent.exe"
    DetailPrint "Killing all processes called '$0'"
    KillProc::KillProcesses 
    
    ;Kill datasidAgent.exe process
    StrCpy $0 "netmon.exe"
    DetailPrint "Killing all processes called '$0'"
    KillProc::KillProcesses

FunctionEnd


;Main installer section
Section "SeçãoPrincipal" SEC01
    SetOutPath "$INSTDIR"
    SetOverwrite ifnewer
	
	ClearErrors
	ReadRegStr $R2 HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyHost"
	${If} ${Errors}
		WriteRegStr HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyHost" $ProxyHost
		WriteRegStr HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyPort" $ProxyPort
		WriteRegStr HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyUser" $ProxyUser
		WriteRegStr HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyPass" $ProxyPass
	${EndIf}
	
	;Add DatasidAgent task in Windows task scheduler
	nsExec::Exec "schtasks /Create /SC HOURLY /MO 1 /TN DatasidAgent \
    /TR $\"\$\"${PRODUCT_TASK}$\"\$\" /RU System /F"
	
    ;Add net monitoring task in Windows task scheduler
    nsExec::Exec "schtasks /Create /SC MINUTE /MO 5 /TN DatasidNetMon \
    /TR $\"\$\"${NETMON_TASK}$\"\$\" /RU System /F"
	Pop $R2
	
	File "..\COPYING"
    CreateDirectory "$INSTDIR\conf"
    CreateDirectory "$INSTDIR\data"
    CreateDirectory "$INSTDIR\log"
    CreateDirectory "$INSTDIR\update"
    CreateDirectory "$INSTDIR\..\data"

    SetOutPath "$INSTDIR\bin"
    File "..\build\bin\client.exe"

    SetOutPath "$INSTDIR\bin\agent"
   	File "..\..\windows-collect\src\dist\API-MS-Win-Core-LocalRegistry-L1-1-0.dll"
    File "..\..\windows-collect\src\dist\API-MS-Win-Core-ProcessThreads-L1-1-0.dll"
    File "..\..\windows-collect\src\dist\API-MS-Win-Security-Base-L1-1-0.dll"
    File "..\..\windows-collect\src\dist\bz2.pyd"
    ;File "..\..\windows-collect\src\dist\KernelBase.dll"
    File "..\..\windows-collect\src\dist\library.zip"
    ;File "..\..\windows-collect\src\dist\powrprof.dll"
    File "..\..\windows-collect\src\dist\datasidAgent.exe"
    File "..\..\windows-collect\src\netmon\dist\netmon.exe"
    File "..\..\windows-collect\src\dist\pyexpat.pyd"
    File "..\..\windows-collect\src\dist\python27.dll"
    File "..\..\windows-collect\src\dist\pythoncom27.dll"
    File "..\..\windows-collect\src\dist\pywintypes27.dll"
    File "..\..\windows-collect\src\dist\select.pyd"
    File "..\..\windows-collect\src\dist\unicodedata.pyd"
    File "..\..\windows-collect\src\dist\w9xpopen.exe"
    File "..\..\windows-collect\src\dist\win32api.pyd"
    File "..\..\windows-collect\src\dist\win32ui.pyd"
    File "..\..\windows-collect\src\dist\_ctypes.pyd"
    File "..\..\windows-collect\src\dist\_hashlib.pyd"
    File "..\..\windows-collect\src\dist\_socket.pyd"
    File "..\..\windows-collect\src\dist\_ssl.pyd"
    File "..\..\windows-collect\src\dist\_win32sysloader.pyd"

    CopyFiles "$EXEDIR\pointInfo" "$INSTDIR\conf"
		
    WriteRegStr HKLM "SOFTWARE\${PRODUCT_NAME}" "Version" ${PRODUCT_VERSION}
	
    ;Execute DataSID Agent
    nsExec::Exec "schtasks /Run /TN DatasidAgent"
SectionEnd

;Uninstaller creation
Section -Post
    WriteUninstaller "$INSTDIR\${PRODUCT_UNINST_NAME}.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" \
    "DisplayName" "$(^Name)"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" \
    "UninstallString" "$INSTDIR\${PRODUCT_UNINST_NAME}.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" \
    "DisplayVersion" "${PRODUCT_VERSION}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" \
    "URLInfoAbout" "${PRODUCT_WEB_SITE}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" \
    "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

;Uninstaller init function
Function un.onInit
    MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Tem certeza que quer \
    remover completamente $(^Name) e todos os seus componentes?" IDYES +2
    Abort
FunctionEnd

;Shows a messagebox if the uninstall process is succeeded
Function un.onUninstSuccess
    HideWindow
    MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) foi removido com sucesso \
    do seu computador."
FunctionEnd

;Remove datasid files
Section Uninstall
    Delete "$INSTDIR\${PRODUCT_UNINST_NAME}.exe"
    Delete "$INSTDIR\COPYING"
    Delete "$INSTDIR\bin\client.exe"
    Delete "$INSTDIR\bin\agent\API-MS-Win-Core-LocalRegistry-L1-1-0.dll"
    Delete "$INSTDIR\bin\agent\API-MS-Win-Core-ProcessThreads-L1-1-0.dll"
    Delete "$INSTDIR\bin\agent\API-MS-Win-Security-Base-L1-1-0.dll"
    Delete "$INSTDIR\bin\agent\bz2.pyd"
    Delete "$INSTDIR\bin\agent\KERNELBASE.dll"
    Delete "$INSTDIR\bin\agent\library.zip"
    Delete "$INSTDIR\bin\agent\POWRPROF.dll"
    Delete "$INSTDIR\bin\agent\datasidAgent.exe"
    Delete "$INSTDIR\bin\agent\pyexpat.pyd"
    Delete "$INSTDIR\bin\agent\python27.dll"
    Delete "$INSTDIR\bin\agent\pythoncom27.dll"
    Delete "$INSTDIR\bin\agent\pywintypes27.dll"
    Delete "$INSTDIR\bin\agent\select.pyd"
    Delete "$INSTDIR\bin\agent\unicodedata.pyd"
    Delete "$INSTDIR\bin\agent\w9xpopen.exe"
    Delete "$INSTDIR\bin\agent\win32api.pyd"
    Delete "$INSTDIR\bin\agent\win32ui.pyd"
    Delete "$INSTDIR\bin\agent\_ctypes.pyd"
    Delete "$INSTDIR\bin\agent\_hashlib.pyd"
    Delete "$INSTDIR\bin\agent\_socket.pyd"
    Delete "$INSTDIR\bin\agent\_ssl.pyd"
    Delete "$INSTDIR\bin\agent\_win32sysloader.pyd"
    RMDir "$INSTDIR\bin"
    RMDir "$INSTDIR\conf"
    RMDir "$INSTDIR\data"
    RMDir "$INSTDIR\log"
    RMDir "$INSTDIR\update"
    RMDir "$INSTDIR\..\data"
    RMDir "$INSTDIR"

	DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
    DeleteRegValue HKLM "SOTWARE\${PRODUCT_NAME}" "Version"
    DeleteRegValue HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyHost"
    DeleteRegValue HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyPort"
    DeleteRegValue HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyUser"
    DeleteRegValue HKLM "SOFTWARE\${PRODUCT_NAME}" "ProxyPass"
    DeleteRegKey HKLM "SOFTWARE\${PRODUCT_NAME}"
	
	;Delete scheduled task
	nsExec::Exec "schtasks /Delete /TN DatasidAgent /F"
	Pop $R2
	
	;Delete scheduled task
	nsExec::Exec "schtasks /Delete /TN DatasidNetMon /F"
	Pop $R2

    SetAutoClose true
SectionEnd