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

;检查重复运行
System::Call 'kernel32::CreateMutexA(i 0, i 0, t "MicroSetup") i .r1 ?e'


;;安装程序已经运行

StrCmp $R1 0 +3
;MessageBox MB_OK|MB_ICONINFORMATION|MB_TOPMOST "程序已经在运行。"
Abort
Push $0
Push $1
ReadRegStr $0 HKLM ${PRODUCT_UNINST_KEY} "UninstallString"
ReadRegStr $1 HKLM ${PRODUCT_UNINST_KEY} "DisplayVersion"
StrCmp $0 "" onInit.End

IfFileExists $0 0 onInit.End

;;杀死安装包启动的程序;;

KillProcDLL::KillProc "${PRODUCT_NAME}.exe"


MessageBox MB_YESNO|MB_ICONQUESTION "检测到系统已经安装了${PRODUCT_NAME} $1。$\r$\n继续之前必须先执行卸载,继续吗?" IDNO onInit.GoAbort

# 静默卸载
IfFileExists "$DESKTOP\${PRODUCT_NAME}.lnk" 0 +2
Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
IfFileExists "$SMPROGRAMS\${PRODUCT_NAME} $1\${PRODUCT_NAME}.lnk" 0 +4
Delete "$SMPROGRAMS\${PRODUCT_NAME} $1\${PRODUCT_NAME}.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME} $1\Uninstall.lnk"
RMDir "$SMPROGRAMS\${PRODUCT_NAME} $1"
IfFileExists "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" 0 +4
Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"

RMDir "$SMPROGRAMS\${PRODUCT_NAME}"
;;删除文件夹和文件;;

RMDir /r "$INSTDIR\platforms"
RMDir /r "$INSTDIR\imageformats"
RMDir /r "$INSTDIR\iconengines"
RMDir /r "$INSTDIR\bearer"
RMDir /r "$INSTDIR\*.*"
RMDir /r "$INSTDIR\..\${PRODUCT_NAME}"
RMDir /r "$INSTDIR"

MessageBox MB_OK "卸载完成!"
Goto Next
onInit.GoAbort:
Abort
onInit.End:
Pop $1
Pop $0
Next:
ExecShell "" "${PRODUCT_NAME}_${PRODUCT_VERSION}.exe"
FunctionEnd