; This script is based upon:
; (1) the "{NSIS folder}\Examples\Modern UI\Basic.nsi" script
; (2) the "{NSIS folder\Examples\one-section.nsi" script

; Allow only the following combinations to be installed:
;
;   (1) Res 1 only
;   (2) Res 2 only
;   (3) Res 3 only
;   (4) Res 4 only
;   (5) Res 5 only

;--------------------------------
; Include Modern UI

  !include "MUI.nsh"
  !include "Sections.nsh"

;--------------------------------
; Product Info

  !define MUI_PRODUCT "Demo with 5 options"
  !define MUI_VERSION "0.22.0"

;--------------------------------
; Configuration

  ; General
  OutFile "Basic-6.exe"

;--------------------------------
; Pages

  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_INSTFILES
  
;--------------------------------
; Modern UI Configuration

  !define MUI_ABORTWARNING
  !define MUI_COMPONENTSPAGE_SMALLDESC
  
;--------------------------------
; Languages
  !define MUI_INNERTEXT_COMPONENTS_TOP \
      "Check the 'Res' component you want to install. You can only select one of these components."
  !insertmacro MUI_LANGUAGE "English"
  
;--------------------------------
; Defines
  
  !define RES        $R9   ; used to remember the 'Res' selection
  !define CANNON     $R8   ; used to remember the 'Cannon' selection
  !define HANGAR     $R7   ; used to remember the 'Hangar' selection

;--------------------------------
; Installer Sections

SubSection /e "!Res option" subsecRes

Section "Res 1" secR1
  ; insert commands here to install relevant files for this option
  
  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install 'Res' option 1"
SectionEnd

Section /o "Res 2" secR2
  ; insert commands here to install relevant files for this option
  
  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install 'Res' option 2"
SectionEnd

Section /o "Res 3" secR3
  ; insert commands here to install relevant files for this option
  
  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install 'Res' option 3"
SectionEnd

Section /o "Res 4" secR4
  ; insert commands here to install relevant files for this option
  
  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install 'Res' option 4"
SectionEnd

Section /o "Res 5" secR5
  ; insert commands here to install relevant files for this option
  
  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install 'Res' option 5"
SectionEnd

SubSectionEnd

SubSection /e "!Ion Cannon" subsecCannon

Section "Cannon A" secCA
  ; insert commands here to install relevant files for this option

  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install Cannon option A"
SectionEnd

Section /o "Cannon B" secCB
  ; insert commands here to install relevant files for this option

  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install Cannon option B"
SectionEnd

SubSectionEnd

SubSection /e "!Hangar" subsecHangar

Section "Hangar A" secHA
  ; insert commands here to install relevant files for this option

  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install Hangar option A"
SectionEnd

Section /o "Hangar B" secHB
  ; insert commands here to install relevant files for this option

  ; display message just to show this section has been 'installed'
  MessageBox MB_OK "Install Hangar option B"
SectionEnd

SubSectionEnd

;--------------------------------
; Descriptions

  LangString DESC_subsecRes ${LANG_ENGLISH} \
              "Choose one of the five 'Res' options."
  LangString DESC_secR1        ${LANG_ENGLISH} \
              "This installs 'Res' option 1 which is RED."
  LangString DESC_secR2        ${LANG_ENGLISH} \
              "This installs 'Res' option 2 which is ORANGE."
  LangString DESC_secR3        ${LANG_ENGLISH} \
              "This installs 'Res' option 3 which is YELLOW."
  LangString DESC_secR4        ${LANG_ENGLISH} \
              "This installs 'Res' option 4 which is GREEN."
  LangString DESC_secR5        ${LANG_ENGLISH} \
              "This installs 'Res' option 5 which is BLUE."
  LangString DESC_subsecCannon ${LANG_ENGLISH} \
              "Choose one of the two 'Ion Cannon' options."
  LangString DESC_secCA        ${LANG_ENGLISH} \
              "This installs 'Ion Cannon' option A which does wonderful things."
  LangString DESC_secCB        ${LANG_ENGLISH} \
              "This installs 'Ion Cannon' option B which does something different."
  LangString DESC_subsecHangar ${LANG_ENGLISH} \
              "Choose one of the two 'Hangar' options."
  LangString DESC_secHA        ${LANG_ENGLISH} \
              "This installs 'Hangar' option A which adds support for something."
  LangString DESC_secHB        ${LANG_ENGLISH} \
              "This installs 'Hangar' option B which adds extra features."

  !insertmacro MUI_FUNCTIONS_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${subsecRes} $(DESC_subsecRes)
    !insertmacro MUI_DESCRIPTION_TEXT ${secR1}     $(DESC_secR1)
    !insertmacro MUI_DESCRIPTION_TEXT ${secR2}     $(DESC_secR2)
    !insertmacro MUI_DESCRIPTION_TEXT ${secR3}     $(DESC_secR3)
    !insertmacro MUI_DESCRIPTION_TEXT ${secR4}     $(DESC_secR4)
    !insertmacro MUI_DESCRIPTION_TEXT ${secR5}     $(DESC_secR5)
    !insertmacro MUI_DESCRIPTION_TEXT ${subsecCannon} $(DESC_subsecCannon)
    !insertmacro MUI_DESCRIPTION_TEXT ${secCA}        $(DESC_secCA)
    !insertmacro MUI_DESCRIPTION_TEXT ${secCB}        $(DESC_secCB)
    !insertmacro MUI_DESCRIPTION_TEXT ${subsecHangar} $(DESC_subsecHangar)
    !insertmacro MUI_DESCRIPTION_TEXT ${secHA}        $(DESC_secHA)
    !insertmacro MUI_DESCRIPTION_TEXT ${secHB}        $(DESC_secHB)
  !insertmacro MUI_FUNCTIONS_DESCRIPTION_END

;--------------------------------
; Macros

!macro HandleSection SECTION
	SectionGetFlags ${SECTION} $0
	IntOp $0 $0 & ${SF_SELECTED}
	IntCmp $0 ${SF_SELECTED} 0 +2 +2
		StrCpy ${RES} ${SECTION}
!macroend

!macro CannonSection SECTION
	SectionGetFlags ${SECTION} $0
	IntOp $0 $0 & ${SF_SELECTED}
	IntCmp $0 ${SF_SELECTED} 0 +2 +2
		StrCpy ${CANNON} ${SECTION}
!macroend

!macro HangarSection SECTION
	SectionGetFlags ${SECTION} $0
	IntOp $0 $0 & ${SF_SELECTED}
	IntCmp $0 ${SF_SELECTED} 0 +2 +2
		StrCpy ${HANGAR} ${SECTION}
!macroend

;--------------------------------
; Functions

Function .onInit
  StrCpy ${RES} ${secR1}       ; Gotta remember which 'Res' section we are at now...

  StrCpy ${CANNON} ${secCA} ; Gotta remember which 'Cannon' section we are at now...

  StrCpy ${HANGAR} ${secHA} ; Gotta remember which 'Hangar' section we are at now...

 FunctionEnd

Function .onSelChange
	Push $0
  Push $2

  ; Check if the user have selected all of the sections using the sub-section
	SectionGetFlags ${subsecRes} $0
	IntOp $0 $0 & ${SF_SELECTED}
	StrCmp $0 0 skip
	SectionSetFlags ${secR1} 0
	SectionSetFlags ${secR2} 0
	SectionSetFlags ${secR3} 0
	SectionSetFlags ${secR4} 0
	SectionSetFlags ${secR5} 0
skip:

	; Turn off old selected section
	!insertmacro UnselectSection ${RES}

	; Now remember the current selection
	StrCpy $2 ${RES}

  !insertmacro HandleSection ${secR1}
  !insertmacro HandleSection ${secR2}
  !insertmacro HandleSection ${secR3}
  !insertmacro HandleSection ${secR4}
  !insertmacro HandleSection ${secR5}
  
	StrCmp $2 ${RES} 0 res ; if selection hasn't changed, re-select it
  !insertmacro SelectSection ${RES}
  
res:
  Pop $2
	Pop $0
	Push $0
  Push $2

  SectionGetFlags ${subsecCannon} $0
	IntOp $0 $0 & ${SF_SELECTED}
  StrCmp $0 0 cannon
  SectionSetFlags ${secCA} 1
  SectionSetFlags ${secCB} 1
cannon:

	; Turn off old selected section
	!insertmacro UnselectSection ${CANNON}

	; Now remember the current selection
	StrCpy $2 ${CANNON}

  !insertmacro CannonSection ${secCA}
  !insertmacro CannonSection ${secCB}

	StrCmp $2 ${CANNON} 0 ion ; if selection hasn't changed, re-select it
  !insertmacro SelectSection ${CANNON}
  
ion:
  Pop $2
	Pop $0
	Push $0
  Push $2

  SectionGetFlags ${subsecHangar} $0
	IntOp $0 $0 & ${SF_SELECTED}
  StrCmp $0 0 hangar
  SectionSetFlags ${secHA} 1
  SectionSetFlags ${secHB} 1
hangar:

	; Turn off old selected section
	!insertmacro UnselectSection ${HANGAR}

	; Now remember the current selection
	StrCpy $2 ${HANGAR}

  !insertmacro HangarSection ${secHA}
  !insertmacro HangarSection ${secHB}

	StrCmp $2 ${HANGAR} 0 done ; if selection hasn't changed, re-select it
  !insertmacro SelectSection ${HANGAR}

done:
  Pop $2
	Pop $0
FunctionEnd

; end of script
