/*
 * Copyright 2012 Amazon Technologies, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://aws.amazon.com/apache2.0
 * 
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and
 * limitations under the License.
 */ 

/*
 * NSIS Windows installer code for the CLT.
 *
 * Initially auto-generated by EclipseNSIS Script Wizard.
 */

# Development defaults that will be overridden by Ant during releases
!ifndef VERSION
    !define VERSION "1.3.1"
!endif
!ifndef BASEDIR
    !define BASEDIR "..\dist\temp\aws-mturk-clt-${VERSION}"
!endif
!ifndef OUTFILE
    !define OUTFILE "..\dist\mech-turk-setup.exe"
!endif
#!define INCLUDEJRE

# This doesn't work
#Name "${COMPANY} ${SHORT_NAME}"
Name "Amazon Mechanical Turk Command Line Tools"

SetCompressor lzma

# Defines
!define COMPANY "Amazon Mechanical Turk"
!define SHORT_NAME "Command Line Tools"
!define URL "http://requester.mturk.com/"
!define REGKEY "SOFTWARE\$(^Name)"

# MUI defines
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
!define MUI_WELCOMEPAGE_TITLE "Welcome to the $(^Name) Setup"
!define MUI_WELCOMEPAGE_TITLE_3LINES
!define MUI_WELCOMEPAGE_TEXT "This will install and set up the $(^Name).$\n$\nClick Next to continue."
!define MUI_DIRECTORYPAGE_TEXT_TOP "This will install the $(^Name) in the following folder. To install in a different folder, click Browse and select another folder. Click Next to continue."
!define MUI_STARTMENUPAGE_REGISTRY_ROOT HKLM
!define MUI_STARTMENUPAGE_REGISTRY_KEY ${REGKEY}
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME StartMenuGroup
!define MUI_STARTMENUPAGE_DEFAULTFOLDER "${COMPANY}"
!define MUI_FINISHPAGE_TITLE "Setup completed!"
!define MUI_FINISHPAGE_TEXT "The $(^Name) v${VERSION} have been installed and set up on your computer.$\n$\nYou can access the user manual at any time from Start Menu -> Programs -> $StartMenuGroup$\n$\nClick Finish to close."
!define MUI_FINISHPAGE_RUN "cmd.exe"
!define MUI_FINISHPAGE_RUN_PARAMETERS "/K cd /D $INSTDIR\bin"
!define MUI_FINISHPAGE_RUN_TEXT "Launch the tools"
!define MUI_FINISHPAGE_SHOWREADME "${URL}mturk/tutorials"
!define MUI_FINISHPAGE_SHOWREADME_TEXT "View the video tutorials"

# Included files
!include Sections.nsh
!include MUI2.nsh
!include EnterKeys.nsh
!include WriteEnvStr.nsh

# Variables
Var StartMenuGroup

# Interface settings
!define MUI_ABORTWARNING

# Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${BASEDIR}\LICENSE"
#!insertmacro MUI_PAGE_COMPONENTS
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE DirectoryPageLeave
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU Application $StartMenuGroup
!define MUI_PAGE_HEADER_SUBTEXT "Please wait while the $(^Name) are being installed."
!insertmacro MUI_PAGE_INSTFILES
Page custom EnterKeysPage EnterKeysPageLeave
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

# Installer languages
!insertmacro MUI_LANGUAGE English

# Installer attributes
OutFile "${OUTFILE}"
Caption "Setup"
# Don't advertise we're using NSIS
BrandingText " " 
InstallDir "C:\mech-turk-tools-${VERSION}"
CRCCheck on
XPStyle on
ShowInstDetails nevershow
ShowUninstDetails nevershow
VIProductVersion "${VERSION}.0"
VIAddVersionKey ProductName "${SHORT_NAME}"
VIAddVersionKey ProductVersion "${VERSION}"
VIAddVersionKey CompanyName "${COMPANY}"
VIAddVersionKey CompanyWebsite "${URL}"
VIAddVersionKey FileVersion "${VERSION}"
VIAddVersionKey FileDescription ""
VIAddVersionKey LegalCopyright "Copyright 2012 Amazon Technologies, Inc."
InstallDirRegKey HKLM "${REGKEY}" Path

# Installer sections
Section "${SHORT_NAME}" SEC0000
    SectionIn RO
    
    SetOverwrite on
    
    SetOutPath $INSTDIR
    File "${BASEDIR}\LICENSE"
    File "${BASEDIR}\NOTICE"
    File "${BASEDIR}\GetStarted.html"
    File "${BASEDIR}\UserGuide.html"
    File "${BASEDIR}\VersionHistory.html"
    
    SetOutPath "$INSTDIR\etc"
    File /r "${BASEDIR}\etc\*"
    SetOutPath "$INSTDIR\hits"
    File /r "${BASEDIR}\hits\*"
    SetOutPath "$INSTDIR\lib"
    File /r "${BASEDIR}\lib\*"
    SetOutPath "$INSTDIR\qualifications"
    File /r "${BASEDIR}\qualifications\*"
    
    # Don't overwrite mturk.properties if it has been customized    
    SetOverwrite ifnewer
    SetOutPath "$INSTDIR\bin"
    File /r "${BASEDIR}\bin\*"
    
    WriteRegStr HKLM "${REGKEY}\Components" "${SHORT_NAME}" 1
SectionEnd

Section "Samples" SEC0001
    SectionIn RO
    
    SetOverwrite on
    
    SetOutPath "$INSTDIR\samples"
    File /r "${BASEDIR}\samples\*"
    
    WriteRegStr HKLM "${REGKEY}\Components" "Samples" 1
SectionEnd

Section "Source code" SEC0002
    SetOverwrite on
    
    SetOutPath $INSTDIR
    File "${BASEDIR}\build.properties"
    File "${BASEDIR}\build.xml"
    
    SetOutPath "$INSTDIR\src"
    File /r "${BASEDIR}\src\*"
    SetOutPath "$INSTDIR\test"
    File /r "${BASEDIR}\test\*"
    SetOutPath "$INSTDIR\installer"
    File /r "${BASEDIR}\installer\*"
    
    WriteRegStr HKLM "${REGKEY}\Components" "Source code" 1
SectionEnd

!ifdef INCLUDEJRE
    Section "JRE" SEC0003
        SetOverwrite on
        
        SetOutPath "$INSTDIR\jre1.5.0_06"
        File /r "${BASEDIR}\jre1.5.0_06\*"
        
        WriteRegStr HKLM "${REGKEY}\Components" "JRE" 1
    SectionEnd
!endif

Section -post SEC0004
    # Create the uninstaller
    WriteRegStr HKLM "${REGKEY}" Path $INSTDIR
    SetOutPath $INSTDIR
    WriteUninstaller $INSTDIR\uninstall.exe
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayName "$(^Name)"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayVersion "${VERSION}"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" Publisher "${COMPANY}"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" URLInfoAbout "${URL}"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayIcon $INSTDIR\uninstall.exe
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" UninstallString $INSTDIR\uninstall.exe
    WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" NoModify 1
    WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" NoRepair 1

    # Set the MTURK_CMD_HOME environment variable for sub processes
    # This is necessary to set it when launching the CLT from the finish page 
    System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("MTURK_CMD_HOME", "$INSTDIR").r0'
    
    # Permanently set the MTURK_CMD_HOME environment variable for new processes 
    Push "MTURK_CMD_HOME"
    Push $INSTDIR
    Call WriteEnvStr
    
    # Create the Start Menu folder
    !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
        SetOutPath $SMPROGRAMS\$StartMenuGroup
        
        # Launch the CLT.  Temporarily adjust $OUTDIR to get the lnk to "Start in" the bin directory
        Push $OUTDIR
        SetOutPath $INSTDIR\bin
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\${SHORT_NAME}.lnk" cmd.exe
        # Create a similar link on the desktop while we're here
        CreateShortCut "$DESKTOP\$(^Name).lnk" cmd.exe
        Pop $OUTDIR
        
        # Documentation
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\Get Started.lnk" $INSTDIR\GetStarted.html
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\User Manual.lnk" $INSTDIR\UserGuide.html
        
        # Uninstaller
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\Uninstall the ${SHORT_NAME}.lnk" $INSTDIR\uninstall.exe
    !insertmacro MUI_STARTMENU_WRITE_END

    Call CopyOldPropertiesFile

SectionEnd

# Macro for selecting uninstaller sections
!macro SELECT_UNSECTION SECTION_NAME UNSECTION_ID
    Push $R0
    ReadRegStr $R0 HKLM "${REGKEY}\Components" "${SECTION_NAME}"
    StrCmp $R0 1 0 next${UNSECTION_ID}
    !insertmacro SelectSection "${UNSECTION_ID}"
    GoTo done${UNSECTION_ID}
next${UNSECTION_ID}:
    !insertmacro UnselectSection "${UNSECTION_ID}"
done${UNSECTION_ID}:
    Pop $R0
!macroend

# Uninstaller sections
Section /o "-un.${SHORT_NAME}" UNSEC0000
    Delete /REBOOTOK $INSTDIR\LICENSE
    Delete /REBOOTOK $INSTDIR\NOTICE
    Delete /REBOOTOK $INSTDIR\UserGuide.html
    Delete /REBOOTOK $INSTDIR\GetStarted.html
    Delete /REBOOTOK $INSTDIR\VersionHistory.html
    RmDir /r /REBOOTOK $INSTDIR\bin
    RmDir /r /REBOOTOK $INSTDIR\etc
    RmDir /r /REBOOTOK $INSTDIR\hits
    RmDir /r /REBOOTOK $INSTDIR\lib
    RmDir /r /REBOOTOK $INSTDIR\qualifications
    
    DeleteRegValue HKLM "${REGKEY}\Components" "${SHORT_NAME}"
SectionEnd

Section /o "-un.Samples" UNSEC0001
    RmDir /r /REBOOTOK $INSTDIR\samples
    
    DeleteRegValue HKLM "${REGKEY}\Components" "Samples"
SectionEnd

Section /o "-un.Source code" UNSEC0002
    Delete /REBOOTOK $INSTDIR\build.properties
    Delete /REBOOTOK $INSTDIR\build.xml
    RmDir /r /REBOOTOK $INSTDIR\src
    RmDir /r /REBOOTOK $INSTDIR\test
    RmDir /r /REBOOTOK $INSTDIR\installer
    
    DeleteRegValue HKLM "${REGKEY}\Components" "Source code"
SectionEnd

Section /o "-un.JRE" UNSEC0003
    RmDir /r /REBOOTOK $INSTDIR\jre1.5.0_06
    
    DeleteRegValue HKLM "${REGKEY}\Components" "JRE"
SectionEnd

Section -un.post UNSEC0004
    # Remove the MTURK_CMD_HOME environment variable
    Push "MTURK_CMD_HOME"
    Call un.DeleteEnvStr
    
    # Delete the shortcuts
    Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\${SHORT_NAME}.lnk"
    Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\Get Started.lnk"
    Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\User Manual.lnk"
    Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\Uninstall the ${SHORT_NAME}.lnk"
    RmDir /REBOOTOK $SMPROGRAMS\$StartMenuGroup
    Delete /REBOOTOK "$DESKTOP\$(^Name).lnk"
    
    # Cleanup the registry settings
    DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
    DeleteRegValue HKLM "${REGKEY}" StartMenuGroup
    DeleteRegValue HKLM "${REGKEY}" Path
    DeleteRegKey /IfEmpty HKLM "${REGKEY}\Components"
    DeleteRegKey /IfEmpty HKLM "${REGKEY}"
    
    # Remove the remaining files
    Delete /REBOOTOK $INSTDIR\uninstall.exe
    RmDir /REBOOTOK $INSTDIR
SectionEnd

# Installer functions
Function .onInit
    InitPluginsDir
    
FunctionEnd

Function DirectoryPageLeave

    # If the install directory already exists
    ${If} ${FileExists} $INSTDIR
        # Check if the install dir is empty
        Push $INSTDIR
        Call isEmptyDir
        Pop $0
        
        ${If} $0 == 0
          MessageBox MB_YESNO "This directory is not empty.  If you choose to continue all of the files in this directory will be replaced.$\n$\nDo you wish to continue?" IDYES OverwriteDirectory
          Abort
          OverwriteDirectory:    
        ${EndIf}
    ${EndIf}
    
FunctionEnd

# Uninstaller functions
Function un.onInit
    SetAutoClose true
    ReadRegStr $INSTDIR HKLM "${REGKEY}" Path
    !insertmacro MUI_STARTMENU_GETFOLDER Application $StartMenuGroup
    !insertmacro SELECT_UNSECTION "${SHORT_NAME}" ${UNSEC0000}
    !insertmacro SELECT_UNSECTION "Samples" ${UNSEC0001}
    !insertmacro SELECT_UNSECTION "Source code" ${UNSEC0002}
    !insertmacro SELECT_UNSECTION "JRE" ${UNSEC0003}
FunctionEnd
