Unicode True
SetCompressor /SOLID lzma
SetCompressorDictSize 64
RequestExecutionLevel user

!include "MUI2.nsh"

!define PROJECT_ROOT "."
!define DIST_ROOT "${PROJECT_ROOT}\dist\pqAutomationApp"
!define APP_EXE "pqAutomationApp.exe"
!define APP_ID "PQAutomationApp"

!define PYTHON_CMD "python"

!system '"${PYTHON_CMD}" -c "import app_version; print(app_version.APP_NAME)" > app_name.txt'
!system '"${PYTHON_CMD}" -c "import app_version; print(app_version.APP_VERSION)" > app_version.txt'

!searchparse /file "app_name.txt" "" APP_NAME ""
!searchparse /file "app_version.txt" "" APP_VERSION ""

!delfile "app_name.txt"
!delfile "app_version.txt"

!if /FileExists "${DIST_ROOT}\${APP_EXE}"
!else
  !error "Executable not found: ${DIST_ROOT}\${APP_EXE}. Please build using PyInstaller first."
!endif

!define UNINSTALL_REG_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_ID}"
!define APP_REG_KEY "Software\${APP_ID}"

Name "${APP_NAME} ${APP_VERSION}"
OutFile "dist\PQAutomationApp_Setup_${APP_VERSION}.exe"
InstallDir "$LOCALAPPDATA\Programs\${APP_ID}"
InstallDirRegKey HKCU "${APP_REG_KEY}" "InstallDir"

!define MUI_ABORTWARNING
!define MUI_ICON "assets\pq.ico"
!define MUI_UNICON "assets\pq.ico"

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

!insertmacro MUI_LANGUAGE "SimpChinese"

Section "Main Installation" SEC_MAIN
  SetOutPath "$INSTDIR"

  File "${DIST_ROOT}\${APP_EXE}"

  SetOutPath "$INSTDIR\internal"
  File /r /x "*.pdb" /x "*.lib" /x "*.exp" /x "*.h" /x "__pycache__" /x "*.pyc" "${DIST_ROOT}\internal\*.*"

  SetOutPath "$INSTDIR\settings"

  IfFileExists "$INSTDIR\settings\pq_config.json" +2 0
    File /oname=pq_config.json "${PROJECT_ROOT}\settings\pq_config.json"

  IfFileExists "$INSTDIR\settings\pantone_2670_colors.xlsx" +2 0
    File /oname=pantone_2670_colors.xlsx "${PROJECT_ROOT}\settings\pantone_2670_colors.xlsx"

  WriteUninstaller "$INSTDIR\Uninstall.exe"

  WriteRegStr HKCU "${APP_REG_KEY}" "InstallDir" "$INSTDIR"
  WriteRegStr HKCU "${UNINSTALL_REG_KEY}" "DisplayName" "${APP_NAME}"
  WriteRegStr HKCU "${UNINSTALL_REG_KEY}" "DisplayVersion" "${APP_VERSION}"
  WriteRegStr HKCU "${UNINSTALL_REG_KEY}" "InstallLocation" "$INSTDIR"
  WriteRegStr HKCU "${UNINSTALL_REG_KEY}" "DisplayIcon" "$INSTDIR\${APP_EXE}"
  WriteRegStr HKCU "${UNINSTALL_REG_KEY}" "Publisher" "Moka"
  WriteRegStr HKCU "${UNINSTALL_REG_KEY}" "UninstallString" "$\"$INSTDIR\Uninstall.exe$\""
  WriteRegDWORD HKCU "${UNINSTALL_REG_KEY}" "NoModify" 1
  WriteRegDWORD HKCU "${UNINSTALL_REG_KEY}" "NoRepair" 1

  CreateDirectory "$SMPROGRAMS\${APP_NAME}"
  CreateShortcut "$SMPROGRAMS\${APP_NAME}\${APP_NAME}.lnk" "$INSTDIR\${APP_EXE}"
  CreateShortcut "$DESKTOP\${APP_NAME}.lnk" "$INSTDIR\${APP_EXE}"
SectionEnd

Section "Uninstall"
  Delete "$DESKTOP\${APP_NAME}.lnk"
  Delete "$SMPROGRAMS\${APP_NAME}\${APP_NAME}.lnk"
  RMDir "$SMPROGRAMS\${APP_NAME}"

  Delete "$INSTDIR\${APP_EXE}"
  Delete "$INSTDIR\Uninstall.exe"
  RMDir /r "$INSTDIR\internal"
  RMDir /r "$INSTDIR\settings"
  RMDir "$INSTDIR"

  DeleteRegKey HKCU "${UNINSTALL_REG_KEY}"
  DeleteRegKey HKCU "${APP_REG_KEY}"
SectionEnd