NSIS一键安装网站环境及部署网站

最近研究通过NISI脚本安装一键部署网站,在这做一个学习札记~

;项目: 智慧校园云平台
;时间: 2014年7月16日
;作者: Kan Lau
; 该脚本使用 HM VNISEdit 脚本编辑器向导产生


; 安装程序初始定义常量
!define PRODUCT_NAME "金太阳智慧校园云平台教育软件"
!define PRODUCT_EN_NAME "金太阳智慧校园云平台教育软件"
!define PRODUCT_PROC_NAME "KSWebInstraller.exe"
!define PRODUCT_VERSION "1.1"
!define PRODUCT_PUBLISHER "深圳市方直科技股份有限公司"
!define PRODUCT_WEB_SITE "http://www.kingsunsoft.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
###############################
#打包时,需要修改此处版本号!
###############################
#安装界面显示版本号,必须为X.X的格式(否则版本号比较会失败)
#安装程序信息描述版本号 必须为 X.X.X.X
!define _PRODUCTVER "1.1.0.0"
!define _PRODUCTDES "金太阳智慧校园云平台教育软件"


!define MUI_WELCOMEFINISHPAGE_BITMAP "res\left.bmp" ; 左侧标题位图


!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_RIGHT ; 将页眉图片显示到右边,默认为左边
!define MUI_HEADERIMAGE_BITMAP "res\header-r.bmp" ; 设置标题图片,BMP位图格式.大小为 150 x 57
SetCompressor lzma


; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"
!include "WinVer.nsh"
!include "UAC.nsh"
; ------ 添加判断操作系统的头函数 ------
!include "x64.nsh"


; ------ 添加字符串处理的头函数 ------
!include WordFunc.nsh
!include "LogicLib.nsh"


; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON "res\inst.ico"
!define MUI_UNICON "res\uninst.ico"


; 欢迎页面
!insertmacro MUI_PAGE_WELCOME
; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 许可协议页面
!insertmacro MUI_PAGE_LICENSE "F:\Work file\2014\智慧校园安装包\Nsis\license.rtf"


; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 定义安装界面
!define MUI_FINISHPAGE_RUN
; 安装完成页面
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
;!insertmacro MUI_PAGE_INSTFILES
;!define MUI_PAGE_CUSTOMFUNCTION_PRE LaunchLink
!insertmacro MUI_PAGE_FINISH


; 安装卸载过程页面
!insertmacro MUI_UNPAGE_INSTFILES


; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "SimpChinese"


VIProductVersion "${_PRODUCTVER}"
VIAddVersionKey /LANG=2052 "ProductName" "${_PRODUCTDES}"
VIAddVersionKey /LANG=2052 "CompanyName" "深圳市方直科技股份有限公司"
VIAddVersionKey /LANG=2052 "FileDescription" "${_PRODUCTDES}"
VIAddVersionKey /LANG=2052 "FileVersion" "${_VERSION}"
VIAddVersionKey /LANG=2052 "LegalCopyright" "Copyright (C) Kingsunsoft. 保留所有权利。"
; 安装预释放文件
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
; ------ MUI 现代界面定义结束 ------


Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "KSWebInstraller.exe"
InstallDir "$PROGRAMFILES\方直金太阳\金太阳智慧校园平台"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "方直金太阳软件"


Var local_ip          ;运行安装程序的电脑IP
Var ports_firstports  ;循环判断空闲端口的第一个端口号
Var ports_value       ;端口值
Var free_ports        ;空闲端口
Var version_number    ;版本号
Var XML_PATH          ;配置文件路径
Var Index             ;生成网站的网站ID
Var path              ;安装程序所在的路径,用于设置安装完IIS后,写入注册表,达到重启一次能自动运行安装程序的作用
Var update_path       ;安装升级服务所在的路径
Var empty
;----------------------------------------------------请求UAC权限---------------------------------
!macro Init thing
uac_tryagain:
!insertmacro UAC_RunElevated
${Switch} $0
${Case} 0
${IfThen} $1 = 1 ${|} Quit ${|} ;we are the outer process, the inner process has done its work, we are done
${IfThen} $3 <> 0 ${|} ${Break} ${|} ;we are admin, let the show go on
${If} $1 = 3 ;RunAs completed successfully, but with a non-admin user
;MessageBox mb_YesNo|mb_IconExclamation|mb_TopMost|mb_SetForeground "This ${thing} requires admin privileges, try again" /SD IDNO IDYES uac_tryagain IDNO 0
${EndIf}
;fall-through and die
${Case} 1223
;MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "This ${thing} requires admin privileges, aborting!"
Quit
${Case} 1062
;MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "Logon service not running, aborting!"
;Quit
${Default}
;MessageBox mb_IconStop|mb_TopMost|mb_SetForeground "Unable to elevate, error $0"
;Quit
${EndSwitch}


SetShellVarContext current
!macroend
;-----------------------------------------------------------------------------------------------


Function .onInit
 StrCpy $version_number "1.1"
 System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${PRODUCT_NAME} ${PRODUCT_VERSION}") i .r1 ?e' ;判断是否已启动安装程序
 Pop $R0
 StrCmp $R0 0 +3
   MessageBox MB_OK|MB_ICONEXCLAMATION "安装程序已经在运行。"
   Abort
   
 ${If} ${IsWinXP}
 ${OrIf} ${IsWinVista}
 MessageBox MB_OK "暂不支持XP或Vista系统!"
 Quit
 ${EndIf}


 ClearErrors
 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\InetStp" "MajorVersion"
 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\InetStp" "MinorVersion"
 IfErrors 0 NextFunction
  ${If} ${RunningX64}
  DetailPrint "暂不支持64位下的IIS安装,请安装完IIS后再运行此安装包"
Quit
 ${EndIf}


   NextFunction:
 
;获取安装程序所在路径
  System::Call 'kernel32::GetCurrentDirectory(i 1024,t .R8)'
  strCpy $path $R8
  ReadRegDWORD $0 HKLM "SOFTWARE\WisdomCampus" "KSWebVersion"
  StrCpy $empty ""
  StrCmp $0 $empty notFound
  ${If} $0 < "$version_number"
  MessageBox MB_OK|MB_ICONEXCLAMATION "检测到当前系统已安装较低版本网站,请先执行Uninstall卸载后再执行此安装"
  Quit
  ${Else}
  MessageBox MB_OK|MB_ICONEXCLAMATION "检测到当前系统已安装相同版本或更高版本的网站"
  Quit
${EndIf}
notFound:
FunctionEnd


Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
;获取安装程序所在路径
 ; File /r "F:\Work file\2014\Nsis\TestWeb\*.*" ;添加网站
 ; File /r "F:\Work file\2014\智慧校园安装包\WebSite\*.*"
 ; File "F:\Work file\2014\Nsis\dotnetfx4.0.30319.exe" ;添加.NET框架
 ; File "F:\Work file\2014\Nsis\dotnetfx35_3.5.30729.1.exe"
 ; File "F:\Work file\2014\Nsis\C++RunDll\vcredist_2010_x64.exe"      ;添加C++运行库
 ; File "F:\Work file\2014\Nsis\C++RunDll\vcredist_2010_x86.exe"      ;添加C++运行库
 ; File "F:\Work file\2014\Nsis\SysData.xml"
  File /r "F:\Work file\2014\智慧校园安装包\WisdomCampus\*.*"


  Push $R0
  IfFileExists C:\Windows\System32\PkgMgr.exe NextStep
  ClearErrors
  ${If} ${RunningX64}
  CopyFiles $INSTDIR\Tools\64bit\PkgMgr.exe C:\Windows\System32                        ;运行IIS安装需要的EXE
  ${Else}
  CopyFiles $INSTDIR\Tools\32bit\PkgMgr.exe C:\Windows\System32
  ${EndIf}
  
  NextStep:
;检测IIS
  ClearErrors
  ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\InetStp" "MajorVersion"
  ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\InetStp" "MinorVersion"
  IfErrors 0 ExitFunction
  ;没有安装iis
  MessageBox MB_OK  "您当前安装的Microsoft Internet信息服务器版 v$0.$1 版本低于要求版本 v6.0,现在将执行IIS安装,请稍等几分钟。"


ClearErrors
  ${If} ${IsWin2003}
  ${OrIf} ${IsWin2003R2}
  ${OrIf} ${IsWinXP}
  ${OrIf} ${IsWin2000}
  DetailPrint "请在安装程序里执行下一步进行安装"
  ExecWait "$INSTDIR\Tools\IIS\setup.exe /q"
;对IIS框架进行检验是否安装完成,如若没有,回调IIS安装
${ElseIf} ${IsWin2008}
DetailPrint "正在安装IIS,请稍等几分钟"
nsExec::Exec '"$INSTDIR\Tools\iis7x_setup.bat" /S'
${Else}
${If} ${RunningX64}
  DetailPrint "暂不支持64位下的IIS安装,请安装完IIS后再运行此安装包"
Quit
   ${Else}
DetailPrint "正在安装IIS,请稍等几分钟"
   ExecWait "$INSTDIR\Tools\iis7x_setup.bat" ;WIN7 32下不支持NSEXEC插件
   ${EndIf}
  ${EndIf}
  
  IntCmp $0 6 ExitFunction NextStep ExitFunction
  Goto NextStep


 
  ExitFunction:
  
  ;Call in.CheckProcess
;检测C++运行库
  Push $R0
  ClearErrors
  ${If} ${RunningX64}
  ReadRegDword $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1D8E6291-B0D5-35EC-8441-6616F567A0F7}" "Version"   ; 检测含有vc的注册表信息是否存在
  IfErrors 0 VSRedistInstalled
 ; MessageBox MB_OK  "未安装64位C++运行库"
  Exec "$INSTDIR\Tools\vcredist_2010_x64.exe /q"
  Delete "$INSTDIR\Tools\vcredist_2010_x64.exe"
  ${Else}
  ReadRegDword $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{196BB40D-1578-3D01-B289-BEFC77A11A1E}" "Version"   ; 检测含有vc的注册表信息是否存在
  IfErrors 0 VSRedistInstalled
 ; MessageBox MB_OK  "未安装86位C++运行库"
  Exec "$INSTDIR\Tools\vcredist_2010_x86.exe /q"
  Delete "$INSTDIR\Tools\vcredist_2010_x86.exe"
${EndIf}




  VSRedistInstalled:


;检测是否有.net framework框架
  Call GetNetFrameworkVersion


  Pop $R1
  DetailPrint $R1


 ; ${If} $R1 < '3.5.30729'


 ; SetDetailsPrint textonly


 ; DetailPrint "正在安装 .NET Framework 3.5"


 ; SetDetailsPrint listonly


 ; SetOutPath "$TEMP"


 ; SetOverwrite on


 ; File "dotnetfx35_3.5.30729.1.exe"


 ; ExecWait '$TEMP\dotnetfx35_3.5.30729.1.exe /q /norestart /ChainingPackage' $R1
 ; Delete "$TEMP\dotnetfx35_3.5.30729.1.exe"
 
 
  ${If} $R1 < '4.0.30319'
    ${If} ${IsWin2003}
    ${OrIf} ${IsWin2003R2}
    ${OrIf} ${IsWinXP}
    ${OrIf} ${IsWin2000}
      ClearErrors
      ${If} ${RunningX64}
      ExecWait "$INSTDIR\Tools\64bit\wic_x64_chs.exe /q"
      ${Else}
      ExecWait "$INSTDIR\Tools\32bit\wic_x86_chs.exe /q"
      ${EndIf}
    ${EndIf}
  DetailPrint "正在安装 .NET Framework 4.0,可能需要几分钟,请稍等"


;File "dotnetfx4.0.30319.exe"


  ;ExecWait '$INSTDIR\Tools\dotnetfx4.0.30319.exe  /norestart /ChainingPackage FullX64Bootstrapper' $R1
  ExecWait '$INSTDIR\Tools\dotnetfx4.0.30319.exe /norestart'
  ${EndIf}
;对.NET框架进行检验是否安装完成,如若没有,回调.NET安装
Call GetNetFrameworkVersion
  Pop $R1
  ${If} $R1 < '4.0.30319'
  DetailPrint ".NET Framework 4.0是必要安装,不能取消此安装,请执行安装"
  Goto VSRedistInstalled
  ${EndIf}
  
;获取本机IP
  ip::get_ip
  Pop $0
  ; test entry
  ;StrCpy $0 '192.168.0.100;127.0.0.1;152.168.0.101;169.254.0.1;'
  ;Loop:
  Push $0
  Call GetNextIp
  Call CheckIp
  Pop $2 ; Type of current IP-address
  Pop $1 ; Current IP-address
   Pop $0 ; Remaining addresses
  StrCmp $2 '1' '' NoLoopBackIp
  ;  MessageBox MB_OK "LoopBack IP-address: $1"
    Goto Continue
  NoLoopBackIp:
  StrCmp $2 '2' '' NoAPA
  ;  MessageBox MB_OK "Automatic Private IP-address: $1"
    Goto Continue
  NoAPA:
  StrCmp $2 '3' '' NoLanIp
   ; MessageBox MB_OK "Network IP-address: $1"
    StrCpy $local_ip $1
Goto Continue
  NoLanIp:
  ;MessageBox MB_OK "Internet IP-address: $1"
  Continue:
  ;StrCmp $0 '' ExitLoop Loop
  ;ExitLoop:
;获取IP结束 $local_ip为本机IP


;获取空闲可用端口
  StrCpy  $ports_firstports 80
${If} $ports_value != 1
StrCpy  $ports_firstports 8000
${Endif}
  ${while} $ports_value != 1
  Call check_ports
  ${EndWhile}
 ; MessageBox MB_OK $free_ports
;获取空闲端口结束 空闲端口为$free_ports,$free_ports在check_ports函数里赋值

;注册MySql服务
; nsExec::Exec '"$INSTDIR\WebSite\SetWebConfig.exe" /S'
  WriteINIStr "$INSTDIR\Tools\mysql5\my.ini" "WinMySQLAdmin" "Server" "$INSTDIR\Tools\mysql5\bin\mysqld-nt.exe"  ;修改MYSQL INI文件
  WriteINIStr "$INSTDIR\Tools\mysql5\my.ini" "mysqld" "basedir" "$INSTDIR\Tools\mysql5"
  WriteINIStr "$INSTDIR\Tools\mysql5\my.ini" "mysqld" "datadir" "$INSTDIR\Tools\mysql5\data"
  DetailPrint "正在安装Mysql并启动服务"
nsExec::ExecToLog '"$INSTDIR\Tools\mysql5\bin\mysqld-nt.exe" --install mysql --defaults-file="$INSTDIR\Tools\mysql5\my.ini"' ;注册并启动MYSQL服务
  nsExec::ExecToLog 'net start mysql'
 ; nsExec::Exec "$INSTDIR\Tools\RegMySql.bat" ;20140807
 
  
;如果先安装了.NET框架后安装IIS,就对.NET进行注册
  ;nsExec::ExecToLog 'cmd.exe cd C:\Windows\Microsoft.NET\Framework\v4.0.30319" aspnet_regiis.exe -i'
  nsExec::Exec "$INSTDIR\Tools\RegNet.bat"
  DetailPrint "注册.NET框架"


  ClearErrors
;生成网站
  StrCpy $1 "v4.0"
  StrCpy $2 "Integrated"
  StrCpy $3 "true"
 ;NsisIIs::CreateAppPool "MyWebSiteAppPool"
  NsisIIs::CreateAppPool "KSAppPool"
  Pop $0
 ; MessageBox MB_OK $0
  StrCpy $1 "KSAppPool"
  ; StrCpy $1 "DefaultAppPool"
  ClearErrors
  ClearErrors
NsisIIs::CreateWebSite "KSWebSite"  "$INSTDIR\WebSite"  "@$local_ip:$free_ports:"
  Pop $0
 ; MessageBox MB_OK $0
  DetailPrint $0
  ${If} $0 == "Create Website Success."
  Push $6
  StrCpy $Index $6


; InstallUpdateServer:
  ExecWait "$INSTDIR\Tools\setup.exe" ;软件升级的服务的安装
;  ReadRegDWORD $0 HKLM "SYSTEM\CurrentControlSet\Services\KingsunUpdate" "ImagePath"
StrCpy $update_path $0
;  StrCpy $empty ""
;  StrCmp $update_path $empty IsQuit
;  Goto Finish
  
;  IsQuit:
;  MessageBox MB_YESNO|MB_ICONQUESTION|MB_TOPMOST "软件升级服务必须安装的,安装升级服务请点击“是”,如若安装.NET后无法安装升级服务,请点击“否”后中止安装并重启计算机再运行安装包继续余下安装!!" IDNO +2
;  Goto InstallUpdateServer
;  Quit
  WriteRegStr HKLM "SOFTWARE\WisdomCampus" "KSWebVersion" "$version_number"
  WriteRegStr HKLM "SOFTWARE\WisdomCampus" "KSWebPath" "$INSTDIR"




  ;写入XML配置文件
  nsisXML::create
  nsisXML::load "$INSTDIR\WebSite\SysData.xml"
  nsisXML::select '/SysData/SysInfo/Version'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText '$version_number'


  nsisXML::select '/SysData/SysInfo/MySQLPath'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText '$INSTDIR\Tools\mysql5'


  nsisXML::select '/SysData/SysInfo/WebsitePath'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText '$INSTDIR\WebSite'


  nsisXML::select '/SysData/SysInfo/ProxyPath'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText "$update_path"


  nsisXML::select '/SysData/SysInfo/WebsiteName'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText "KSWebSite"


  nsisXML::select '/SysData/SysInfo/ServerIP'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText '$local_ip'


  nsisXML::select '/SysData/SysInfo/Website'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText "http://$local_ip:$free_ports/Login.aspx"


  nsisXML::select '/SysData/SysInfo/WebsiteIndexId'
  IntCmp $2 0 notFound
  nsisXML::getText
  nsisXML::setText '$Index'


  nsisXML::save "$INSTDIR\WebSite\SysData.xml"


  ExecWait '$INSTDIR\Tools\AddMime\AddMime.exe'
  Goto Finish
  
  notFound:
  DetailPrint "没有找到系统配置文件"
  
  Finish:
  MessageBox MB_OK "金太阳智慧校园云平台网站安装成功,请在浏览器输入$local_ip:$free_ports/Login.aspx地址访问或在开始菜单栏里的金太阳智慧校园云平台目录下点击WebSite打开网站"
  ;安装成功,写入注册表






  ${ElseIf} $0 == "Error creating website."
  MessageBox MB_OK "安装中止,网站ID错误,请联系管理员"
  Abort
  
  ${ElseIf} $0 == "Error:ADsGetObject Getting IDispatch:iContainer"
    MessageBox MB_OK "IIS没有安装完整,网站生成失败,安装中止!"
  Abort
  
  ${Else}
  MessageBox MB_OK "安装错误并中止,请联系管理员"
  Abort
  ${EndIf}


 
/* ${Switch} $0
    ${Case} 'Create Website Success'  ;"网站生成成功"
    ;安装完成后拷贝一个配置文件
    CopyFiles $INSTDIR\Tools\IIS\SysData.xml $INSTDIR\Tools\SysData.xml
    MessageBox MB_OK "网站生成成功"
${Break}


    ${Case} 'Error:ADsGetObject Getting IDispatch:iContainer' ;"安装IIS没有安装完整"
    MessageBox MB_OK "IIS没有安装完整,网站生成失败,安装中止!"
    Abort
    ${Break}
${EndSwitch}
*/






SectionEnd


Section -AdditionalIcons
  ;WriteIniStr "$SMPROGRAMS\方直智慧云平台.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  SetShellVarContext all
  CreateDirectory "$SMPROGRAMS\金太阳智慧校园云平台"
  ;CreateShortCut "$SMPROGRAMS\My application\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  CreateShortCut "$SMPROGRAMS\金太阳智慧校园云平台\Website.lnk" "http://$local_ip:$free_ports/Login.aspx"
  CreateShortCut "$SMPROGRAMS\金太阳智慧校园云平台\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd


Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd


Function GetNetFrameworkVersion


 ;获取.Net Framework版本支持


 Push $1


 Push $0


 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"


 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"


 StrCmp $0 1 KnowNetFrameworkVersion +1


 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"


 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"


 StrCmp $0 1 KnowNetFrameworkVersion +1


 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"


 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"


 StrCmp $0 1 KnowNetFrameworkVersion +1


 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"


 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"


 StrCmp $1 "" +1 +2


 StrCpy $1 "2.0.50727.832"


 StrCmp $0 1 KnowNetFrameworkVersion +1


 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"


 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"


 StrCmp $1 "" +1 +2


 StrCpy $1 "1.1.4322.573"


 StrCmp $0 1 KnowNetFrameworkVersion +1


 ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"


 ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"


 StrCmp $1 "" +1 +2


 StrCpy $1 "1.0.3705.0"


 StrCmp $0 1 KnowNetFrameworkVersion +1


 StrCpy $1 "not .NetFramework"


 KnowNetFrameworkVersion:


 Pop $0


 Exch $1


 FunctionEnd
 
; Function GetNextIp
; input: head of stack
; format: 'ip1;ip2;ip3;ip4;'
; output: 'ip1' head of stack
;         'ip2;ip3;ip4;' second entry of stack


Function GetNextIp
  Exch $0
  Push $1
  Push $2
  Strcpy $2 0             ; Counter
  Loop:
    IntOp $2 $2 + 1
    StrCpy $1 $0 1 $2
    StrCmp $1 '' ExitLoop
    StrCmp $1 ';' '' Loop
    StrCpy $1 $0 $2       ; IP-address
    IntOp $2 $2 + 1
    StrCpy $0 $0 '' $2    ; Remaining string
  ExitLoop:
  Pop $2
  Push $0
  Exch 2
  Pop $0
  Exch $1
FunctionEnd


; Function CheckIP
; input: IP-address on stack
; output: additional entry on stack
;         1 - LoopBack IP (localhost, indicates no connection to a LAN or to the internet).
;         2 - Automatic Private IP Address (no DHCP server).
;         3 - Network IP.
;         4 - Internet IP.
; Eg:
; Push '192.168.0.100'
; Call CheckIP
; Pop $0 ; Contains '3'
; Pop $1 ; Contains '192.168.0.100'


Function CheckIP
  Exch $0
  Push $1


  ; Check 127.x.x.x
  Push '127.0.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range1     ; IP cannot be in range of LoopBack addresses
  Push '127.255.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LoopBack      ; We found a LoopBack IP


  ; Check 10.x.x.x
  Range1:
  Push '10.0.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range2     ; IP cannot be in range 1
  Push '10.255.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LanIp         ; We found a LanIp


  ; Check 172.16.x.x to 172.31.x.x
  Range2:
  Push '172.16.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range3     ; IP cannot be in range 2
  Push '172.31.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LanIp         ; We found a LanIp


  ; Check 192.168.x.x
  Range3:
  Push '192.168.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' Range4     ; IP cannot be in range 3
  Push '192.168.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 LanIp         ; We found a LanIp


  ; Check 169.254.x.x
  Range4:
  Push '169.254.0.0'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 2 '' InternetIp ; It should be an internet IP
  Push '169.254.255.255'
  Push $0
  Call VersionCheck
  Pop $1
  StrCmp $1 1 APA           ; We found an Automatic Private IP Address


  Goto InternetIp           ; Remaining addresses are internet IPs


  LoopBack:
  StrCpy $1 1
  Goto Exit


  APA:
  StrCpy $1 2
  Goto Exit


  LanIp:
  StrCpy $1 3
  Goto Exit


  InternetIp:
  StrCpy $1 4


  Exit:
  Exch $1
  Exch 1
  Exch $0
  Exch 1
FunctionEnd


; Function VersionCheck
; input: 'v1', 'v2' on stack
; output 1 - if number 1 is newer
;        2 - if number 2 is newer
;        0 - if it is the same verion
; Eg:
; Push '3.5.1.4'
; Push '3.5'
; Call VersionCheck
; Pop $0 ; now contains 1


Function VersionCheck
  Exch $0 ;second versionnumber
  Exch
  Exch $1 ;first versionnumber
  Push $R0 ;counter for $0
  Push $R1 ;counter for $1
  Push $3 ;temp char
  Push $4 ;temp string for $0
  Push $5 ;temp string for $1
  StrCpy $R0 "-1"
  StrCpy $R1 "-1"
  Start:
  StrCpy $4 ""
  DotLoop0:
  IntOp $R0 $R0 + 1
  StrCpy $3 $0 1 $R0
  StrCmp $3 "" DotFound0
  StrCmp $3 "." DotFound0
  StrCpy $4 $4$3
  Goto DotLoop0
  DotFound0:
  StrCpy $5 ""
  DotLoop1:
  IntOp $R1 $R1 + 1
  StrCpy $3 $1 1 $R1
  StrCmp $3 "" DotFound1
  StrCmp $3 "." DotFound1
  StrCpy $5 $5$3
  Goto DotLoop1
  DotFound1:
  Strcmp $4 "" 0 Not4
    StrCmp $5 "" Equal
    Goto Ver2Less
  Not4:
  StrCmp $5 "" Ver2More
  IntCmp $4 $5 Start Ver2Less Ver2More
  Equal:
  StrCpy $0 "0"
  Goto Finish
  Ver2Less:
  StrCpy $0 "1"
  Goto Finish
  Ver2More:
  StrCpy $0 "2"
  Finish:
  Pop $5
  Pop $4
  Pop $3
  Pop $R1
  Pop $R0
  Pop $1
  Exch $0
FunctionEnd


Function check_ports
StrCpy $1 $ports_firstports
;validate_port:
  TCP::CheckPort $1
  Pop $0
;DetailPrint $0
${Switch} $0
    ${Case} 'free'  ;"端口空闲."
    StrCpy $ports_value 1
${Break}


    ${Case} 'socket_error' ;"非法 TCP 端口号. 端口号必须在 1 至 65535 之间."
    StrCpy $ports_value 0
    IntOp $ports_firstports $ports_firstports + 1
    ${Break}


    ${Case} 'inuse' ; "端口正在被程序使用."
    StrCpy $ports_value 2
    IntOp $ports_firstports $ports_firstports + 1
    ${Break}


    ${Case} 'bind_error' ;"端口绑定"
    StrCpy $ports_value 3
    IntOp $ports_firstports $ports_firstports + 1
    ${Break}


${EndSwitch}
    StrCpy $free_ports $1


FunctionEnd


;Function in.CheckProcess
;DetectProc:
;;FindProcDLL::FindProc ${PRODUCT_PROC_NAME}


   ;; StrCmp $R0 1 +3 +9


;FindProcDLL::FindProc ${PRODUCT_PROC_NAME}


    ;StrCmp $R0 1 +1 +7
   ; MessageBox MB_OKCANCEL|MB_ICONSTOP "安装程序检测到 ${PRODUCT_NAME} 正在运行。$\r$\n$\r$\n点击 “确定” 强制关闭${PRODUCT_NAME},继续安装。$\r$\n点击 “取消” 退出安装程序。" IDCANCEL Exit
    ;Retry:
; DetailPrint "正在退出$(^Name),请稍候..."


 ;KillProcDLL::KillProc ${PRODUCT_PROC_NAME}
    ; sleep 444
   ;  goto DetectProc
   ;  Exit:
   ;  ;Abort
   ;  Quit
;FunctionEnd




Function LaunchLink
ExecShell "open" "http://$local_ip:$free_ports/Login.aspx"
; IEFunctions::OpenBrowser /NOUNLOAD
; IEFunctions::SurfTo /NOUNLOAD "http://192.168.3.92:8003/default.aspx"
FunctionEnd
/******************************
 *  以下是安装程序的卸载部分  *
 ******************************/


Section Uninstall
  ;ExecWait "$PROGRAMFILES\WisdomCampus\Tools\DelMySql.bat"
  isfound:
  nsExec::Exec 'NET STOP "KingsunUpdate"'
  MessageBox MB_OK "请选择删除KingsunSoftUpdateSetup,然后点击完成"
  ExecWait "$INSTDIR\Tools\setup.exe" ;软件升级的服务的的删除
  ReadRegDWORD $0 HKLM "SYSTEM\CurrentControlSet\Services\KingsunUpdate" "ImagePath"
  StrCpy $empty ""
  StrCmp $0 $empty nofound
  goto isfound
  
  nofound:
;  nsExec::ExecToLog 'net stop mysql' ;停止并删除MYSQL服务
;  nsExec::ExecToLog '"$INSTDIR\Tools\mysql5\bin\mysqld-nt.exe" --remove mysql --defaults-file="$INSTDIR\Tools\mysql5\my.ini"'
  nsExec::Exec "$INSTDIR\Tools\DelMySql.bat"
; UninstallUpdateServer:
;  ExecWait "$INSTDIR\Tools\setup.exe" ;软件升级的服务的的删除
;  ReadRegDWORD $0 HKLM "SYSTEM\ControlSet001\Services\Services" "ImagePath"
;  StrCpy $empty ""
;  StrCmp $0 $empty UninstallUpdateServer
  
  DeleteRegKey /ifempty HKLM "Software\WisdomCampus"
  Delete "$INSTDIR\${PRODUCT_NAME}.url"
  strCpy $XML_PATH '$INSTDIR\WebSite\SysData.xml'
  nsisXML::create ;根据Index删除网站,Index值为$3
  nsisXML::load "$XML_PATH"
  nsisXML::select '/SysData/SysInfo/WebsiteIndexId'
  IntCmp $2 0 notFound
  nsisXML::getText
  Push $3
  StrCmp $3 "" notFound
  NsisIIs::DeleteWebSite "$3"


notFound:
RMDir /r "$INSTDIR\Tools\*.*"
  RMDir /r "$INSTDIR\WebSite\*.*"


SetShellVarContext all
  Delete "$SMPROGRAMS\金太阳智慧校园云平台\Uninstall.lnk"
  Delete "$SMPROGRAMS\金太阳智慧校园云平台\Website.lnk"
  RMDir /r "$SMPROGRAMS\金太阳智慧校园云平台"
  Delete "$INSTDIR\uninst.exe"
  


  NsisIIs::DeleteAppPool "KSAppPool"    ;删除程序池
  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  SetAutoClose true
SectionEnd


#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#


Function un.onInit
  FindProcDLL::FindProc "${PRODUCT_PROC_NAME}.exe"
   Pop $R0
   IntCmp $R0 1 0 no_run
   MessageBox MB_ICONSTOP "安装程序检测到 ${PRODUCT_PROC_NAME} 正在运行,请退出程序后重试"
   Quit
   no_run:
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您确实要完全移除 $(^Name) ,及其所有的组件?" IDYES +2
  Abort
FunctionEnd


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从您的计算机移除。"
FunctionEnd

你可能感兴趣的:(NSIS)