!define SC_NAME "Source"
!define SC_WEB_SITE_LINK "$INSTDIR\Source Code Homepage.url"

!include "Installer.nsh"
!include "MUI.nsh"
!include "LogicLib.nsh"

!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"
!define DIR_MODULES "Modules"


; MUI Settings
!define MUI_ABORTWARNING
!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_PAGE_CUSTOMFUNCTION_PRE SourceCode_Check
!insertmacro MUI_PAGE_DIRECTORY

!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

; Uninstaller
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------

OutFile "${SCRIPT_OUTPUT_DIR}\SourceCode_${PRODUCT_NAME}${PRODUCT_VERSION}.exe"
ShowInstDetails show
ShowUnInstDetails show

; Check to see if an older version is already installed.
Function SourceCode_Check
  call RetrieveInstallDir
  ${If} $0 == "" ; did not install DB3 first
    MessageBox MB_OKCANCEL \
      "Warning!  No existing install of ${PRODUCT_NAME} detected.  \
      While you can still install the source code, some features \
      in build scripts and program settings may not work properly.  \
      Continue anyway?" \
      IDOK OK IDCANCEL Cancel
    Cancel:
      Quit
    OK:
  ${Else} ; DB3 already installed, so proceed
    StrCpy $INSTDIR "$0/Source" ;
  ${EndIf}
FunctionEnd

Section -AdditionalIcons
  SetOutPath $INSTDIR
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  WriteIniStr "${SC_WEB_SITE_LINK}" "InternetShortcut" "URL" "${SC_WEB_SITE}"
  CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\Homepage.lnk" "${WEBSITE_LINK}"
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "SourceLocation" "$INSTDIR"
SectionEnd

Section "Source Code" SEC01
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateDirectory "${DIR_MODULES}"
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section "Default Bots" SEC02
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateDirectory "${DIR_BOTS}"
    SetOutPath "${DIR_BOTS}"
    File /nonfatal ${DEFAULT_ASSETS}\Bots\*.*
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section "Default Settings" SEC03
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateDirectory "${DIR_SETTINGS}"
    SetOutPath "${DIR_SETTINGS}"
    File /nonfatal ${DEFAULT_ASSETS}\Settings\*.*
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section "Default Simulations" SEC04
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateDirectory "${DIR_SIMS}"
    SetOutPath "${DIR_SIMS}"
    File /nonfatal ${DEFAULT_ASSETS}\Sims\*.*
  !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

; Section descriptions
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC01} "The main program."
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC02} "Some bots written by the community to get you started."
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC03} "Some default settings to get you started."
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC04} "Some already running simulations to get you started."
!insertmacro MUI_FUNCTION_DESCRIPTION_END


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  !insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP
  Delete "${WEBSITE_LINK}"
  Delete "$INSTDIR\uninst.exe"

  RMDir "${DIR_BOTS}"
  RMDir /r "${DIR_SHARING}" ; force removal of the sharing directory since there's nothing in there we need
  RMDir "${DIR_SETTINGS}"
  RMDir "${DIR_SIMS}"

  RMDir  $INSTDIR ; // remove only if it's not empty (the user might have installed it somewhere weird. like c:\)

  RMDir /r "$SMPROGRAMS\$ICONS_GROUP"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  SetAutoClose true
SectionEnd