; Script for creating a Windows installer with NSIS (http://nsis.sf.net)

!define PENTOBI_VERSION "@PENTOBI_VERSION@"
!define PENTOBI_SRC_DIR "@CMAKE_SOURCE_DIR@"
!define PENTOBI_BUILD_DIR "@CMAKE_BINARY_DIR@"

!define UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\Pentobi"

SetCompressor /SOLID lzma

!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico"
!define MUI_WELCOMEFINISHPAGE_BITMAP "${NSISDIR}\Contrib\Graphics\Wizard\orange.bmp"
!define MUI_COMPONENTSPAGE_NODESC
!include "MUI.nsh"
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${PENTOBI_SRC_DIR}\COPYING"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN "$INSTDIR\Pentobi.exe"
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "German"
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS

!define ADD_START_MENU_ENTRY_DEFAULT "Add start menu entry"
!define CREATE_DESKTOP_SHORTCUT_DEFAULT "Create desktop shortcut"
!define INSTALLER_TITLE_DEFAULT "Pentobi ${PENTOBI_VERSION} Installer"
LangString ADD_START_MENU_ENTRY ${LANG_ENGLISH} \
  "${ADD_START_MENU_ENTRY_DEFAULT}"
LangString CREATE_DESKTOP_SHORTCUT ${LANG_ENGLISH} \
  "${CREATE_DESKTOP_SHORTCUT_DEFAULT}"
LangString INSTALLER_TITLE ${LANG_ENGLISH} \
  "${INSTALLER_TITLE_DEFAULT}"
!include "${PENTOBI_SRC_DIR}\windows\German.nsh"

Name "Pentobi"
Caption "$(INSTALLER_TITLE)"
OutFile "pentobi-${PENTOBI_VERSION}-install.exe"
InstallDir "$PROGRAMFILES\Pentobi"
InstallDirRegKey HKLM "Software\Pentobi" ""
; Set admin level, needed for shortcut removal on Vista
; (http://nsis.sf.net/Shortcuts_removal_fails_on_Windows_Vista)
RequestExecutionLevel admin

Section

IfFileExists "$INSTDIR\Uninstall.exe" 0 +2
ExecWait '"$INSTDIR\Uninstall.exe" /S _?=$INSTDIR'

SetOutPath "$INSTDIR\translations"
File "${PENTOBI_BUILD_DIR}\src\libpentobi_gui\*.qm"
File "${PENTOBI_BUILD_DIR}\src\pentobi\*.qm"
SetOutPath "$INSTDIR\books"
File "${PENTOBI_SRC_DIR}\src\books\book_*.blksgf"
SetOutPath "$INSTDIR"
File /r "${PENTOBI_SRC_DIR}\src\pentobi\help"
File /oname=COPYING.txt "${PENTOBI_SRC_DIR}\COPYING"
File /oname=Pentobi.exe "${PENTOBI_BUILD_DIR}\windows\deploy\pentobi.exe"
File "${PENTOBI_SRC_DIR}\src\pentobi\pentobi.ico"
SetOutPath "$INSTDIR"
File "${PENTOBI_BUILD_DIR}\windows\deploy\*.dll"
SetOutPath "$INSTDIR\imageformats"
File "${PENTOBI_BUILD_DIR}\windows\deploy\imageformats\*.dll"
SetOutPath "$INSTDIR\platforms"
File "${PENTOBI_BUILD_DIR}\windows\deploy\platforms\*.dll"
SetOutPath "$INSTDIR\translations"
File "${PENTOBI_BUILD_DIR}\windows\deploy\translations\qt_de.qm"

WriteRegStr HKLM "Software\Pentobi" "" $INSTDIR

WriteUninstaller $INSTDIR\Uninstall.exe
WriteRegStr HKLM "${UNINST_KEY}" "DisplayName" "Pentobi"
WriteRegStr HKLM "${UNINST_KEY}" "DisplayVersion" "${PENTOBI_VERSION}"
WriteRegStr HKLM "${UNINST_KEY}" "DisplayIcon" "$INSTDIR\pentobi.ico"
WriteRegStr HKLM "${UNINST_KEY}" "URLInfoAbout" "http://pentobi.sf.net/"
WriteRegStr HKLM "${UNINST_KEY}" "UninstallString" "$INSTDIR\Uninstall.exe"

SetOutPath "$INSTDIR"
File "${PENTOBI_SRC_DIR}\windows\blksgf.ico"

WriteRegStr HKCR ".blksgf" "" "Pentobi"
WriteRegStr HKCR ".blksgf" "Content Type" "application/x-blokus-sgf"
WriteRegStr HKCR "Pentobi" "" "Blokus Game"
WriteRegStr HKCR "Pentobi\DefaultIcon" "" "$INSTDIR\blksgf.ico"
WriteRegStr HKCR "Pentobi\shell\open\command" "" \
  "$\"$INSTDIR\Pentobi.exe$\" $\"%1$\""

WriteRegStr HKCR "MIME\Database\Content Type\application/x-blokus-sgf" \
  "Extension" ".blksgf"

WriteRegStr HKCR "Applications\Pentobi.exe" "SupportedTypes" ".blksgf"
WriteRegStr HKCR "Applications\Pentobi\shell\open\command" "" \
  "$\"$INSTDIR\Pentobi.exe$\" $\"%1$\""

SectionEnd

Section "$(ADD_START_MENU_ENTRY)"

SetShellVarContext all
CreateDirectory "$SMPROGRAMS\Games"
CreateShortCut "$SMPROGRAMS\Games\Pentobi.lnk" "$INSTDIR\Pentobi.exe"

SectionEnd

Section "$(CREATE_DESKTOP_SHORTCUT)"

SetShellVarContext all
CreateShortCut "$DESKTOP\Pentobi.lnk" "$INSTDIR\Pentobi.exe"

SectionEnd

Section "Uninstall"

Delete "$INSTDIR\Uninstall.exe"
Delete "$INSTDIR\Pentobi.exe"
Delete "$INSTDIR\COPYING.txt"
Delete "$INSTDIR\pentobi.ico"
Delete "$INSTDIR\blksgf.ico"
Delete "$INSTDIR\*.dll"
RmDir /r "$INSTDIR\books"
RmDir /r "$INSTDIR\translations"
RmDir /r "$INSTDIR\help"
RmDir /r "$INSTDIR\platforms"
RmDir /r "$INSTDIR\imageformats"
RmDir /r "$INSTDIR\plugins"
RmDir "$INSTDIR"

SetShellVarContext all
Delete "$SMPROGRAMS\Games\Pentobi.lnk"
Delete "$DESKTOP\Pentobi.lnk"

DeleteRegKey HKLM "Software\Pentobi"
DeleteRegKey HKLM "${UNINST_KEY}"
DeleteRegKey HKCR "Pentobi"
DeleteRegKey HKCR "Applications\Pentobi.exe"
DeleteRegKey HKCR "Applications\Pentobi"

SectionEnd
