1!include FileFunc.nsh
2!include UAC.nsh
3
4!define FOLDERID_UserProgramFiles {5CD7AEE2-2219-4A67-B85D-6C9CE15660CB}
5!define KF_FLAG_CREATE 0x00008000
6
7# allow user to define own custom
8!define /ifndef INSTALL_REGISTRY_KEY "Software\${APP_GUID}"
9!define /ifndef UNINSTALL_REGISTRY_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${UNINSTALL_APP_KEY}"
10
11# current Install Mode ("all" or "CurrentUser")
12Var installMode
13
14!ifndef INSTALL_MODE_PER_ALL_USERS
15  !ifndef ONE_CLICK
16    Var hasPerUserInstallation
17    Var hasPerMachineInstallation
18  !endif
19  Var PerUserInstallationFolder
20
21  !macro setInstallModePerUser
22    StrCpy $installMode CurrentUser
23    SetShellVarContext current
24
25    # сhecks registry for previous installation path
26    ReadRegStr $perUserInstallationFolder HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation
27    ${if} $perUserInstallationFolder != ""
28      StrCpy $INSTDIR $perUserInstallationFolder
29    ${else}
30      StrCpy $0 "$LocalAppData\Programs"
31      System::Store S
32      # Win7 has a per-user programfiles known folder and this can be a non-default location
33      System::Call 'SHELL32::SHGetKnownFolderPath(g "${FOLDERID_UserProgramFiles}", i ${KF_FLAG_CREATE}, p 0, *p .r2)i.r1'
34      ${If} $1 == 0
35        System::Call '*$2(&w${NSIS_MAX_STRLEN} .s)'
36        StrCpy $0 $1
37        System::Call 'OLE32::CoTaskMemFree(p r2)'
38      ${endif}
39      System::Store L
40      StrCpy $INSTDIR "$0\${APP_FILENAME}"
41    ${endif}
42
43    # allow /D switch to override installation path https://github.com/electron-userland/electron-builder/issues/1551
44    ${StdUtils.GetParameter} $R0 "D" ""
45    ${If} $R0 != ""
46      StrCpy $INSTDIR $R0
47    ${endif}
48
49  !macroend
50!endif
51
52!ifdef INSTALL_MODE_PER_ALL_USERS_REQUIRED
53  Var perMachineInstallationFolder
54
55  !macro setInstallModePerAllUsers
56    StrCpy $installMode all
57    SetShellVarContext all
58
59    !ifdef BUILD_UNINSTALLER
60      ${IfNot} ${UAC_IsAdmin}
61        ShowWindow $HWNDPARENT ${SW_HIDE}
62        !insertmacro UAC_RunElevated
63        Quit
64      ${endif}
65    !endif
66
67    # сheck registry for previous installation path
68    ReadRegStr $perMachineInstallationFolder HKLM "${INSTALL_REGISTRY_KEY}" InstallLocation
69    ${if} $perMachineInstallationFolder != ""
70      StrCpy $INSTDIR $perMachineInstallationFolder
71    ${else}
72      StrCpy $0 "$PROGRAMFILES"
73      !ifdef APP_64
74        ${if} ${RunningX64}
75          StrCpy $0 "$PROGRAMFILES64"
76        ${endif}
77      !endif
78
79      !ifdef MENU_FILENAME
80        StrCpy $0 "$0\${MENU_FILENAME}"
81      !endif
82
83      StrCpy $INSTDIR "$0\${APP_FILENAME}"
84    ${endif}
85
86    # allow /D switch to override installation path https://github.com/electron-userland/electron-builder/issues/1551
87    ${StdUtils.GetParameter} $R0 "D" ""
88    ${If} $R0 != ""
89      StrCpy $INSTDIR $R0
90    ${endif}
91
92  !macroend
93!endif
94