NSIS.template.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. ; CPack install script designed for a nmake build
  2. ;--------------------------------
  3. ; You must define these values
  4. !define VERSION "@CPACK_PACKAGE_VERSION@"
  5. !define PATCH "@CPACK_PACKAGE_VERSION_PATCH@"
  6. !define INST_DIR "@CPACK_TEMPORARY_DIRECTORY@"
  7. ;--------------------------------
  8. ;Variables
  9. Var MUI_TEMP
  10. Var STARTMENU_FOLDER
  11. ;--------------------------------
  12. ;Include Modern UI
  13. !include "MUI.nsh"
  14. ;Default installation folder
  15. InstallDir "$PROGRAMFILES\@CPACK_PACKAGE_NAME@ ${VERSION}"
  16. ;--------------------------------
  17. ; determine admin versus local install
  18. Function .onInit
  19. ClearErrors
  20. UserInfo::GetName
  21. IfErrors noLM
  22. Pop $0
  23. UserInfo::GetAccountType
  24. Pop $1
  25. StrCmp $1 "Admin" 0 +3
  26. SetShellVarContext all
  27. ;MessageBox MB_OK 'User "$0" is in the Admin group'
  28. Goto done
  29. StrCmp $1 "Power" 0 +3
  30. SetShellVarContext all
  31. ;MessageBox MB_OK 'User "$0" is in the Power Users group'
  32. Goto done
  33. noLM:
  34. ;Get installation folder from registry if available
  35. done:
  36. FunctionEnd
  37. ;--------------------------------
  38. ;General
  39. ;Name and file
  40. Name "@CPACK_PACKAGE_NAME@ ${VERSION}"
  41. OutFile "@CPACK_TOPLEVEL_DIRECTORY@/@CPACK_OUTPUT_FILE_NAME@"
  42. ;--------------------------------
  43. ;Interface Settings
  44. !define MUI_HEADERIMAGE
  45. !define MUI_ABORTWARNING
  46. ;--------------------------------
  47. ; path functions
  48. !verbose 3
  49. !include "WinMessages.NSH"
  50. !verbose 4
  51. ; AddToPath - Adds the given dir to the search path.
  52. ; Input - head of the stack
  53. ; Note - Win9x systems requires reboot
  54. Function AddToPath
  55. Exch $0
  56. Push $1
  57. Push $2
  58. Push $3
  59. # don't add if the path doesn't exist
  60. IfFileExists "$0\*.*" "" AddToPath_done
  61. ReadEnvStr $1 PATH
  62. Push "$1;"
  63. Push "$0;"
  64. Call StrStr
  65. Pop $2
  66. StrCmp $2 "" "" AddToPath_done
  67. Push "$1;"
  68. Push "$0\;"
  69. Call StrStr
  70. Pop $2
  71. StrCmp $2 "" "" AddToPath_done
  72. GetFullPathName /SHORT $3 $0
  73. Push "$1;"
  74. Push "$3;"
  75. Call StrStr
  76. Pop $2
  77. StrCmp $2 "" "" AddToPath_done
  78. Push "$1;"
  79. Push "$3\;"
  80. Call StrStr
  81. Pop $2
  82. StrCmp $2 "" "" AddToPath_done
  83. Call IsNT
  84. Pop $1
  85. StrCmp $1 1 AddToPath_NT
  86. ; Not on NT
  87. StrCpy $1 $WINDIR 2
  88. FileOpen $1 "$1\autoexec.bat" a
  89. FileSeek $1 -1 END
  90. FileReadByte $1 $2
  91. IntCmp $2 26 0 +2 +2 # DOS EOF
  92. FileSeek $1 -1 END # write over EOF
  93. FileWrite $1 "$\r$\nSET PATH=%PATH%;$3$\r$\n"
  94. FileClose $1
  95. SetRebootFlag true
  96. Goto AddToPath_done
  97. AddToPath_NT:
  98. ReadRegStr $1 SHCTX "Environment" "PATH"
  99. StrCpy $2 $1 1 -1 # copy last char
  100. StrCmp $2 ";" 0 +2 # if last char == ;
  101. StrCpy $1 $1 -1 # remove last char
  102. StrCmp $1 "" AddToPath_NTdoIt
  103. StrCpy $0 "$1;$0"
  104. AddToPath_NTdoIt:
  105. WriteRegExpandStr SHCTX "Environment" "PATH" $0
  106. SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
  107. AddToPath_done:
  108. Pop $3
  109. Pop $2
  110. Pop $1
  111. Pop $0
  112. FunctionEnd
  113. ; RemoveFromPath - Remove a given dir from the path
  114. ; Input: head of the stack
  115. Function un.RemoveFromPath
  116. Exch $0
  117. Push $1
  118. Push $2
  119. Push $3
  120. Push $4
  121. Push $5
  122. Push $6
  123. IntFmt $6 "%c" 26 # DOS EOF
  124. Call un.IsNT
  125. Pop $1
  126. StrCmp $1 1 unRemoveFromPath_NT
  127. ; Not on NT
  128. StrCpy $1 $WINDIR 2
  129. FileOpen $1 "$1\autoexec.bat" r
  130. GetTempFileName $4
  131. FileOpen $2 $4 w
  132. GetFullPathName /SHORT $0 $0
  133. StrCpy $0 "SET PATH=%PATH%;$0"
  134. Goto unRemoveFromPath_dosLoop
  135. unRemoveFromPath_dosLoop:
  136. FileRead $1 $3
  137. StrCpy $5 $3 1 -1 # read last char
  138. StrCmp $5 $6 0 +2 # if DOS EOF
  139. StrCpy $3 $3 -1 # remove DOS EOF so we can compare
  140. StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
  141. StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
  142. StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
  143. StrCmp $3 "" unRemoveFromPath_dosLoopEnd
  144. FileWrite $2 $3
  145. Goto unRemoveFromPath_dosLoop
  146. unRemoveFromPath_dosLoopRemoveLine:
  147. SetRebootFlag true
  148. Goto unRemoveFromPath_dosLoop
  149. unRemoveFromPath_dosLoopEnd:
  150. FileClose $2
  151. FileClose $1
  152. StrCpy $1 $WINDIR 2
  153. Delete "$1\autoexec.bat"
  154. CopyFiles /SILENT $4 "$1\autoexec.bat"
  155. Delete $4
  156. Goto unRemoveFromPath_done
  157. unRemoveFromPath_NT:
  158. ReadRegStr $1 SHCTX "Environment" "PATH"
  159. StrCpy $5 $1 1 -1 # copy last char
  160. StrCmp $5 ";" +2 # if last char != ;
  161. StrCpy $1 "$1;" # append ;
  162. Push $1
  163. Push "$0;"
  164. Call un.StrStr ; Find `$0;` in $1
  165. Pop $2 ; pos of our dir
  166. StrCmp $2 "" unRemoveFromPath_done
  167. ; else, it is in path
  168. # $0 - path to add
  169. # $1 - path var
  170. StrLen $3 "$0;"
  171. StrLen $4 $2
  172. StrCpy $5 $1 -$4 # $5 is now the part before the path to remove
  173. StrCpy $6 $2 "" $3 # $6 is now the part after the path to remove
  174. StrCpy $3 $5$6
  175. StrCpy $5 $3 1 -1 # copy last char
  176. StrCmp $5 ";" 0 +2 # if last char == ;
  177. StrCpy $3 $3 -1 # remove last char
  178. WriteRegExpandStr SHCTX "Environment" "PATH" $3
  179. SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
  180. unRemoveFromPath_done:
  181. Pop $6
  182. Pop $5
  183. Pop $4
  184. Pop $3
  185. Pop $2
  186. Pop $1
  187. Pop $0
  188. FunctionEnd
  189. ###########################################
  190. # Utility Functions #
  191. ###########################################
  192. ; IsNT
  193. ; no input
  194. ; output, top of the stack = 1 if NT or 0 if not
  195. ;
  196. ; Usage:
  197. ; Call IsNT
  198. ; Pop $R0
  199. ; ($R0 at this point is 1 or 0)
  200. !macro IsNT un
  201. Function ${un}IsNT
  202. Push $0
  203. ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
  204. StrCmp $0 "" 0 IsNT_yes
  205. ; we are not NT.
  206. Pop $0
  207. Push 0
  208. Return
  209. IsNT_yes:
  210. ; NT!!!
  211. Pop $0
  212. Push 1
  213. FunctionEnd
  214. !macroend
  215. !insertmacro IsNT ""
  216. !insertmacro IsNT "un."
  217. ; StrStr
  218. ; input, top of stack = string to search for
  219. ; top of stack-1 = string to search in
  220. ; output, top of stack (replaces with the portion of the string remaining)
  221. ; modifies no other variables.
  222. ;
  223. ; Usage:
  224. ; Push "this is a long ass string"
  225. ; Push "ass"
  226. ; Call StrStr
  227. ; Pop $R0
  228. ; ($R0 at this point is "ass string")
  229. !macro StrStr un
  230. Function ${un}StrStr
  231. Exch $R1 ; st=haystack,old$R1, $R1=needle
  232. Exch ; st=old$R1,haystack
  233. Exch $R2 ; st=old$R1,old$R2, $R2=haystack
  234. Push $R3
  235. Push $R4
  236. Push $R5
  237. StrLen $R3 $R1
  238. StrCpy $R4 0
  239. ; $R1=needle
  240. ; $R2=haystack
  241. ; $R3=len(needle)
  242. ; $R4=cnt
  243. ; $R5=tmp
  244. loop:
  245. StrCpy $R5 $R2 $R3 $R4
  246. StrCmp $R5 $R1 done
  247. StrCmp $R5 "" done
  248. IntOp $R4 $R4 + 1
  249. Goto loop
  250. done:
  251. StrCpy $R1 $R2 "" $R4
  252. Pop $R5
  253. Pop $R4
  254. Pop $R3
  255. Pop $R2
  256. Exch $R1
  257. FunctionEnd
  258. !macroend
  259. !insertmacro StrStr ""
  260. !insertmacro StrStr "un."
  261. ;--------------------------------
  262. ; Define some macro setting for the gui
  263. !define MUI_HEADERIMAGE_BITMAP "@CPACK_PACKAGE_ICON@"
  264. ;--------------------------------
  265. ;Pages
  266. !insertmacro MUI_PAGE_DIRECTORY
  267. ;Start Menu Folder Page Configuration
  268. !define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX"
  269. !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}"
  270. !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
  271. !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  272. !insertmacro MUI_PAGE_INSTFILES
  273. !insertmacro MUI_UNPAGE_CONFIRM
  274. !insertmacro MUI_UNPAGE_INSTFILES
  275. ;--------------------------------
  276. ;Languages
  277. !insertmacro MUI_LANGUAGE "English"
  278. ;--------------------------------
  279. ;Installer Sections
  280. Section "Add to path"
  281. Push $INSTDIR\bin
  282. Call AddToPath
  283. SectionEnd
  284. Section
  285. SectionEnd
  286. Section "Dummy Section" SecDummy
  287. ;Use the entire tree produced by the INSTALL target. Keep the
  288. ;list of directories here in sync with the RMDir commands below.
  289. SetOutPath "$INSTDIR"
  290. File /r "${INST_DIR}\bin"
  291. File /r "${INST_DIR}\doc"
  292. File /r "${INST_DIR}\share"
  293. @CPACK_NSIS_EXTRA_COMMANDS@
  294. ;Store installation folder
  295. WriteRegStr SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}" "" $INSTDIR
  296. ;Create uninstaller
  297. WriteUninstaller "$INSTDIR\Uninstall.exe"
  298. !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  299. ;Create shortcuts
  300. CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
  301. CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\@[email protected]" "$INSTDIR\bin\@[email protected]"
  302. CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
  303. !insertmacro MUI_STARTMENU_WRITE_END
  304. SectionEnd
  305. ;--------------------------------
  306. ; determine admin versus local install
  307. Function un.onInit
  308. ClearErrors
  309. UserInfo::GetName
  310. IfErrors noLM
  311. Pop $0
  312. UserInfo::GetAccountType
  313. Pop $1
  314. StrCmp $1 "Admin" 0 +3
  315. SetShellVarContext all
  316. ;MessageBox MB_OK 'User "$0" is in the Admin group'
  317. Goto done
  318. StrCmp $1 "Power" 0 +3
  319. SetShellVarContext all
  320. ;MessageBox MB_OK 'User "$0" is in the Power Users group'
  321. Goto done
  322. noLM:
  323. ;Get installation folder from registry if available
  324. done:
  325. FunctionEnd
  326. ;--------------------------------
  327. ;Uninstaller Section
  328. Section "Uninstall"
  329. ;Remove directories we installed.
  330. ;Keep the list of directories here in sync with the File commands above.
  331. RMDir /r "$INSTDIR\bin"
  332. RMDir /r "$INSTDIR\doc"
  333. RMDir /r "$INSTDIR\share"
  334. ;Remove the uninstaller itself.
  335. Delete "$INSTDIR\Uninstall.exe"
  336. ;Remove the installation directory if it is empty.
  337. RMDir "$INSTDIR"
  338. ; Remove the registry entries.
  339. DeleteRegKey SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}"
  340. !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
  341. Delete "$SMPROGRAMS\$MUI_TEMP\Uninstall.lnk"
  342. Delete "$SMPROGRAMS\$MUI_TEMP\@[email protected]"
  343. ;Delete empty start menu parent diretories
  344. StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
  345. startMenuDeleteLoop:
  346. ClearErrors
  347. RMDir $MUI_TEMP
  348. GetFullPathName $MUI_TEMP "$MUI_TEMP\.."
  349. IfErrors startMenuDeleteLoopDone
  350. StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
  351. startMenuDeleteLoopDone:
  352. DeleteRegKey /ifempty SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_NAME@ ${VERSION}"
  353. Push $INSTDIR\bin
  354. Call un.RemoveFromPath
  355. SectionEnd