# Compression (lzma = god)
!ifdef RELEASEBUILD
    SetCompressor /SOLID lzma
!endif

# Included files
!include MUI2.nsh
!include include\fileAssociate.nsh

Name Zandronum

# Add/Remove Programs entry  
!define REGKEY "SOFTWARE\$(^Name)"
!define COMPANY Zandronum
!define URL http://zandronum.com

# Installer graphics settings
    ; Installer EXE icon.
    !define MUI_ICON "res\icon_install.ico"
    
    ; Uninstaller EXE icon.
    !define MUI_UNICON "res\icon_uninstall.ico"
    
    ; The large side image shown at start / finish.
    !define MUI_WELCOMEFINISHPAGE_BITMAP "res\graphics_side.bmp"
    !define MUI_UNWELCOMEFINISHPAGE_BITMAP "res\graphics_sideun.bmp"
    
    ; The small icon shown at the top during setup.
    !define MUI_HEADERIMAGE
    !define MUI_HEADERIMAGE_BITMAP "res\graphics_top.bmp"
    
    ; The grayed text at the bottom.
    BrandingText " "
    
    Caption "Zandronum ${VERSION} Setup"

    ; While debugging, pause to show the install log.
    !ifndef RELEASEBUILD
      !define MUI_FINISHPAGE_NOAUTOCLOSE
      !define MUI_UNFINISHPAGE_NOAUTOCLOSE
    !endif

# Insert the installer pages
;Page custom nsUninstaller_create nsUninstaller_exit
Page custom nsAllInOne_create nsAllInOne_exit
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

# Insert uninstaller pages
UnInstPage custom un.nsUninstaller_create un.nsUninstaller_exit
!insertmacro MUI_UNPAGE_INSTFILES

# Languages
!insertmacro MUI_LANGUAGE English

# EXE attributes
VIProductVersion ${VERSION_NUM}.0.0.1
OutFile zandronum${VERSION}.exe
InstallDir $PROGRAMFILES\Zandronum
CRCCheck on
XPStyle on
ShowInstDetails show
VIAddVersionKey ProductName Zandronum
VIAddVersionKey ProductVersion "${VERSION}"
VIAddVersionKey CompanyName "${COMPANY}"
VIAddVersionKey CompanyWebsite "${URL}"
VIAddVersionKey FileVersion "${VERSION}"
VIAddVersionKey FileDescription ""
VIAddVersionKey LegalCopyright ""
InstallDirRegKey HKLM "${REGKEY}" Path
ShowUninstDetails show

# Variables
Var portableInstallation
Var shouldAssociate
Var shouldRemoveShortcuts
Var shouldRemoveAssociations
Var shouldRemoveDSConfig

# Form elements
Var textbox_Path
Var button_Browse
Var check_PortableInstall
Var check_Associate
Var check_RemoveShortcuts
Var check_RemoveAssociations
Var check_RemoveConfig
!define NSD_SETCHECK `!insertmacro _NSD_SETCHECK`
!macro _NSD_SETCHECK NAME TOGGLE
    ${If} ${TOGGLE} == 1
        SendMessage ${NAME} ${BM_SETCHECK} ${BST_CHECKED} 0
    ${Else}
        SendMessage ${NAME} ${BM_SETCHECK} ${BST_UNCHECKED} 0
    ${EndIf}
!macroend

Function un.nsUninstaller_create

    # Create the dialog.
    nsDialogs::Create /NOUNLOAD 1018
    Pop $0

    ${If} $0 == error
        Abort
    ${EndIf}
    nsDialogs::SetRTL /NOUNLOAD $(^RTL)
    
    # Create the "Welcome!" labels.
    ${NSD_CreateLabel} 8u 0u 100% 10u "This will uninstall Zandronum."
    ${NSD_CreateGroupBox} 8u 20u 95% 90u "Uninstall from"    
    
    ${NSD_CreateText} 16u 34u 90% 12u $INSTDIR
        Pop $0
        EnableWindow $0 0
    ${NSD_CreateCheckbox} 24u 54u -28u 10u "Remove shortcuts"
        Pop $check_RemoveShortcuts
    ${NSD_CreateCheckbox} 24u 66u -28u 10u "Remove PWAD associations"
        Pop $check_RemoveAssociations
    ${NSD_CreateCheckbox} 24u 78u -28u 10u "Remove static Doomseeker configuration"
        Pop $check_RemoveConfig
    ${NSD_SETCHECK} $check_RemoveShortcuts 1
    ${NSD_SETCHECK} $check_RemoveAssociations 1    
	${NSD_SETCHECK} $check_RemoveConfig 0
    
    nsDialogs::Show
FunctionEnd

Function un.nsUninstaller_exit
    ${NSD_GetState} $check_RemoveShortcuts $shouldRemoveShortcuts
    ${NSD_GetState} $check_RemoveAssociations $shouldRemoveAssociations
    ${NSD_GetState} $check_RemoveConfig $shouldRemoveDSConfig
FunctionEnd

Var mui.WelcomePage.Image
Var mui.WelcomePage.Image.Bitmap
Var mui.WelcomePage.Title.Font
Function nsAllInOne_create

    # Create the dialog (a special, full size type).
    nsDialogs::Create /NOUNLOAD 1044
    Pop $0

    ${If} $0 == error
        Abort
    ${EndIf}
    nsDialogs::SetRTL /NOUNLOAD $(^RTL)
    
    # Make the background white.
    SetCtlColors $0 "" "${MUI_BGCOLOR}"
    
    # Create the banner on the left.
    ${NSD_CreateBitmap} 0u 0u 109u 193u ""
    Pop $mui.WelcomePage.Image
    System::Call 'user32::LoadImage(i 0, t "$PLUGINSDIR\modern-wizard.bmp", i ${IMAGE_BITMAP}, i 0, i 0, i ${LR_LOADFROMFILE}) i.s'
    Pop $mui.WelcomePage.Image.Bitmap
    SendMessage $mui.WelcomePage.Image ${STM_SETIMAGE} ${IMAGE_BITMAP} $mui.WelcomePage.Image.Bitmap
    
    # Create the "Welcome!" labels.
    ${NSD_CreateLabel} 140u 30u -140u 12u "Welcome to Zandronum setup!"
        Pop $0
        SetCtlColors $0 "" "${MUI_BGCOLOR}"
        CreateFont $mui.WelcomePage.Title.Font "Tahoma" "8" "700"
        SendMessage $0 ${WM_SETFONT} $mui.WelcomePage.Title.Font 0
    ${NSD_CreateLabel} 140u 42u -140u 10u "Zandronum continues where Skulltag left off." # [JZ] i'm so original.
        Pop $0
        SetCtlColors $0 "" "${MUI_BGCOLOR}"
    
    # Create the install directory group.
    ${NSD_CreateGroupBox} 140u 128u -155u 32u "Install into"
        Pop $0
        SetCtlColors $0 "" "${MUI_BGCOLOR}"
        ${NSD_CreateDirRequest} 148u 140u -222u 12u $INSTDIR
            Pop $textbox_Path
            SetCtlColors $textbox_Path "" "${MUI_BGCOLOR}"
        ${NSD_CreateBrowseButton} -68u 139u 48u 14u "Browse..."
            Pop $button_Browse
            SetCtlColors $button_Browse "" "${MUI_BGCOLOR}"
            GetFunctionAddress $0 OfflineFileBrowseButton
            nsDialogs::OnClick /NOUNLOAD $button_Browse $0
            
    # Create the check boxes for extra settings.
    ${NSD_CreateCheckbox} 144u 168u 86u 10u "Portable installation"
        Pop $check_PortableInstall
        SetCtlColors $check_PortableInstall "" "${MUI_BGCOLOR}"
        ${NSD_OnClick} $check_PortableInstall nsAllInOne_checkPortable
		${NSD_SETCHECK} $check_PortableInstall 0
    ${NSD_CreateCheckbox} -100u 168u 86u 10u "Associate PWAD files"
        Pop $check_Associate
        SetCtlColors $check_Associate "" "${MUI_BGCOLOR}"  
        ${NSD_OnClick} $check_Associate nsAllInOne_checkPortable
		${NSD_SETCHECK} $check_Associate 1
		
    nsDialogs::Show
    
    # Delete the image from memory.
    System::Call gdi32::DeleteObject(i$mui.WelcomePage.Image.Bitmap)   
FunctionEnd

Function nsAllInOne_checkPortable		
	${NSD_GetState} $check_PortableInstall $portableInstallation    

	GetDlgItem $0 $HWNDPARENT 1 ; Next button
    ${If} $portableInstallation == 1
		${NSD_SetText} $0 "Extract"
		${NSD_SETCHECK} $check_Associate 0
		EnableWindow $check_Associate 0
	${Else}
		${NSD_SetText} $0 "Install"
		EnableWindow $check_Associate 1
	${EndIf}
FunctionEnd

Function OfflineFileBrowseButton
      ${NSD_GetText} $textbox_Path $INSTDIR
      nsDialogs::SelectFolderDialog /NOUNLOAD "Select the folder." $INSTDIR 
      Pop $2
      ${If} $2 != "error"
      ${AndIf} $2 != ""
          SendMessage $textbox_Path ${WM_SETTEXT} 0 STR:$2
     ${EndIf}
FunctionEnd

Function nsAllInOne_exit
    ${NSD_GetText} $textbox_Path $INSTDIR
    ${NSD_GetState} $check_PortableInstall $portableInstallation    
    ${NSD_GetState} $check_Associate $shouldAssociate
FunctionEnd

#===================================================
#
# Creates internet shortcuts.
#   Acquired 12/25/08 from http://nsis.sourceforge.net/CreateInternetShortcut_macro_&_function
#
#===================================================

!macro CreateInternetShortcut FILENAME URL
WriteINIStr "${FILENAME}.url" "InternetShortcut" "URL" "${URL}"
!macroend

#--------------------------
# The installation process.
Section "Installer"
    SetOverwrite on   

