!include Sections.nsh
!include "MUI2.nsh"
!include "LogicLib.nsh"


Name "APS Setup Test"
OutFile "APS Install.exe"

InstallDir "C:\APS_Dashboard"



!define PROG1_InstDir    "C:\PROG1"
!define PROG2_InstDir    "C:\PROG2"
!define PROG3_InstDir 	 "C:\APS_Services"



!macro BackupFile FILE_DIR FILE BACKUP_TO
 IfFileExists "${BACKUP_TO}\*.*" +2
  CreateDirectory "${BACKUP_TO}"
 IfFileExists "${FILE_DIR}\${FILE}" 0 +2
  Rename "${FILE_DIR}\${FILE}" "${BACKUP_TO}\${FILE}"
!macroend

!macro RestoreFile BUP_DIR FILE RESTORE_TO
 IfFileExists "${BUP_DIR}\${FILE}" 0 +2
  Rename "${BUP_DIR}\${FILE}" "${RESTORE_TO}\${FILE}"
!macroend

!macro "UninstallDash"
#Remove installed copy
 Delete "$0\APSBusinessLayer.dll"
 Delete "$0\APSDashboard.exe"
 Delete "$0\APSDashboard.exe.config"
 Delete "$0\MPSRNG.dll"
 Delete "$0\RichTextBoxPrintCtrl.dll"
#Restore old copy
!insertmacro RestoreFile "$0\nsi-backup" "APSBusinessLayer.dll" "$0"
!insertmacro RestoreFile "$0\nsi-backup" "APSDashboard.exe" "$0"
!insertmacro RestoreFile "$0\nsi-backup" "APSDashboard.exe.config" "$0"
!insertmacro RestoreFile "$0\nsi-backup" "MPSRNG.dll" "$0"
!insertmacro RestoreFile "$0\nsi-backup" "RichTextBoxPrintCtrl.dll" "$0"


!macroEnd

!macro "UnistallClient"

Delete "$1\APS_DBClient_110a.exe"
!insertmacro RestoreFile "$1\nsi-backup" "APS_DBClient_110a.exe" "$1"
!insertmacro "UninstallDash"


!macroEnd

!macro "uninstallSoapService"
 Delete "$PROG3_InstDir\APSSOAPService.exe"

!insertmacro RestoreFile "$Prog3_InstDir\nsi-backup" "APSBusinessLayer.dll" "$PROG3_InstDir"

!macroEnd


!define MUI_ABORTWARNING


!define MUI_DIRECTORYPAGE_TEXT_TOP "$(MUI_DIRECTORYPAGE_TEXT_TOP_Dashboard)"
 
!define MUI_PAGE_CUSTOMFUNCTION_PRE InstallDB
!insertmacro MUI_PAGE_DIRECTORY 
!insertmacro MUI_PAGE_INSTFILES

!define MUI_DIRECTORYPAGE_TEXT_TOP "$(MUI_DIRECTORYPAGE_TEXT_TOP_DashBboardClient)"
!define MUI_PAGE_CUSTOMFUNCTION_PRE InstallClient
!insertmacro MUI_PAGE_DIRECTORY 
!insertmacro MUI_PAGE_INSTFILES

!define MUI_DIRECTORYPAGE_TEXT_TOP "$(MUI_DIRECTORYPAGE_TEXT_TOP_Service)"
!define MUI_PAGE_CUSTOMFUNCTION_PRE InstallService
!insertmacro MUI_PAGE_DIRECTORY 
!insertmacro MUI_PAGE_INSTFILES


LangString MUI_DIRECTORYPAGE_TEXT_TOP_Dashboard ${LANG_ENGLSH} "Setup will install the APS Dashboard"
LangString MUI_DIRECTORYPAGE_TEXT_TOP_DashBboardClient ${LANG_ENGLSH} "Setup will install the APS Dashboard Client"


SectionGroup /e "Dashboard" Dashboard

Section "Dashboard Install" SecDash

ReadINIStr $R0 "$PLUGINSDIR\test.ini" "Field 1" "State"


StrCpy $0 $INSTDIR 

SetOutPath $INSTDIR

!insertmacro BackupFile "$INSTDIR" "APSBusinessLayer.dll" "$INSTDIR\nsi-backup"
!insertmacro BackupFile "$INSTDIR" "APSDashboard.exe" "$INSTDIR\nsi-backup"
!insertmacro BackupFile "$INSTDIR" "APSDashboard.exe.config" "$INSTDIR\nsi-backup"
!insertmacro BackupFile "$INSTDIR" "MPSRNG.dll" "$INSTDIR\nsi-backup"
!insertmacro BackupFile "$INSTDIR" "RichTextBoxPrintCtrl.dll" "$INSTDIR\nsi-backup"

ClearErrors


file APSBusinessLayer.dll
file APSDashboard.exe
file APSDashboard.exe.config
file MPSRNG.dll
file RichTextBoxPrintCtrl.dll


IfErrors notPossible possible

notPossible: 
MessageBox MB_OK "Error occurred installing APS Dashboard"
!insertmacro UninstallDash

Abort

possible:
WriteRegStr HKLM APSDashboard InstallSource "$EXEDIR" 


SectionEnd

Section "Other" SEC2
SectionEND

SectionGroupEnd

SectionGroup /e "DashboardClient" dClient

Section "Dashboard Client Install" secClient


ReadINIStr $R0 "$PLUGINSDIR\test.ini" "Field 1" "State"
StrCpy $1 $INSTDIR 

SetOutPath $INSTDIR

!insertmacro BackupFile "$INSTDIR" "APS_DBClient_110a.exe" "$INSTDIR\nsi-backup"

ClearErrors
file "APS_DBClient_110a.exe"

IfErrors notPossible possible

notPossible:
MessageBox MB_OK "Error occurred installing APS Dashboard Client"
!insertmacro  UnistallClient
!insertmacro UninstallDash

Abort

possible:

SectionEnd

Section "Other" SEC3
SectionEND

SectionGroupEnd

SectionGroup /e "Service" SoapService
Section "APSSoapService Install" secService

StrCpy $INSTDIR ${PROG3_INSTDIR}
SetOutPath $INSTDIR

!insertmacro BackupFile "$INSTDIR" "APSSOAPService.exe" "$INSTDIR\nsi-backup"

ClearErrors

file "APSSoapService.exe"

IfErrors notPossible possible
notPossible:
MessageBox MB_OK "Error occurred installing APSSoapService"
!insertmacro uninstallSoapService
!insertmacro unistallClient
!insertmacro uninstallDash

Abort

possible:

SectionEnd

Section "Other" SEC4
SectionEND

SectionGroupEnd



!define PROG1_StartIndex ${Dashboard}
!define PROG1_EndIndex ${SEC2}
!define PROG2_StartIndex ${dClient}
!define PROG2_EndIndex ${SEC3}
!define PROG3_StartIndex ${SoapService}
!define PROG3_EndIndex ${SEC4}

Function .onInit
;InstallDirRegKey HKLM APSDashboard InstallSource

FunctionEnd


Function InstallDB

StrCpy $R0 ${PROG1_StartIndex}    # Group 1 start
 
  Loop:
   IntOp $R0 $R0 + 1
    StrCmp $R0 ${PROG1_EndIndex} 0 Loop


FunctionEND

Function InstallClient

StrCpy $R0 ${PROG2_StartIndex}    # Group 2 start
 
  Loop:
   IntOp $R0 $R0 + 1
    StrCmp $R0 ${PROG2_EndIndex} 0 Loop


FunctionEND

Function InstallService

StrCpy $R0 ${PROG3_StartIndex}    # Group 3 start
 
  Loop:
   IntOp $R0 $R0 + 1
    StrCmp $R0 ${PROG3_EndIndex} 0 Loop


FunctionEND







