!define PRODUCT_NAME "Wifi/Bluetooth Enabler"
!define PRODUCT_VERSION "1.0.0.0"
!define PRODUCT_SMALLNAME "WifiBTenabler"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_SMALLNAME}"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"


; MUI 1.67 compatible ------
!include "MUI.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "icons\installericon.ico"
;!define MUI_UNICON "uninstallericon.ico"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "readme.txt"

; Directory page
;with different directories it doesn't work so we don't allow it
;!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
;!define MUI_FINISHPAGE_RUN "$INSTDIR\${PRODUCT_SMALLNAME}.exe"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------


;Silent Installing - no windows
;SilentInstall silent

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "..\${PRODUCT_SMALLNAME}-Setup.exe"

!include WordFunc.nsh
!include LogicLib.nsh

;the program files path in other languages is not working
InstallDir "$PROGRAMFILES\${PRODUCT_SMALLNAME}"

;InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""

ShowInstDetails hide
ShowUnInstDetails show


Function .onInit

FunctionEnd

Section "InstallationSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
  File "readme.txt"
  File "wifiOn.exe"
  File "wifiOff.exe"
  File "BlueToothOn.exe"
  File "BlueToothOff.exe"
  File "files\*"
  
  
  CreateDirectory "$SMPROGRAMS\${PRODUCT_SMALLNAME}"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_SMALLNAME}\wifiOn.lnk" "$INSTDIR\wifiOn.exe"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_SMALLNAME}\wifiOff.lnk" "$INSTDIR\wifiOff.exe"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_SMALLNAME}\BlueToothOn.lnk" "$INSTDIR\BlueToothOn.exe"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_SMALLNAME}\BlueToothOff.lnk" "$INSTDIR\BlueToothOff.exe"
  
  ;CreateShortCut "$DESKTOP\${PRODUCT_SMALLNAME}.lnk" "$INSTDIR\${PRODUCT_SMALLNAME}.exe"

;WriteRegStr HKEY_CURRENT_USER "Software\Synaptics\SynTP\TouchPadPS2" "ExButton2PlugInID" "SynTP"
;WriteRegDWORD HKEY_CURRENT_USER "Software\Synaptics\SynTP\TouchPadPS2" "ExButton2Action" 0x0
;WriteRegDWORD HKEY_CURRENT_USER "Software\Synaptics\SynTP\TouchPadPS2" "ExButton2PlugInActionID" 0x5
;WriteRegStr HKEY_CURRENT_USER "Software\Synaptics\SynTPEnh\PlugInConfig\TouchPadPS2" "ExButton2RunApp" "$PROGRAMFILES\SwapResolution\SwapResolution.exe"
;WriteRegDWORD HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Services\ialm\Device0" "Display1_DownScalingEnabled" 0x1

;installing driver
;nsExec::Exec "$EXEDIR\ioperm.exe -i"

SectionEnd

Section -AdditionalIcons
  CreateShortCut "$SMPROGRAMS\${PRODUCT_SMALLNAME}\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  ;WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\${PRODUCT_SMALLNAME}.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\${PRODUCT_SMALLNAME}.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\wifion.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
SectionEnd


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  ;removing the driver before the files
  nsExec::Exec "$instdir\ioperm.exe -u"
  
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\readme.txt"
  Delete "$INSTDIR\wifiOn.exe"
  Delete "$INSTDIR\wifiOff.exe"
  Delete "$INSTDIR\BlueToothOn.exe"
  Delete "$INSTDIR\BlueToothOff.exe"
  
  Delete "$INSTDIR\cygioperm-0.dll"
  Delete "$INSTDIR\cygpopt-0.dll"
  Delete "$INSTDIR\cygwin1.dll"
  Delete "$INSTDIR\hsect2.exe"
  Delete "$INSTDIR\ioperm.exe"
  Delete "$INSTDIR\ioperm.sys"
  
  
  Delete "$SMPROGRAMS\${PRODUCT_SMALLNAME}\Uninstall.lnk"
 ; Delete "$DESKTOP\${PRODUCT_SMALLNAME}.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_SMALLNAME}\wifiOn.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_SMALLNAME}\wifiOff.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_SMALLNAME}\BlueToothOn.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_SMALLNAME}\BlueToothOff.lnk"

  
  RMDir "$SMPROGRAMS\${PRODUCT_SMALLNAME}"
  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  SetAutoClose true
SectionEnd