  Unicode true
  SetCompressor /SOLID lzma

  !define MULTIUSER_EXECUTIONLEVEL Highest
  !define MULTIUSER_MUI
  !define MULTIUSER_INSTALLMODE_COMMANDLINE
  !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_KEY "Software\Battle for Wesnoth\%version"
  !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_VALUENAME ""
  !define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_KEY "Software\Battle for Wesnoth\%version"
  !define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_VALUENAME ""
  !define MULTIUSER_INSTALLMODE_INSTDIR "Battle for Wesnoth %version"
  !include "MultiUser.nsh"
  !include "MUI2.nsh"
  !include "LogicLib.nsh"
  !include "WinVer.nsh"

;--------------------------------
;General

  ;Name and file
  Name $(LWesnoth)
  OutFile "wesnoth-%version-win32.exe"

;--------------------------------
;Variables

  Var StartMenuFolder

;--------------------------------
;Interface Settings

  !define MUI_WELCOMEFINISHPAGE_BITMAP packaging\windows\WindowsInstallerGraphic.bmp
  !define MUI_ABORTWARNING
  !define MUI_WELCOMEPAGE_TITLE_3LINES
  !define MUI_FINISHPAGE_TITLE_3LINES
  !define MUI_LICENSEPAGE_TEXT_BOTTOM " "
  !define MUI_LICENSEPAGE_BUTTON $(^NextBtn)
  !define MUI_COMPONENTSPAGE_NODESC

;--------------------------------
;Language Selection Dialog Settings

  ;Remember the installer language
  !define MUI_LANGDLL_REGISTRY_ROOT "SHCTX"
  !define MUI_LANGDLL_REGISTRY_KEY "Software\Battle for Wesnoth\%version"
  !define MUI_LANGDLL_REGISTRY_VALUENAME "Installer Language"

;--------------------------------
;Pages

  !insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_LICENSE $(MUILicense)
  !insertmacro MULTIUSER_PAGE_INSTALLMODE
  !insertmacro MUI_PAGE_DIRECTORY

  ;Start Menu Folder Page Configuration
  !define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX"
  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\Battle for Wesnoth\%version"
  !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
  !define MUI_STARTMENUPAGE_DEFAULTFOLDER "Battle for Wesnoth %version"

  !insertmacro MUI_PAGE_STARTMENU Application $StartMenuFolder

  !insertmacro MUI_PAGE_INSTFILES

  !insertmacro MUI_PAGE_FINISH

  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_COMPONENTS
  !insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages
  !include "packaging\windows\translations.nsh"

;--------------------------------
;Reserve Files

  ;If you are using solid compression, files that are required before
  ;the actual installation should be stored first in the data block,
  ;because this will make your installer start faster.

  !insertmacro MUI_RESERVEFILE_LANGDLL
  ReserveFile /plugin "${NSISDIR}\Plugins\x86-unicode\*.dll"

;--------------------------------
;Installer Sections

Section "Battle for Wesnoth" BfWSection

  SectionIn RO

  SetOutPath "$INSTDIR\manual"
  File /r /x .* doc\manual\*.html
  File /r /x .* doc\manual\styles
  File /r /x .* doc\manual\images

  SetOutPath "$INSTDIR"
  File packaging\windows\wesnoth_editor-icon.ico

  File "/oname=README.txt" README.md
  File "/oname=copyright.txt" copyright
  File "/oname=COPYING.txt" COPYING
  File "/oname=changelog.txt" changelog.md
  File "/oname=players_changelog.txt" players_changelog.md
  File cwesnoth.cmd

  %NSIS_INSTALL_FILES

  SetOutPath "$INSTDIR"

  ;Store installation folder
  WriteRegStr SHCTX "Software\Battle for Wesnoth\%version" "" $INSTDIR

  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"

  ;Add uninstall information
  WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "DisplayName" "Battle for Wesnoth %version"
  WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "DisplayIcon" "$\"$INSTDIR\wesnoth.exe$\""
  WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "UninstallString" "$\"$INSTDIR\Uninstall.exe$\""
  WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "QuietUninstallString" "$\"$INSTDIR\Uninstall.exe$\" /S"
  WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "URLInfoAbout" "www.wesnoth.org"
  WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "DisplayVersion" "%version"
  WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "NoModify" 1
  WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version" "NoRepair" 1

  Call StartMenuShortcuts

SectionEnd

;--------------------------------
;Installer Functions

Function .onInit

  !insertmacro MULTIUSER_INIT
  !insertmacro MUI_LANGDLL_DISPLAY

FunctionEnd

Function StartMenuShortcuts

  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application

  ; Create shortcuts
  CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
  CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Battle for Wesnoth.lnk" "$INSTDIR\wesnoth.exe"
  CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Battle for Wesnoth (with console).lnk" "$INSTDIR\cwesnoth.cmd" "" "$INSTDIR\wesnoth.exe"
  CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Manual.lnk" "$INSTDIR\manual\manual.$(LCode).html"

  ${If} ${IsNT}
  ${AndIf} ${AtMostWin7}
  ; Win7 or lower has collapsed start menu folders, activate additional shortcuts.
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Map editor.lnk" "$INSTDIR\wesnoth.exe" "-e" "$INSTDIR\wesnoth_editor-icon.ico"
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Changelog.lnk" "$INSTDIR\changelog.txt"
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Player's changelog.lnk" "$INSTDIR\players_changelog.txt"
  ${EndIf}

  StrCpy $0 "$SMPROGRAMS\$StartMenuFolder\desktop.ini"
  ; Create desktop.ini file (allows to display localized start menu entries)
  ${IfNot} ${FileExists} $0
    FileOpen $9 $0 w
      FileWriteUTF16LE $9 ${U+FEFF} ; UTF16 BOM to enforce encoding
    FileClose $9
  ${EndIf}

  ; Add translations to desktop.ini
  ${If} $StartMenuFolder == "Battle for Wesnoth %version"
    WriteIniStr $0 ".ShellClassInfo" "LocalizedResourceName" "$(LWesnoth) %version"
  ${EndIf}
  WriteIniStr $0 "LocalizedFileNames" "Battle for Wesnoth.lnk" "$(LWesnoth)"
  WriteIniStr $0 "LocalizedFileNames" "Battle for Wesnoth (with console).lnk" "$(LWesnoth) (with console)"
  WriteIniStr $0 "LocalizedFileNames" "Manual.lnk" "$(LManual)"
  WriteIniStr $0 "LocalizedFileNames" "Map editor.lnk" "$(LEditor)"

  SetFileAttributes "$SMPROGRAMS\$StartMenuFolder" SYSTEM
  SetFileAttributes $0 HIDDEN|SYSTEM

  !insertmacro MUI_STARTMENU_WRITE_END

FunctionEnd

;--------------------------------
;Uninstaller Section

Section "un.$(LWesnoth)" un.BfW
  SectionIn RO  # read only (mandatory)

  %NSIS_UNINSTALL_FILES
  Delete "$INSTDIR\wesnoth_editor-icon.ico"
  RMDir /r "$INSTDIR\manual"
  Delete $INSTDIR\std*.txt
  Delete $INSTDIR\README.txt
  Delete $INSTDIR\copyright.txt
  Delete $INSTDIR\COPYING.txt
  Delete $INSTDIR\changelog.txt
  Delete $INSTDIR\players_changelog.txt
  Delete $INSTDIR\cwesnoth.cmd

  Delete "$INSTDIR\Uninstall.exe"

  RMDir "$INSTDIR"

  !insertmacro MUI_STARTMENU_GETFOLDER Application $StartMenuFolder

; If we created desktop.ini it'll start with a blank line
  FileOpen $9 "$SMPROGRAMS\$StartMenuFolder\desktop.ini" r
    FileReadUTF16LE $9 $1
  FileClose $9
  DetailPrint $1
  ${If} $1 == "$\r$\n"
    Delete "$SMPROGRAMS\$StartMenuFolder\desktop.ini"
  ${EndIf}

  Delete "$SMPROGRAMS\$StartMenuFolder\License.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Player's changelog.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Changelog.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Uninstall.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Manual.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Multiplayer server.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Map editor.lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Battle for Wesnoth (with console).lnk"
  Delete "$SMPROGRAMS\$StartMenuFolder\Battle for Wesnoth.lnk"
  RMDir "$SMPROGRAMS\$StartMenuFolder"

  DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\Battle for Wesnoth %version"
  DeleteRegKey /ifempty SHCTX "Software\Battle for Wesnoth\%version"
  DeleteRegKey /ifempty SHCTX "Software\Battle for Wesnoth"

SectionEnd

Section /o "un.$(LUserdata)" un.Userdata # optional
  RMDir /r "$DOCUMENTS\My Games\Wesnoth%version_major"
SectionEnd

;--------------------------------
;Uninstaller Functions

Function un.onInit

  !insertmacro MULTIUSER_UNINIT
  !insertmacro MUI_UNGETLANGUAGE

FunctionEnd
