; Copyright (c) 2010-2016 Kouhei Sutou <kou@clear-code.com>

!define PACKAGE_NAME "@PACKAGE@"
!define PRODUCT_NAME "@PACKAGE_LABEL@"
!define PRODUCT_VERSION "@VERSION@"
!define PRODUCT_PUBLISHER "Brazil"
!define PRODUCT_WEB_SITE "http://groonga.org/"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"

SetCompress force
SetCompressor lzma

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

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_LANGDLL_ALLLANGUAGES
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

!insertmacro MUI_PAGE_WELCOME
!define MUI_LICENSEPAGE_CHECKBOX
!insertmacro MUI_PAGE_LICENSE "LICENSE"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
Var START_MENU_FOLDER
!define MUI_STARTMENUPAGE_NODISABLE
!define MUI_STARTMENUPAGE_DEFAULTFOLDER "${PRODUCT_NAME}"
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}"
!insertmacro MUI_PAGE_STARTMENU Application $START_MENU_FOLDER
!insertmacro MUI_PAGE_INSTFILES
; !define MUI_FINISHPAGE_SHOWREADME "$INSTDIR\doc\README.txt"
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "Japanese"
!insertmacro MUI_RESERVEFILE_LANGDLL

!include "language-files\english.nsi"
!include "language-files\japanese.nsi"

Function .onInit
  !insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd


Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "files\${PACKAGE_NAME}-${PRODUCT_VERSION}-x64.exe"
InstallDir "$PROGRAMFILES64\${PRODUCT_NAME}"
ShowInstDetails show
ShowUnInstDetails show


# Installer

Section "groonga"
  SectionIn 1 RO
  SetOverwrite ifdiff

  SetOutPath $INSTDIR
  File /r dist-x64\*
  File /oname=bin\create-setup.bat create-setup.bat

  ExecWait '"$INSTDIR\bin\create-setup.bat" "$INSTDIR\bin"'

  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  SetShellVarContext all
  CreateDirectory "$SMPROGRAMS\$START_MENU_FOLDER"
  CreateShortCut "$SMPROGRAMS\$START_MENU_FOLDER\Uninstall.lnk" $INSTDIR\uninstall.exe
  CreateShortCut "$SMPROGRAMS\$START_MENU_FOLDER\groonga command prompt.lnk" "$SYSDIR\cmd.exe" '/E:ON /K ""$INSTDIR\bin\setup.bat""'
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section -Post
  WriteUninstaller $INSTDIR\uninstall.exe
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" $INSTDIR\uninstall.exe
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "StartMenuFolder" "$START_MENU_FOLDER"
SectionEnd

# Uninstaller

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK $(FinishUninstallSuccessfully)
FunctionEnd

Function un.onInit
  !insertmacro MUI_UNGETLANGUAGE
FunctionEnd

Section "Uninstall"
  ReadRegStr $START_MENU_FOLDER ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "StartMenuFolder"
  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"

  RMDir /r $INSTDIR

  SetShellVarContext all
  RMDir /r "$SMPROGRAMS\$START_MENU_FOLDER"
SectionEnd
