# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

# Required Plugins:
# ShellLink   http://nsis.sourceforge.net/ShellLink_plug-in

; Set verbosity to 3 (e.g. no script) to lessen the noise in the build logs
!verbose 3

SetDatablockOptimize on
CRCCheck on
SilentInstall silent

RequestExecutionLevel user

!addplugindir ./

; prevents compiling of the reg write logging.
!define NO_LOG

; Variables
Var AppFilename
Var AppName
Var AppRTTempDir

; Variables/macros used by common.nsh
Var TmpVal
!define FileMainEXE           "$AppFilename.exe"

; Other included files may depend upon these includes!
; The following includes are provided by NSIS.
!include "FileFunc.nsh"
!insertmacro GetParameters
!insertmacro un.RefreshShellIcons
!include "LogicLib.nsh"
!include "WinMessages.nsh"
!include "WinVer.nsh"
!include "WordFunc.nsh"

; File properties, version strings, etc
!define CompanyName           "Mozilla Corporation"
!define UninstallerName       "Mozilla Webapp Runtime App Uninstaller"
!define UninstallerFilename   "webapp-uninstaller.exe"
!define AppVersion            "@APP_VERSION@"
VIProductVersion "1.0.0.0"
VIAddVersionKey "ProductName"      "${UninstallerName}"
VIAddVersionKey "CompanyName"      "${CompanyName}"
VIAddVersionKey "LegalCopyright"   "${CompanyName}"
VIAddVersionKey "FileVersion"      "${AppVersion}"
VIAddVersionKey "ProductVersion"   "${AppVersion}"
VIAddVersionKey "FileDescription"  "${UninstallerName}"
VIAddVersionKey "OriginalFilename" "${UninstallerFilename}"

; Mozilla custom include
!include "common.nsh"
!insertmacro un.DeleteShortcuts
!insertmacro un.RegCleanUninstall
!insertmacro un.ParseUninstallLog

Name "Mozilla Web App Runtime App"
OutFile "${UninstallerFilename}"
ShowUnInstDetails nevershow

# Create a blank page so that the default pages (instfiles) don't appear
UninstPage custom un.blankPage

################################################################################
# Install Sections
# The "installer" that is generated by this file is a stub that generates the
# uninstaller at runtime in a temp directory and launches it.
# We call `WriteUninstaller` during `onInit` so this section is empty.
Section ""
SectionEnd

################################################################################
# This is where uninstallation happens
################################################################################
Function un.webappUninstall
  ; Delete the app exe to prevent launching the app while we are uninstalling.
  ClearErrors
  ${DeleteFile} "$INSTDIR\${FileMainEXE}"
  ${If} ${Errors}
    ; If the app is running, rename the EXE out of the way
    CreateDirectory "$AppRTTempDir"
    Rename "$INSTDIR\${FileMainEXE}" "$AppRTTempDir\${FileMainEXE}"
    ClearErrors
  ${EndIf}

  SetShellVarContext current  ; Set SHCTX to HKCU

  ; Remove our entry in the "Uninstall" key
  ${un.RegCleanUninstall}

  ; Remove our shortcuts from start menu, desktop, and taskbar
  ${un.DeleteShortcuts}

  ; Parse the uninstall log to remove all installed
  ; files / directories this install is responsible for.
  ${un.ParseUninstallLog}

  ; Remove the uninstall directory that we control.
  ; The installer is in the uninstall directory, it generates
  ; the uninstaller in a temp directory and waits for its
  ; execution. Thus, we can't remove the uninstall directory
  ; now and need to wait for a restart.
  ; See bug 994965.
  RmDir /r /REBOOTOK "$INSTDIR\uninstall"

  ; Remove the installation directory if it is empty
  ${RemoveDir} "$INSTDIR"

  ; Refresh shell icons to reflect the changes we've made
  ${un.RefreshShellIcons}
FunctionEnd

Function un.blankPage
  MessageBox MB_OKCANCEL "$(UN_CONFIRM_UNINSTALL)" /SD IDOK IDCANCEL done

  Call un.webappUninstall

  done:
FunctionEnd

################################################################################
# Language

!verbose push
!verbose 3
!include "webapp-uninstaller-locale.nsh"
!verbose pop

; Set this after the locale files to override it if it is in the locale. Using
; " " for BrandingText will hide the "Nullsoft Install System..." branding.
BrandingText " "

# Initialization Functions
Function .onInit
  ; Remove the current exe directory from the search order.
  ; This only effects LoadLibrary calls and not implicitly loaded DLLs.
  System::Call 'kernel32::SetDllDirectoryW(w "")'

  GetTempFileName $0
  Delete "$0"
  CreateDirectory "$0"
  SetOutPath "$0"

  StrCpy $1 "$0\${UninstallerFilename}"
  WriteUninstaller "$1"

  ${GetParameters} $2
  StrCpy $2 "$2 _?=$EXEDIR"
  ExecWait '"$1" $2'
  Quit
FunctionEnd

Function un.onInit
  ; Remove the current exe directory from the search order.
  ; This only effects LoadLibrary calls and not implicitly loaded DLLs.
  System::Call 'kernel32::SetDllDirectoryW(w "")'

  StrCpy $LANGUAGE 0

  ${un.GetParent} "$INSTDIR" $INSTDIR
  ${un.GetLongPath} "$INSTDIR" $INSTDIR

  ReadINIStr $AppFilename "$INSTDIR\webapp.ini" "Webapp" "Executable"
  ReadINIStr $AppName "$INSTDIR\webapp.ini" "Webapp" "Name"

  ${Unless} ${FileExists} "$INSTDIR\${FileMainEXE}"
    Abort
  ${EndUnless}

  StrCpy $AppRTTempDir "$TEMP\moz_webapprt"
  RmDir /r "$AppRTTempDir"

  ${If} ${Silent}
    Call un.webappUninstall
  ${EndIf}
FunctionEnd
