# 安装包防解压
!packhdr temp.dat "cmd /c Copy /B temp.dat /B +Nanlon.7z temp.dat"
#=================================================================

/************************
* 引入 nsh 脚本
************************/

# 头文件目录
!addincludedir ".\Include"

!include "MUI2.nsh"
!include "x64.nsh"

/************************
* 安装程序初始定义常量 
************************/

!define APPNAME            "wlt"
!define PRODUCT_NAME       "wlt"
!define PRODUCT_VERSION    "5.0.1.0"
!define PRODUCT_PUBLISHER  "aaaaaa"
!define PRODUCT_WEBSITE    ""
!define WEBSITE_LINK       ""
!define APPNAMESRT            "王老头加速器"
# 定义注册表
!define PRODUCT_KEY_ROOT   "HKLM"
!define PRODUCT_KEY_UNINST "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_KEY_APPDIR "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\${APPNAME}.exe"

/************************
* 初始化定义变量
************************/

# 声明变量
Var BIT
Var BITSUFFIX

# 标题
Caption "${PRODUCT_NAME} ${PRODUCT_VERSION} $BITSUFFIX"
# 设置文件覆盖标记
SetOverwrite try
# 设置压缩选项
SetCompress auto
# 选择压缩方式
SetCompressor /SOLID lzma
SetCompressorDictSize 32
# 设置数据块优化
SetDatablockOptimize on
# 设置在数据中写入文件时间
SetDateSave on
# 请求应用程序管理员权限
RequestExecutionLevel admin
# 是否允许安装在根目录下
AllowRootDirInstall false
# 设置是否显示安装详细信息
ShowInstDetails hide 
# 设置是否显示卸载详细信息
ShowUnInstDetails hide 

# 设置安装类型
InstType "推荐安装"
InstType "全部安装"

/************************
* MUI 预定义常量
************************/

!define MUI_UI ".\Resource\mui_sdesc.exe"

/*** 安装界面 ***/
!define MUI_ICON ".\Resource\Install.ico"

!define MUI_WELCOMEFINISHPAGE_BITMAP ".\Resource\nsis3-grey.bmp"

!define MUI_FINISHPAGE_RUN "$INSTDIR\${APPNAME}.exe"
!define MUI_FINISHPAGE_RUN_TEXT "运行 ${PRODUCT_NAME}"

/*** 卸载界面 ***/
!define MUI_UNICON ".\Resource\Uninst.ico"

!define MUI_UNWELCOMEFINISHPAGE_BITMAP ".\Resource\nsis3-grey.bmp"

/************************
* 安装/卸载 页面设置
************************/

/*** 加快载入界面资源 ***/
ReserveFile ".\Resource\Install.ico"
ReserveFile ".\Resource\nsis3-grey.bmp"

# 插件目录
!ifdef NSIS_PACKEDVERSION
!define /math NSIS_VERSON_MAJOR ${NSIS_PACKEDVERSION} >> 24
!else
!define NSIS_VERSON_MAJOR 2
!endif

!if ${NSIS_VERSON_MAJOR} > 2

# NSIS 3.x 方式引用插件
!addplugindir "/x86-unicode" ".\Plugins\x86-unicode"
!addplugindir "/x86-ansi" ".\Plugins\x86-ansi"

!else

# NSIS 2.x 方式引用插件
!ifdef NSIS_UNICODE
!addplugindir ".\Plugins\x86-unicode"
!else
!addplugindir ".\Plugins\x86-ansi"
!endif

!endif

# dll
ReserveFile /plugin "System.dll"
ReserveFile /plugin "Process.dll"

/*** 安装页面 ***/

# 欢迎页面
!insertmacro MUI_PAGE_WELCOME
# 组件选择页面
!insertmacro MUI_PAGE_COMPONENTS
# 安装目录选择页面
!define MUI_PAGE_CUSTOMFUNCTION_show OnDirPageshow
!insertmacro MUI_PAGE_DIRECTORY
# 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
# 安装完成页面
!insertmacro MUI_PAGE_FINISH

/*** 卸载页面 ***/

# 卸载欢迎页面
!insertmacro MUI_UNPAGE_WELCOME
# 卸载目录选择页面
!insertmacro MUI_UNPAGE_DIRECTORY
# 卸载过程页面
!insertmacro MUI_UNPAGE_INSTFILES
# 卸载完成页面
!insertmacro MUI_UNPAGE_FINISH

/*** 语言文件 ***/

!insertmacro MUI_LANGUAGE "SimpChinese"

/************************
* 版本声明
************************/

VIProductVersion "${PRODUCT_VERSION}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} "ProductName"     "${PRODUCT_NAME}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} "Comments"        "${PRODUCT_NAME}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} "LegalCopyright"  "Copyright ? 2017 ${PRODUCT_PUBLISHER}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} "FileDescription" "${PRODUCT_NAME}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} "FileVersion"     "${PRODUCT_VERSION}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} "ProductVersion"  "${PRODUCT_VERSION}"

/************************
* 安装文件定义
************************/

Name "${PRODUCT_NAME}"
OutFile "${PRODUCT_NAME}_${PRODUCT_VERSION}.exe"
InstallDir "$PROGRAMFILES64\${PRODUCT_NAME}"

InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
BrandingText " ${WEBSITE_LINK}"

/************************
* 安装 Section 段
************************/

!macro ProcessCleanup  # 进程清理
ProcessFindLoop:
    Process::Find "$INSTDIR\${APPNAME}.exe"
    Pop $R0
    IntCmp $R0 0 ProcessFindDone
    Process::Kill $R0
    Pop $R1
    IntCmp $R1 0 ProcessFindDone
    Goto ProcessFindLoop
ProcessFindDone:
!macroend

!macro AddUninstReg
# 将卸载信息写入注册表
    WriteRegStr HKLM "${PRODUCT_KEY_UNINST}" "DisplayName"     "${PRODUCT_NAME} $BITSUFFIX"
    WriteRegStr HKLM "${PRODUCT_KEY_UNINST}" "UninstallString" "$INSTDIR\uninst.exe"
    WriteRegStr HKLM "${PRODUCT_KEY_UNINST}" "DisplayIcon"     "$INSTDIR\${APPNAME}.exe"
    WriteRegStr HKLM "${PRODUCT_KEY_UNINST}" "DisplayVersion"  "${PRODUCT_VERSION}"
    WriteRegStr HKLM "${PRODUCT_KEY_UNINST}" "URLInfoAbout"    "${PRODUCT_WEBSITE}"
    WriteRegStr HKLM "${PRODUCT_KEY_UNINST}" "Publisher"       "${PRODUCT_PUBLISHER}"
# 获取安装段的大小(KB)写入注册表
    SectionGetSize ${CoreFiles} $R0
    WriteRegDWORD HKLM "${PRODUCT_KEY_UNINST}" "EstimatedSize" "$R0"
    WriteRegStr      HKLM "${PRODUCT_KEY_APPDIR}" "" "$INSTDIR\${APPNAME}.exe"
    SetRegView lastused
!macroend

Section "主程序(必选)" CoreFiles
    
    SectionIn RO 1 2
    
    SetDetailsPrint textonly
    DetailPrint "正在安装${PRODUCT_NAME}..."
    SetDetailsPrint listonly
    
    # 进程清理
    !insertmacro ProcessCleanup
    
    SetOutPath "$INSTDIR"
    
    ${If} ${RunningX64}
    File /r ".\${PRODUCT_NAME}\x64\*.*"
    
    ${Else}
    File /r ".\${PRODUCT_NAME}\x86\*.*"
    
    ${EndIf}
    
    WriteUninstaller "$INSTDIR\uninst.exe"
    
SectionEnd

SectionGroup /e "快捷方式" Shortcuts
    
    Section "桌面" ShortcutsDesktop
        
        SectionIn 1 2
        
        SetDetailsPrint textonly
        DetailPrint "正在创建桌面快捷方式..."
        SetDetailsPrint listonly
        
        SetShellVarContext current
        CreateShortCut "$DESKTOP\${APPNAMESRT}.lnk" "$INSTDIR\${APPNAME}.exe"
    
    SectionEnd

    ; Section "开始菜单" ShortcutsPrograms
        
    ;     SectionIn 1 2
        
    ;     SetDetailsPrint textonly
    ;     DetailPrint "正在创建桌开始菜单目录..."
    ;     SetDetailsPrint listonly
        
    ;     SetShellVarContext current
    ;     CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
    ;     CreateShortCut    "$SMPROGRAMS\${PRODUCT_NAME}\${APPNAME}.lnk" "$INSTDIR\${APPNAME}.exe"
    ;     CreateShortCut    "$SMPROGRAMS\${PRODUCT_NAME}\卸载${APPNAME}.lnk" "$INSTDIR\uninst.exe"
    
    ; SectionEnd

SectionGroupEnd

; Section "添加到系统右键菜单" AddSystemContextMenu
    
;     SectionIn 1 2
    
;     SetDetailsPrint textonly
;     DetailPrint "正在添加系统右键菜单..."
;     SetDetailsPrint listonly
    
;     WriteRegStr HKCR "*\shell\${APPNAME}" "" "使用${PRODUCT_NAME}编辑"
;     WriteRegStr HKCR "*\shell\${APPNAME}\command" "" `"$INSTDIR\${APPNAME}.exe" "%1"`
    
; SectionEnd

; Section "替换系统记事本程序" ReplaceSystemNotepad
    
;     SectionIn 2
    
;     SetDetailsPrint textonly
;     DetailPrint "正在替换系统记事本程序..."
;     SetDetailsPrint listonly
    
;     ${If} ${RunningX64}
;     SetRegView 64
;     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" "Debugger" `"$INSTDIR\${APPNAME}.exe" "/z"`
;     SetRegView lastused
    
;     ${Else}
;     SetRegView 32
;     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" "Debugger" `"$INSTDIR\${APPNAME}.exe" "/z"`
;     SetRegView lastused
    
;     ${EndIf}
    
; SectionEnd

Section -Post
    
    SetDetailsPrint textonly
    DetailPrint "正在注册文件..."
    SetDetailsPrint listonly
     
    ${If} ${RunningX64}
    SetRegView 64 
    !insertmacro AddUninstReg
    SetRegView lastused
    
    ${Else}
    SetRegView 32
    !insertmacro AddUninstReg
    SetRegView lastused
    
    ${EndIf}
    
    # 刷新图标关联
    System::Call `shell32::SHChangeNotify(i 0x08000000, i 0, i 0, i 0)`
    
SectionEnd

/************************
* 安装回调函数
************************/
    
Function .onInit
    # 初始化插件目录
    InitPluginsDir
    File "/ONAME=$PluginsDIR\Install.ico" ".\Resource\Install.ico"
    File "/ONAME=$PluginsDIR\nsis3-grey.bmp" ".\Resource\nsis3-grey.bmp"

    # 系统版本位数检测
    ${If} ${RunningX64}
    StrCpy $BIT       "64"
    StrCpy $BITSUFFIX "(64-bit)"
    ${Else}
    StrCpy $BIT       ""
    StrCpy $BITSUFFIX ""
    ${EndIf}
    
    # 创建互斥防止重复运行
    System::Call `kernel32::CreateMutex(i 0, i 0, t "${PRODUCT_NAME}_installer") i .r1 ?e`
    Pop $R0
    StrCmp $R0 0 +3
    MessageBox MB_OK|MB_ICONEXCLAMATION "安装程序已经运行！"
    Abort
    
    # 旧版本检测及卸载
    SetRegView 64
    ClearErrors
    ReadRegStr $R0 HKLM "${PRODUCT_KEY_UNINST}" "UninstallString"
    ${Unless} ${Errors}
    ReadRegStr $R1 HKLM "${PRODUCT_KEY_UNINST}" "DisplayVersion"
    SetRegView lastused
    Goto Uninst
    
    ${EndUnless}
    
    SetRegView 32
    ClearErrors
    ReadRegStr $R0 HKLM "${PRODUCT_KEY_UNINST}" "UninstallString"
    ${Unless} ${Errors}
    ReadRegStr $R1 HKLM "${PRODUCT_KEY_UNINST}" "DisplayVersion"
    SetRegView lastused
    
Uninst:
    
    ${AndUnless} ${Cmd} `MessageBox MB_YESNO|MB_ICONQUESTION "检测到本机已经安装了 ${APPNAME} v$R1 $\n$\n是否卸载已安装的版本？" /SD IDYES IDNO`
    System::Call "*(&t${NSIS_MAX_STRLEN}R0)p.r0"
    System::Call "shlwapi::PathParseIconLocation(pr0)"
    System::Call "shlwapi::PathRemoveFileSpec(pr0)"
    System::Call "*$0(&t${NSIS_MAX_STRLEN}.R2)"
    System::Free $0
    ${AndUnless} $R2 == ""
    ExecWait `"$R0" /S _?=$R2` $0
    ${EndUnless}
    
FunctionEnd

Function OnDirPageshow    # 安装目录设置
    SetRegView 64
    ReadRegStr $0 HKLM "${PRODUCT_KEY_APPDIR}" ""
    SetRegView lastused
    ${If} $0 == ""
    
    SetRegView 32
    ReadRegStr $0 HKLM "${PRODUCT_KEY_APPDIR}" ""
    SetRegView lastused
    ${ElseIf} $0 != ""
    
    FindWindow $R0 "#32770" "" $HWNDPARENT

    # 禁用浏览按钮
    GetDlgItem $0 $R0 1001
    EnableWindow $0 0

    # 禁用编辑的目录
    GetDlgItem $0 $R0 1019
    EnableWindow $0 0

    GetDlgItem $0 $R0 1006
    SendMessage $0 ${WM_SETTEXT} 0 "STR:已经检测到您的计算机上安装了${PRODUCT_NAME}，现在进行的覆盖安装不能更改安装目录。如果您需要更改安装目录，请先卸载已经安装的版本之后再运行此安装程序！"

    ${EndIf}
FunctionEnd

/************************
* 区段组件描述
************************/

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${CoreFiles}            "安装 ${PRODUCT_NAME}"
    !insertmacro MUI_DESCRIPTION_TEXT ${Shortcuts}            "创建快捷方式"
    !insertmacro MUI_DESCRIPTION_TEXT ${ShortcutsDesktop}     "创建桌面快捷方式"
    ;!insertmacro MUI_DESCRIPTION_TEXT ${ShortcutsPrograms}    "创建开始菜单程序组快捷方式"
    ; !insertmacro MUI_DESCRIPTION_TEXT ${AddSystemContextMenu} "将 ${PRODUCT_NAME} 添加到系统右键菜单"
    ; !insertmacro MUI_DESCRIPTION_TEXT ${ReplaceSystemNotepad} "用 ${PRODUCT_NAME} 替换系统记事本程序"
!insertmacro MUI_FUNCTION_DESCRIPTION_END

/************************
* 卸载 Section 段
************************/

Section Uninstall
    SetDetailsPrint textonly
    DetailPrint "正在卸载${PRODUCT_NAME}..."
    SetDetailsPrint listonly

    # 进程清理
    !insertmacro ProcessCleanup
    
    
    
    Delete "$INSTDIR\*.*"
   
   
    # 删除安装文件
    Delete "$INSTDIR\${APPNAME}.exe"
    Delete "$INSTDIR\${APPNAME}.ini"
    
    Delete "$INSTDIR\uninst.exe"
    
    RMDir /r "$INSTDIR"
    RMDir "$INSTDIR"
     
    # 删除快捷方式
    SetShellVarContext current
    Delete "$DESKTOP\${APPNAMESRT}.lnk"

    SetShellVarContext current
    RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
   RMDir  /r "$PROFILE\naiyou"
    # 删除注册表
    SetRegView 64 
    DeleteRegKey HKLM "${PRODUCT_KEY_APPDIR}"
    DeleteRegKey HKLM "${PRODUCT_KEY_UNINST}"
    DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe"
    SetRegView lastused
    
    SetRegView 32 
    DeleteRegKey HKLM "${PRODUCT_KEY_APPDIR}"
    DeleteRegKey HKLM "${PRODUCT_KEY_UNINST}"
    DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe"
    SetRegView lastused
    
    DeleteRegKey HKCR "*\shell\${APPNAME}"
    
    # 刷新图标关联
    System::Call `shell32::SHChangeNotify(i 0x08000000, i 0, i 0, i 0)`

    SetAutoClose true
SectionEnd

/************************
* 卸载回调函数
************************/

Function un.onInit
    # 初始化插件目录
    InitPluginsDir

    # 创建互斥防止重复运行
    System::Call `kernel32::CreateMutex(i0,i0,t"${PRODUCT_NAME}_uninstaller")i.r1?e`
    Pop $R0
    StrCmp $R0 0 +3
    MessageBox MB_OK|MB_ICONEXCLAMATION "卸载程序已经运行！"
    Abort
FunctionEnd
