; Modified to conform to Modern UI 2.0

!define PROJECT_PATH ".."

!define PRODUCT_NAME "%(display-name)s"
!define PRODUCT_VERSION "%(version)s"
!define PRODUCT_PUBLISHER "MusicBrainz"
!define PRODUCT_DESCRIPTION "%(description)s"
!define PRODUCT_URL "%(url)s"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor /FINAL /SOLID LZMA
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "picard-setup-${PRODUCT_VERSION}.exe"
ShowInstDetails show
ShowUnInstDetails show
BrandingText " "
Unicode true
; We need this so Windows 7/Vista lets us install what we need to
RequestExecutionLevel admin

; The default installation directory
InstallDir "$PROGRAMFILES64\${PRODUCT_NAME}"

; The default installation directory
InstallDirRegKey HKLM "Software\MusicBrainz\${PRODUCT_NAME}" "InstallDir"

!include "MUI2.nsh"
!include "InstallOptions.nsh"
!include "x64.nsh"

; MUI Settings

; Make installer pretty
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_RIGHT
!define MUI_HEADERIMAGE_BITMAP "${PROJECT_PATH}\installer\images\hx.bmp" ;
!define MUI_WELCOMEFINISHPAGE_BITMAP "${PROJECT_PATH}\installer\images\wiz.bmp"
!define MUI_ICON "${PROJECT_PATH}\installer\images\tango-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\classic-uninstall.ico"

; Abort Warning
!define MUI_ABORTWARNING

; Welcome page
!define MUI_WELCOMEPAGE_TITLE_3LINES
!insertmacro MUI_PAGE_WELCOME

; License page
!insertmacro MUI_PAGE_LICENSE "${PROJECT_PATH}\COPYING.txt"

; Directory page
!insertmacro MUI_PAGE_DIRECTORY

; Components page
!define MUI_COMPONENTSPAGE_SMALLDESC
!insertmacro MUI_PAGE_COMPONENTS

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES

; Finish page
!define MUI_FINISHPAGE_TITLE_3LINES
!define MUI_FINISHPAGE_RUN "$WINDIR\explorer.exe"
!define MUI_FINISHPAGE_RUN_PARAMETERS "$\"$INSTDIR\picard.exe$\""
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_CONFIRM
UninstPage custom un.RemoveSettingsPage
!insertmacro MUI_UNPAGE_INSTFILES
!define MUI_UNPAGE_FINISH_TITLE_3LINES
!insertmacro MUI_UNPAGE_FINISH

; Reserve files
ReserveFile "removeSettings.ini"
ReserveFile "${NSISDIR}\Plugins\x86-unicode\InstallOptions.dll"

; Language handling
!macro LOAD_LANGUAGE LANGUAGE
  !insertmacro MUI_LANGUAGE "${LANGUAGE}"
  !include "i18n\out\${LANGUAGE}.nsh"
!macroend

; Language files
!insertmacro LOAD_LANGUAGE "English"
; !insertmacro LOAD_LANGUAGE "Arabic"
!insertmacro LOAD_LANGUAGE "Albanian"
; !insertmacro LOAD_LANGUAGE "Catalan"
; !insertmacro LOAD_LANGUAGE "Czech"
; !insertmacro LOAD_LANGUAGE "Danish"
!insertmacro LOAD_LANGUAGE "Dutch"
!insertmacro LOAD_LANGUAGE "Estonian"
!insertmacro LOAD_LANGUAGE "Finnish"
!insertmacro LOAD_LANGUAGE "French"
!insertmacro LOAD_LANGUAGE "German"
; !insertmacro LOAD_LANGUAGE "Greek"
!insertmacro LOAD_LANGUAGE "Hebrew"
!insertmacro LOAD_LANGUAGE "Italian"
!insertmacro LOAD_LANGUAGE "Japanese"
; !insertmacro LOAD_LANGUAGE "Korean"
; !insertmacro LOAD_LANGUAGE "Norwegian"
; !insertmacro LOAD_LANGUAGE "Polish"
; !insertmacro LOAD_LANGUAGE "Portuguese"
!insertmacro LOAD_LANGUAGE "PortugueseBR"
; !insertmacro LOAD_LANGUAGE "Russian"
; !insertmacro LOAD_LANGUAGE "SimpChinese"
; !insertmacro LOAD_LANGUAGE "Slovak"
; !insertmacro LOAD_LANGUAGE "Slovenian"
!insertmacro LOAD_LANGUAGE "Spanish"
; !insertmacro LOAD_LANGUAGE "Swedish"
!insertmacro LOAD_LANGUAGE "TradChinese"
; !insertmacro LOAD_LANGUAGE "Turkish"
; !insertmacro LOAD_LANGUAGE "Ukrainian"

; Adds info to installer
VIProductVersion "%(file-version)s"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "${PRODUCT_NAME}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "Comments" "${PRODUCT_DESCRIPTION}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "${PRODUCT_PUBLISHER}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "© ${PRODUCT_PUBLISHER} under the GNU GPLv2."
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "Installation for ${PRODUCT_NAME}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "%(version)s"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductVersion" "%(version)s"

; Install
Section "!$(SectionRequired)" required
  SectionIn RO
    SetOutPath "$INSTDIR"
    SetOverwrite on

    ; Files
    File /r /x "locale" "${PROJECT_PATH}\dist\picard\"

    ; Write the installation path into the registry
    WriteRegStr HKLM "Software\MusicBrainz\${PRODUCT_NAME}" "InstallDir" "$INSTDIR"

    ; Create uninstaller
    WriteUninstaller "$INSTDIR\uninst.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "${PRODUCT_NAME}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\picard.exe"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "InstallSource" "$INSTDIR\"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Comments" "${PRODUCT_DESCRIPTION}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_URL}"
    WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
SectionEnd

Section "$(SectionLanguages)" lang
  RMDir "$INSTDIR\locale"
  CreateDirectory "$INSTDIR\locale"
  SetOutPath "$INSTDIR\locale"
  File /r "${PROJECT_PATH}\dist\picard\locale\"
SectionEnd

SectionGroup "$(SectionShortcuts)" shortcuts
  Section "$(SectionStartmenu)" startmenu
    SetShellVarContext all
    SetOutPath "$INSTDIR"
    CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}.lnk" "$INSTDIR\picard.exe" \
      "" "" "" SW_SHOWNORMAL "" "${PRODUCT_DESCRIPTION}"
  SectionEnd

  Section "$(SectionDesktop)" desktop
    SetShellVarContext all
    SetOutPath "$INSTDIR"
    CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\picard.exe" \
      "" "" "" SW_SHOWNORMAL "" "${PRODUCT_DESCRIPTION}"
  SectionEnd
SectionGroupEnd

; Uninstall
Function un.RemoveSettingsPage
  !insertmacro MUI_DEFAULT MUI_UNCONFIRMPAGE_TEXT_TOP ""
  !insertmacro MUI_DEFAULT MUI_UNCONFIRMPAGE_TEXT_LOCATION ""
  !insertmacro INSTALLOPTIONS_DISPLAY "removeSettings.ini"
FunctionEnd

Section Uninstall
  RMDir /r "$INSTDIR"

  SetShellVarContext all
  Delete "$DESKTOP\${PRODUCT_NAME}.lnk"

  SetShellVarContext all
  Delete "$SMPROGRAMS\${PRODUCT_NAME}.lnk"

  SetShellVarContext all
  Delete "$QUICKLAUNCH\${PRODUCT_NAME}.lnk"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "Software\MusicBrainz\${PRODUCT_NAME}"

  !insertmacro INSTALLOPTIONS_READ $R0 "removeSettings.ini" "Field 1" "State"
  StrCmp $R0 "1" 0 +2
    ; Even more attempts
    DeleteRegKey HKCU "Software\MusicBrainz\Picard\persist"
    DeleteRegKey HKCU "Software\MusicBrainz\Picard\setting"
    DeleteRegKey HKCU "Software\MusicBrainz\Picard"
SectionEnd

; Checks whether program is running.
!define WNDCLASS "Qt5QWindowIcon"
!define WNDTITLE "${PRODUCT_NAME}"
Function un.onInit
  FindWindow $0 "${WNDCLASS}" "${WNDTITLE}"
  StrCmp $0 0 continueInstall
    MessageBox MB_ICONSTOP|MB_OK "$(MsgApplicationRunning)"
    Abort
  continueInstall:
  !insertmacro INSTALLOPTIONS_EXTRACT "removeSettings.ini"
  !insertmacro INSTALLOPTIONS_WRITE "removeSettings.ini" "Field 1" "Text" "$(OptionRemoveSettings)"
FunctionEnd

Function .onInit
  ; Abort installation if this is not a 64 bit system
  ${IfNot} ${RunningX64}
    MessageBox MB_ICONSTOP|MB_OK "$(MsgRequires64Bit)"
    Abort
  ${EndIf}

  ; Abort installation if Picard is currently running
  FindWindow $0 "${WNDCLASS}" "${WNDTITLE}"
  StrCmp $0 0 continueInstall
    MessageBox MB_ICONSTOP|MB_OK "$(MsgApplicationRunning)"
    Abort
  continueInstall:

  ; Uninstall previous installation
  ReadRegStr $R0 HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
  StrCmp $R0 "" skipUninstall

  MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "$(MsgAlreadyInstalled)" IDOK uninstall
  Abort

  uninstall:
  ClearErrors
  Exec $R0

  ; Check if previous install location was inside $PROGRAMFILES32
  ReadRegStr $R0 HKLM "Software\MusicBrainz\${PRODUCT_NAME}" "InstallDir"
  StrLen $R1 $PROGRAMFILES32
  StrCpy $R2 $R0 $R1
  StrCmp $R2 $PROGRAMFILES32 0 skipUninstall
    StrCpy $INSTDIR "$PROGRAMFILES64\${PRODUCT_NAME}"

  skipUninstall:
  !insertmacro UnselectSection ${desktop}
FunctionEnd

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${required} "$(MuiDescriptionRequired)"
  !insertmacro MUI_DESCRIPTION_TEXT ${lang} "$(MuiDescriptionLang)"
  !insertmacro MUI_DESCRIPTION_TEXT ${shortcuts} "$(MuiDescriptionShortcuts)"
  !insertmacro MUI_DESCRIPTION_TEXT ${desktop} "$(MuiDescriptionDesktop)"
  !insertmacro MUI_DESCRIPTION_TEXT ${startmenu} "$(MuiDescriptionStartMenu)"
!insertmacro MUI_FUNCTION_DESCRIPTION_END
