<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
Unicode true
ManifestDPIAware true
!define APPNAME "<#= AppName #>"
!define EXECUTABLE "<#= Executable #>"
!define COMPANYNAME "<#= CompanyName #>"
!define DESCRIPTION "<#= Description #>"
!define ASSEMBLYNAME "<#= AssemblyName #>"
!define VERSION <#= Version.ToString() #>
!define HELPURL <#= HelpUrl #>
!define INSTALLSIZE <#= InstallSize #>
; exampleCmd: makensis.exe /DVERSION=1.0.0.5 /DNAME=WpfApp1 Setup.nsi

Name "${APPNAME}"
OutFile "<#= OutFile #>"
InstallDir "$LOCALAPPDATA\Programs\${APPNAME}"
RequestExecutionLevel user
; SetCompressor /SOLID lzma
SilentUnInstall silent
; Subcaption 3 " "
XPStyle on
AutoCloseWindow true
ChangeUI all "<#= UserInterface #>"
Icon "<#= IconPath #>"
UninstallIcon "${NSISDIR}\Contrib\Graphics\Icons\nsis3-uninstall.ico"
ShowInstDetails nevershow 
ShowUninstDetails nevershow 
BrandingText "${COMPANYNAME}"

VIProductVersion <#= LegacyVersion.ToString(4) #>
VIAddVersionKey ProductName "${APPNAME}"
VIAddVersionKey Comments ""
VIAddVersionKey CompanyName "${COMPANYNAME}"
VIAddVersionKey LegalCopyright "${COMPANYNAME}"
VIAddVersionKey FileDescription "${DESCRIPTION}"
VIAddVersionKey FileVersion "${VERSION}"
VIAddVersionKey ProductVersion "${VERSION}"
VIAddVersionKey OriginalFilename "<#= OutFile #>"

;--------------------------------
; The stuff to install
Section "install" ;No components page, name is not important
    ;Wait for running processes in the installation directory.
    HideWindow
    InitPluginsDir
    SetShellVarContext current
    
    File "/oname=$PLUGINSDIR\wait-for-process.exe" "<#= WaitForProcessExe #>"
    File "/oname=$PLUGINSDIR\app.package" "<#= AppPackage #>"
    DetailPrint 'Waiting for closing ${APPNAME}. Please wait...'
    nsExec::ExecToStack '"$PLUGINSDIR\wait-for-process.exe" "$INSTDIR\${EXECUTABLE}"'
    BringToFront

    DetailPrint 'Installing ${APPNAME}. Please wait...'
    SetDetailsPrint None
    RMDir /r $INSTDIR
    SetOutPath $INSTDIR
    Nsis7z::Extract "$PLUGINSDIR\app.package"
    ; Delete "$INSTDIR\app.package"
    File "/oname=$INSTDIR\Katteker.config" "<#= KattekerConfig #>"
    WriteUninstaller "$INSTDIR\uninstall.exe"
    ; Desktop
    CreateShortCut "$DESKTOP\${APPNAME}.lnk" "$INSTDIR\${EXECUTABLE}"
    
    # Start Menu
    CreateDirectory "$SMPROGRAMS\${COMPANYNAME}"
    CreateShortCut "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk" "$INSTDIR\${EXECUTABLE}"

    # Update pinned Taskbar
    IfFileExists "$APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\${APPNAME}.lnk" 0 +2
    CreateShortCut "$APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\${APPNAME}.lnk" "$INSTDIR\${EXECUTABLE}"
    StrCpy $0 ${EXECUTABLE} -4
    IfFileExists "$APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\$0.lnk" 0 +2
    CreateShortCut "$APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\$0.lnk" "$INSTDIR\app-${VERSION}\${EXECUTABLE}"
    
    # Registry information for add/remove programs
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayName" "${APPNAME}"
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S"
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "InstallLocation" "$\"$INSTDIR$\""
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayIcon" "$\"$INSTDIR\app-${VERSION}\${EXECUTABLE}$\""
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "Publisher" "${COMPANYNAME}"
    ; WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "HelpLink" "${HELPURL}"
    WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayVersion" "${VERSION}"
    # There is no option for modifying or repairing the install
    WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "NoModify" 1
    WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "NoRepair" 1
    # Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size
    WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "EstimatedSize" ${INSTALLSIZE}
SectionEnd ; end the section

Function .onInstSuccess
    <#= OnInstSuccessContent #>
    IfSilent +2
    Exec '"$OUTDIR\${EXECUTABLE}"'
FunctionEnd

Section "uninstall"
    DetailPrint 'Please wait...'
    SetShellVarContext current
    SetDetailsPrint None
    SetAutoClose true
    
    # Remove Start Menu launcher
    Delete "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk"
    Delete "$DESKTOP\${APPNAME}.lnk"
    StrCpy $0 ${EXECUTABLE} -4
    Delete "$APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\${APPNAME}.lnk"
    Delete "$APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\$0.lnk"
    # Try to remove the Start Menu folder - this will only happen if it is empty
    RMDir "$SMPROGRAMS\${COMPANYNAME}"

    # Always delete uninstaller as the last action
    delete $INSTDIR\uninstall.exe

    # Try to remove the install directory - this will only happen if it is empty
    RMDir /r /REBOOTOK $INSTDIR

    # Remove uninstaller information from the registry
    DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
sectionEnd

Function un.onUninstSuccess
    <#= OnUnInstSuccessContent #>
FunctionEnd