!include "MUI2.nsh"
!include "LogicLib.nsh"
!include "installer.nsh"

!ifndef OUTFILE
  !define OUTFILE "farmcontrol-installer.exe"
!endif

!ifndef VERSION
  !define VERSION "0.1.0"
!endif

!ifndef BUILD_NUMBER
  !define BUILD_NUMBER "dev"
!endif

!ifndef APP_SOURCE_DIR
  !define APP_SOURCE_DIR "app"
!endif

!ifndef APP_FILE_LIST_GENERATOR
  !define APP_FILE_LIST_GENERATOR "..\..\scripts\generate-nsis-file-list.ps1"
!endif

; Enumerate APP_SOURCE_DIR now, while compiling, so the generated installer
; knows the exact unpacked byte total and can log each extracted file.
!insertmacro compileProgressFileCopy "${APP_SOURCE_DIR}" "${APP_FILE_LIST_GENERATOR}"

Name "Farm Control"
OutFile "${OUTFILE}"
InstallDir "$LOCALAPPDATA\Programs\Farm Control"
InstallDirRegKey HKCU "Software\Tom Butcher\Farm Control" "InstallDir"
RequestExecutionLevel user
SilentInstall normal

!define MUI_ABORTWARNING

!ifndef INSTALLER_ICON
  !define INSTALLER_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!endif

!ifndef UNINSTALLER_ICON
  !define UNINSTALLER_ICON "${INSTALLER_ICON}"
!endif

!define MUI_ICON "${INSTALLER_ICON}"
!define MUI_UNICON "${UNINSTALLER_ICON}"

Icon "${INSTALLER_ICON}"
UninstallIcon "${UNINSTALLER_ICON}"

BrandingText "Farm Control v${VERSION}-b${BUILD_NUMBER} Installer"

!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"

Function .onInit
  !insertmacro initProgressLog

  ${If} ${Silent}
    SetAutoClose true
  ${EndIf}

  !insertmacro progressPhase "Starting installation"
  !insertmacro progressStatus "Starting Farm Control installation..."
  !insertmacro progressCopyTotal "${APP_COPY_TOTAL_BYTES}"
  !insertmacro progressStart

  ; In-app updates install into Farm Control.new while the running process
  ; keeps using Farm Control; the folders are swapped after exit (/RESTARTFC).
  ; Interactive / silent fresh installs still remove the previous version first.
  ${If} $IsInAppUpdate == "1"
    !insertmacro prepareSideBySideUpdate
  ${Else}
    StrCpy $FinalInstDir $INSTDIR
    !insertmacro uninstallPreviousFarmControl
  ${EndIf}

  !insertmacro progressPrepare
FunctionEnd

Function .onInstFailed
  !insertmacro progressFailure "Installation failed."
FunctionEnd

Function .onInstSuccess
  Call restartFarmControlAfterUpdate
FunctionEnd

Section "Farm Control" SecMain
  SectionIn RO

  !insertmacro progressPhase "Copying application files"
  !insertmacro progressStatus "Copying application files..."
  !insertmacro progressCopyStart

  SetOverwrite try
  !insertmacro copyApplicationFilesWithProgress

  !insertmacro progressCopyComplete
  !insertmacro customInstall

  !insertmacro progressFinalizeInstall
  !insertmacro progressPhase "Finalizing installation"
  !insertmacro progressStatus "Writing installation registry entries..."

  WriteRegStr HKCU "Software\Tom Butcher\Farm Control" "InstallDir" $FinalInstDir
  WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control" \
    "DisplayName" "Farm Control"
  WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control" \
    "DisplayVersion" "${VERSION}"
  WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control" \
    "Publisher" "Tom Butcher"
  WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control" \
    "UninstallString" "$FinalInstDir\Uninstall.exe"
  WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control" \
    "NoModify" 1
  WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control" \
    "NoRepair" 1

  !insertmacro progressWriteUninstaller
  !insertmacro progressStatus "Writing uninstall information..."

  WriteUninstaller "$INSTDIR\Uninstall.exe"

  !insertmacro progressFinalize


  ; For in-app updates (/RESTARTFC) the success signal is emitted from
  ; restartFarmControlAfterUpdate, right before the install directory swap,
  ; so the running app quits while this installer stays alive.
  ${If} $RestartAfterInstall != "1"
    !insertmacro progressSuccess
  ${EndIf}
SectionEnd

Section "Uninstall"
  !insertmacro customUnInstall

  Delete "$INSTDIR\Uninstall.exe"
  RMDir /r "$INSTDIR"

  DeleteRegKey HKCU "Software\Tom Butcher\Farm Control"
  DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control"
SectionEnd
