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

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

!ifndef VERSION
  !define VERSION "1.0.0"
!endif

!ifndef APP_SOURCE_DIR
  !define APP_SOURCE_DIR "."
!endif

Name "Farm Control Server"
OutFile "${OUTFILE}"
InstallDir "$PROGRAMFILES64\Farm Control Server"
InstallDirRegKey HKLM "Software\Tom Butcher\Farm Control Server" "InstallDir"
RequestExecutionLevel admin

!define MUI_ABORTWARNING

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

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

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

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

Function .onInit
  ${If} ${Silent}
    SetAutoClose true
  ${EndIf}
FunctionEnd

Section "Farm Control Server" SecMain
  SectionIn RO
  SetOutPath $INSTDIR
  File /r "${APP_SOURCE_DIR}\*"

  !insertmacro customInstall

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

  WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd

Section "Desktop shortcut" SecDesktop
  !insertmacro createDesktopShortcut
SectionEnd

Section "Start Menu shortcut" SecStartMenu
  !insertmacro createStartMenuShortcut
SectionEnd

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${SecMain} "Install Farm Control Server."
  !insertmacro MUI_DESCRIPTION_TEXT ${SecDesktop} "Create a shortcut on the Desktop."
  !insertmacro MUI_DESCRIPTION_TEXT ${SecStartMenu} "Create a shortcut in the Start Menu."
!insertmacro MUI_FUNCTION_DESCRIPTION_END

Section "Uninstall"
  !insertmacro customUnInstall

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

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