; 该脚本使用 HM VNISEdit 脚本编辑器向导产生
!include "StrFunc.nsh"
${StrRep}

; 安装程序初始定义常量
!define PRODUCT_NAME "MOKA Factory Tools"
!define PRODUCT_VERSION "3.1.0.1"
!define PRODUCT_PUBLISHER "MOKA"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\MOKA Factory Tools.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor lzma

; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"

; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

;安装.net运行环境
!define NETVersion "4.5.50709"
!define NETPath "../Runtime\ndp48-x86-x64-allos-enu.exe"
!define NETInstaller "ndp48-x86-x64-allos-enu.exe"
Section "MS .NET Framework v${NETVersion}" SecFramework
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"
     ${If} $0 == "4.5.50709"
      DetailPrint "Microsoft .NET Framework is already installed!"
   	 ${Else}
   	 File /oname=$TEMP\${NETInstaller} ${NETPath}
  		DetailPrint "Starting Microsoft .NET Framework v${NETVersion} Setup..."
			ExecWait "$TEMP\${NETInstaller}"
  		Return
  	${EndIf}
SectionEnd

; 语言选择窗口常量设置
!define MUI_LANGDLL_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_LANGDLL_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_LANGDLL_REGISTRY_VALUENAME "NSIS:Language"

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME
; 许可协议页面
!insertmacro MUI_PAGE_LICENSE "../Licence.txt"
; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面
!define MUI_FINISHPAGE_RUN "$INSTDIR\MOKA Factory Tools.exe"
!insertmacro MUI_PAGE_FINISH

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

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

; 安装预释放文件
!insertmacro MUI_RESERVEFILE_LANGDLL
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
; ------ MUI 现代界面定义结束 ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Setup${PRODUCT_VERSION}.exe"
InstallDir "$PROGRAMFILES\MOKA Factory Tools"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
  File "../bin\Release\System.Data.SQLite.xml"
  CreateDirectory "$SMPROGRAMS\MOKA Factory Tools"
  CreateShortCut "$SMPROGRAMS\MOKA Factory Tools\MOKA Factory Tools.lnk" "$INSTDIR\MOKA Factory Tools.exe"
  CreateShortCut "$DESKTOP\MOKA Factory Tools.lnk" "$INSTDIR\MOKA Factory Tools.exe"
  Delete "$INSTDIR\Config.json"
  File "../bin\Release\System.Data.SQLite.dll"
  File "../bin\Release\MOKA Factory Tools.exe"
  File "../bin\Release\MOKA Factory Tools.exe.config"
  File "../bin\Release\Newtonsoft.Json.dll"
  File "../bin\Release\CSkin.xml"
  File "../bin\Release\CSkin.dll"
  File "../bin\Release\Config.json"
  File "../Runtime\ndp48-x86-x64-allos-enu.exe"
  File "../bin\Release\route-ping.bat"
  File "../bin\Release\route-tracert.bat"
  SetOutPath "$INSTDIR\x86"
  File "../bin\Release\x86\*.*"
  SetOutPath "$INSTDIR\x64"
  File "../bin\Release\x64\*.*"
  SetOutPath "$INSTDIR\zh-CN"
  File "../bin\Release\zh-CN\*.*"
SectionEnd


Section -AdditionalIcons
  CreateShortCut "$SMPROGRAMS\MOKA Factory Tools\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\MOKA Factory Tools.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}" "DisplayIcon" "$INSTDIR\MOKA Factory Tools.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

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

Function .onInit
  Call RemoveOld
  !insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd

/******************************
 *  以下是安装程序的卸载部分  *
 ******************************/

Section Uninstall
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\ndp48-x86-x64-allos-enu.exe"
  Delete "$INSTDIR\Config.json"
  Delete "$INSTDIR\CSkin.dll"
  Delete "$INSTDIR\CSkin.xml"
  Delete "$INSTDIR\Devaof.DB.SqlBuilder.dll"
  Delete "$INSTDIR\Devaof.DB.SQLite.dll"
  Delete "$INSTDIR\Newtonsoft.Json.dll"
  Delete "$INSTDIR\MOKA Factory Tools.exe"
  Delete "$INSTDIR\MOKA Factory Tools.exe.config"
  Delete "$INSTDIR\MOKA Fartory Tools Server.exe"
  Delete "$INSTDIR\MOKA Fartory Tools Server.exe.config"
  Delete "$INSTDIR\System.Data.SQLite.dll"
  Delete "$INSTDIR\System.Data.SQLite.xml"
  Delete "$SMPROGRAMS\MOKA Factory Tools\Uninstall.lnk"
  Delete "$DESKTOP\MOKA Factory Tools.lnk"
  Delete "$SMPROGRAMS\MOKA Factory Tools\MOKA Factory Tools.lnk"
  Delete "$INSTDIR\*.*"
  RMDir "$SMPROGRAMS\MOKA Factory Tools"
  RMDir "$INSTDIR"
  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  SetAutoClose true
SectionEnd

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

Function un.onInit
!insertmacro MUI_UNGETLANGUAGE
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您确实要完全移除 $(^Name) ，及其所有的组件？" IDYES +2
  Abort
FunctionEnd

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

Function RemoveOld
  # 获取注册表信息;
  ReadRegStr $0 HKLM "${UNINSTALL_KEY}\SCBC Factory Tools" "UninstallString"
  ${IF} $0 != ""
    MessageBox MB_ICONINFORMATION|MB_OK "检测到旧的安装包，将会移除。"
    ${StrRep} $R1 $0 "\uninst.exe" ""
    # 复制文件;
    CopyFiles "$R1\Config.json" "$INSTDIR\Config.json"
    CopyFiles "$R1\Error.db" "$INSTDIR\Error.db"
    CopyFiles "$R1\Keys" "$INSTDIR\Keys"
    CopyFiles "$R1\log" "$INSTDIR\log"
    CopyFiles "$R1\download" "$INSTDIR\download"
    # 删除程序及注册表信息;
    Delete "$R1\uninst.exe"
    Delete "$R1\CSkin.dll"
    Delete "$R1\CSkin.xml"
    Delete "$R1\Devaof.DB.SqlBuilder.dll"
    Delete "$R1\Devaof.DB.SQLite.dll"
    Delete "$R1\Newtonsoft.Json.dll"
    Delete "$R1\SCBC Factory Tools.exe"
    Delete "$R1\SCBC Factory Tools.exe.config"
    Delete "$R1\SCBC Fartory Tools Server.exe"
    Delete "$R1\SCBC Fartory Tools Server.exe.config"
    Delete "$R1\System.Data.SQLite.dll"
    Delete "$R1\System.Data.SQLite.xml"
    Delete "$SMPROGRAMS\SCBC Factory Tools\Uninstall.lnk"
    Delete "$DESKTOP\SCBC Factory Tools.lnk"
    Delete "$SMPROGRAMS\SCBC Factory Tools\SCBC Factory Tools.lnk"
    RMDir "$SMPROGRAMS\SCBC Factory Tools"
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SCBC Factory Tools"
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\App Paths\SCBC Factory Tools.exe"
  ${ENDIF}
FunctionEnd
