; Modified to conform to Modern UI 2.0

!define PROJECT_PATH ".."

!define PRODUCT_NAME "%(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 " "
; 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
!insertmacro MUI_PAGE_COMPONENTS

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES

; Finish page
!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 files
!insertmacro MUI_LANGUAGE "English"

; Adds info to installer
VIProductVersion "0.12.0.0"
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"

; Install
Section !Required req
  SectionIn RO
    SetOutPath "$INSTDIR"
    SetOverwrite on

    ; Files

    File "${PROJECT_PATH}\dist\picard\"
    File /r "${PROJECT_PATH}\dist\picard\PyQt5"

    ; 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 "Languages" lang
  RMDir "$INSTDIR\locale"
  CreateDirectory "$INSTDIR\locale"
  SetOutPath "$INSTDIR\locale"
  File /r "${PROJECT_PATH}\dist\picard\locale\"
SectionEnd

SubSection "Shortcuts" shortcuts

  Section "Startmenu" startmenu
    SetShellVarContext all
    SetOutPath "$INSTDIR"
    CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}.lnk" "$INSTDIR\picard.exe" \
      "" "" "" SW_SHOWNORMAL "" "${PRODUCT_DESCRIPTION}"
  SectionEnd

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

SubSectionEnd

; 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 "QWidget"
!define WNDTITLE "${PRODUCT_NAME}"
Function un.onInit
  !insertmacro INSTALLOPTIONS_EXTRACT "removeSettings.ini"
  FindWindow $0 "${WNDCLASS}" "${WNDTITLE}"
  StrCmp $0 0 continueInstall
    MessageBox MB_ICONSTOP|MB_OK "The application you are trying to remove is running. Close it and try again."
    Abort
  continueInstall:
FunctionEnd

Function .onInit
  ${IfNot} ${RunningX64}
    MessageBox MB_ICONSTOP|MB_OK "This version of ${PRODUCT_NAME} requires a 64-bit Windows system."
    Abort
  ${EndIf}

  FindWindow $0 "${WNDCLASS}" "${WNDTITLE}"
  StrCmp $0 0 continueInstall
    MessageBox MB_ICONSTOP|MB_OK "The application you are trying to install is running. Close it and try again."
    Abort
  continueInstall:
  !insertmacro UnselectSection ${desktop}
FunctionEnd

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${req} "Installs ${PRODUCT_NAME} along with the necessary files for it run."
  !insertmacro MUI_DESCRIPTION_TEXT ${lang} "Installs translations of ${PRODUCT_NAME} in different languages."
  !insertmacro MUI_DESCRIPTION_TEXT ${desktop} "Installs a shortcut on the desktop."
  !insertmacro MUI_DESCRIPTION_TEXT ${startmenu} "Installs a shortcut in the Start Menu."
!insertmacro MUI_FUNCTION_DESCRIPTION_END
