!define PRODUCT_NAME "E8810Driver"
!define PRODUCT_VERSION "v1.05"
!define PRODUCT_PUBLISHER "Eastman Kodak"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\E8810Driver.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_BETA "*Beta*"
!define PRODUCT_REGISTRY_ENTRY "SYSTEM\CurrentControlSet\Enum\USBPRINT\Eastman_Kodak_CompanyKODAK_8810_Photo_Printer"
!define PRODUCT_DRIVER_NAME "KODAK 8810 Printer"
!define PRODUCT_PRINTER_NAME "KODAK 8810 Digital Photo Printer"
!define PRODUCT_REGKEY "SOFTWARE\Kodak\E8810"

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

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "Kodak.ico"
!define MUI_UNICON "Kodak.ico"

; Language Selection Dialog Settings
!define MUI_LANGDLL_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_LANGDLL_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_LANGDLL_REGISTRY_VALUENAME "NSIS:Language"

; Welcome page
!insertmacro MUI_PAGE_WELCOME

; License page
!insertmacro MUI_PAGE_LICENSE "license.txt"

; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
;;;;;!define MUI_FINISHPAGE_RUN "$INSTDIR\E8810Driver.exe"
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"
;!insertmacro MUI_LANGUAGE "French"
;!insertmacro MUI_LANGUAGE "German"
;!insertmacro MUI_LANGUAGE "Spanish"
;!insertmacro MUI_LANGUAGE "Italian"
;!insertmacro MUI_LANGUAGE "Russian"
;!insertmacro MUI_LANGUAGE "Romanian"
;!insertmacro MUI_LANGUAGE "SimpChinese"

; MUI end ------

Name "${PRODUCT_PRINTER_NAME} ${PRODUCT_VERSION} ${PRODUCT_BETA}"
OutFile "${PRODUCT_NAME}_${PRODUCT_VERSION}.exe"
InstallDir "$PROGRAMFILES\EK8810"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

var PORTNAME

Function isOSSupported
  ;check the OS for giving warning
  File "i386\getOs.exe"
  SetOutPath "$INSTDIR\i386\Microsoft.VC90.CRT"
  File "Microsoft.VC90.CRT\msvcr90.dll"
  File "Microsoft.VC90.CRT\msvcp90.dll"
  File "Microsoft.VC90.CRT\Microsoft.VC90.CRT.manifest"
  File "Microsoft.VC90.CRT\msvcm90.dll"
  
  nsExec::Exec '$INSTDIR\i386\getOs.exe'
  Pop $0 # return value/error/timeout
  DetailPrint "return of getOs = $0"
  ;;;;;; ; WINXP SP2 < 32     ;;;;;;;  < 32
  ${If} $0 < 32
    MessageBox MB_YESNO "${PRODUCT_PRINTER_NAME} will require MINIMUM OS 'Windows XP with SP2' to work properly. Do you wish to continue the setup?" IDYES nooscheckexit
    Delete /REBOOTOK "$INSTDIR\i386\getOs.exe"
    Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\msvcr90.dll"
    Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\msvcp90.dll"
    Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\Microsoft.VC90.CRT.manifest"
    Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\msvcm90.dll"
    Quit
  nooscheckexit:
  Delete /REBOOTOK "$INSTDIR\i386\getOs.exe"
  Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\msvcr90.dll"
  Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\msvcp90.dll"
  Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\Microsoft.VC90.CRT.manifest"
  Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC90.CRT\msvcm90.dll"
  ${EndIf}  
FunctionEnd  

Function isPrinterConnected
  StrCpy $2 0
  loop:
    EnumRegKey $1 HKLM ${PRODUCT_REGISTRY_ENTRY} $2
    StrCmp $1 "" done
    ReadRegStr $PORTNAME HKLM "${PRODUCT_REGISTRY_ENTRY}\$1\Device Parameters" "PortName"
    StrCmpS $PORTNAME "" 0 return
    IntOp $2 $2 + 1
    Goto loop
  done:
    MessageBox MB_OK|MB_ICONSTOP "Installer could not find ${PRODUCT_PRINTER_NAME}. Please connect to USB Port, switch 'ON' and then restart the installation process again."
    Quit
  return:
    
FunctionEnd

Function .onInit

  !insertmacro MUI_LANGDLL_DISPLAY
  Call isPrinterConnected
  
FunctionEnd

Section "MainSection" SEC01
  SetOutPath "$INSTDIR\i386"
  SetOverwrite ifnewer
  
  Call isOSSupported  
  
  SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
  File "license.txt"
  File "E8810_ReadMe_BETA.txt"  
  file "vcredist_x86.exe"
  File "i386\KODAKPRC.dll"
  File "i386\QtCore4.dll"
  File "i386\QtGui4.dll"
  File "i386\msvcr80.dll"
  File "i386\base.bmp"
  SetOutPath "$INSTDIR\i386"
  File "i386\E8810bm.dll"
  File "i386\E8810ui.dll"
  File "i386\E8810bm.gpd"
  File "i386\E8810bm.ini"
  File "i386\E8810gpd.dll"
  File "i386\E8810bmxp.inf"
  File "i386\E8810bm.inf"
  File "i386\E8810localmon.dll"
  File "i386\E8810localui.dll"
  File "i386\E8810rc.gpd"
  File "i386\InstallMonitorDriver.exe"
  File "i386\Microsoft.VC80.CRT.manifest"
  File "i386\msvcr80.dll"
  File "i386\Printer_Forms_Reg.reg"
  File "i386\stdnames.gpd"
  File "i386\uninstall.bat"
  File "i386\EK8810.dll"
  File "i386\EK8810ColorMatch.dll"
  File "i386\EK8810UsbCtrl.dll"
  
  ;remove printer
  ExecWait 'rundll32 printui.dll,PrintUIEntry /dl /n "${PRODUCT_DRIVER_NAME}" /q'
  sleep 1
  ;install port
  DetailPrint "Stoping Print Spooler service"
  nsExec::Exec "net stop Spooler"
  sleep 2
  CopyFiles	"$INSTDIR\i386\E8810localmon.dll" "$SYSDIR\E8810localmon.dll"
  CopyFiles	"$INSTDIR\i386\E8810localui.dll" "$SYSDIR\E8810localui.dll"
  CopyFiles	"$INSTDIR\i386\EK8810.dll" "$SYSDIR\EK8810.dll"
  CopyFiles	"$INSTDIR\i386\EK8810ColorMatch.dll" "$SYSDIR\EK8810ColorMatch.dll"
  CopyFiles	"$INSTDIR\i386\EK8810UsbCtrl.dll" "$SYSDIR\EK8810UsbCtrl.dll"
  CopyFiles	"$INSTDIR\QtCore4.dll" "$SYSDIR\QtCore4.dll"
  CopyFiles	"$INSTDIR\QtGui4.dll" "$SYSDIR\QtGui4.dll"
  CopyFiles "$INSTDIR\msvcr80.dll" "$SYSDIR\msvcr80.dll"
  DetailPrint "Restarting Print Spooler service"
  nsExec::Exec "sc config Spooler start= auto"
  nsExec::Exec "net start Spooler"
  sleep 3
  DetailPrint "Instaling E8810 Print Monitor"
  SetOutPath "$INSTDIR\i386"
  ExecWait "$INSTDIR\i386\InstallMonitorDriver.exe" $0 ; for AddMonitor
  
  ${If} $0 <= 8   ; if winxp/w2003/wVista then use USBPRINT port
    ${If} $0 <= 6 
      ; VS2008 redistributable.
      MessageBox MB_YESNO "${PRODUCT_PRINTER_NAME} will require vc 2008 redistributable files. Please select 'No', if it is already installed earlier. Do you wish to run the 'vcredist_x86' installer?" IDNO noinstall
        Execwait "$INSTDIR\vcredist_x86.exe"
      noinstall:
    ${EndIf}
    ;<shs> it is already installed in "InstallmonitorDriver" application, so no need to install again.
    ;DetailPrint "Instaling E8810 Print Driver (with USBPrint port)"
    ;ExecWait 'rundll32 printui.dll,PrintUIEntry /if /b "${PRODUCT_DRIVER_NAME}" /f E8810bmxp.inf /r "$PORTNAME" /m "${PRODUCT_DRIVER_NAME}"'
  ${Else}
    sleep 1
    DetailPrint "Instaling E8810 Print Driver pnputility"
    ;ExecWait 'pnputil -a E8810bm.inf'
    nsExec::Exec 'pnputil -a E8810bm.inf'
    Pop $0 # return value/error/timeout
    DetailPrint "     pnputility:$0      "
    sleep 3
    DetailPrint "Instaling E8810 Print Driver"
    ExecWait 'rundll32 printui.dll,PrintUIEntry /if /b "${PRODUCT_DRIVER_NAME}" /f E8810bm.inf /r "E8810:" /m "${PRODUCT_DRIVER_NAME}"'
  ${EndIf}

  SetRebootFlag true
SectionEnd

Section -Post
  ;DetailPrint "Installing E8810 Forms"
  WriteRegBin HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms" "8 x 10 in (E8810)" "eb2e030020e803000000000000000000eb2e030020e803000F00000002000000"
  WriteRegBin HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms" "8 x 12 in (E8810)" "eb2e030090ae04000000000000000000eb2e030090ae04000F00000002000000"
  WriteRegBin HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms" "4 x 8 in (E8810)" "76970100eb2e0300000000000000000076970100eb2e03000F00000002000000"
  
  ;DetailPrint "SHS ***** Adding Uninst.exe ******"
  WriteUninstaller "$INSTDIR\uninst.exe"
  ;WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\E8810Driver.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\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  
  ;DetailPrint "SHS ***** Adding Registry entries ******"
  WriteRegStr HKCU "${PRODUCT_REGKEY}" "Path" "$INSTDIR"
  WriteRegStr HKCU "${PRODUCT_REGKEY}\Operations" "Sequence" "1,2"
  WriteRegDWORD HKCU "${PRODUCT_REGKEY}\Operations" "Sharpness" 0x00 
  WriteRegDWORD HKCU "${PRODUCT_REGKEY}\Operations" "XtraLife" 0x01 
  WriteRegDWORD HKCU "${PRODUCT_REGKEY}\Operations" "CutPageInHalf" 0x00  
  WriteRegDWORD HKCU "${PRODUCT_REGKEY}\Operations" "ScalePercent" 0x64
  
  WriteRegStr HKU ".DEFAULT\${PRODUCT_REGKEY}" "Path" "$INSTDIR"
  WriteRegStr HKU ".DEFAULT\${PRODUCT_REGKEY}\Operations" "Sequence" "1,2"
  WriteRegDWORD HKU ".DEFAULT\${PRODUCT_REGKEY}\Operations" "Sharpness" 0x00 
  WriteRegDWORD HKU ".DEFAULT\${PRODUCT_REGKEY}\Operations" "XtraLife" 0x01 
  WriteRegDWORD HKU ".DEFAULT\${PRODUCT_REGKEY}\Operations" "CutPageInHalf" 0x00  
  WriteRegDWORD HKU ".DEFAULT\${PRODUCT_REGKEY}\Operations" "ScalePercent" 0x64

  WriteRegStr HKLM "${PRODUCT_REGKEY}" "Path" "$INSTDIR"
  ;DetailPrint "SHS ***** completed registry adding ******"
SectionEnd

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

Section -NowReboot
  DetailPrint "Installation completed"
  SetOutPath "$INSTDIR"
  IfRebootFlag 0 noreboot
    MessageBox MB_YESNO "${PRODUCT_PRINTER_NAME} has been installed. A reboot is required to finish the installation. Do you wish to reboot now?" IDNO noreboot
      Reboot
  noreboot:
SectionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "Uninstallation program $(^Name) successful."
  IfRebootFlag 0 noreboot
    MessageBox MB_YESNO "A reboot is required to finish the Uninstallation. Do you wish to reboot now?" IDNO noreboot
      Reboot
  noreboot:
FunctionEnd

Function un.onInit
!insertmacro MUI_UNGETLANGUAGE
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Uninstall program $(^Name) ?" IDYES +2
  Abort
FunctionEnd

Section Uninstall

ExecWait 'rundll32 printui.dll,PrintUIEntry /dl /n "${PRODUCT_DRIVER_NAME}" /q'
ExecWait 'rundll32 printui.dll,PrintUIEntry /dl /n "${PRODUCT_DRIVER_NAME}" /q'
ExecWait 'rundll32 printui.dll,PrintUIEntry /dl /n "${PRODUCT_DRIVER_NAME}" /q'
sleep 2
DetailPrint "Stoping Print Spooler service"
nsExec::Exec "net stop Spooler"
DetailPrint "Delete files"
delete	/REBOOTOK "$SYSDIR\E8810localmon.dll"
delete	/REBOOTOK "$SYSDIR\E8810localui.dll"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810bm.bud"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810bm.dll"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810ui.dll"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810bm.gpd"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810gpd.dll"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810rc.gpd"
delete	/REBOOTOK "$SYSDIR\spool\drivers\w32x86\3\E8810bm.ini"
delete	/REBOOTOK "$SYSDIR\EK8810.dll"
delete	/REBOOTOK "$SYSDIR\EK8810ColorMatch.dll"
delete	/REBOOTOK "$SYSDIR\EK8810UsbCtrl.dll"

DetailPrint "delete printer ports to registry"

DeleteRegValue HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port\Ports" "E8810"
DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port\Ports"
DeleteRegValue HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port" "Driver"
DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port"

DetailPrint "delete printer forms to registry"

DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms\8 x 10 in (E8810)" "FormKeyword"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms\8 x 10 in (E8810)" "ResourceNameID"
DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port\8 x 10 in (E8810)"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms" "8 x 10 in (E8810)"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms\8 x 12 in (E8810)" "FormKeyword"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms\8 x 12 in (E8810)" "ResourceNameID"
DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port\8 x 12 in (E8810)"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms" "8 x 12 in (E8810)"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms\4 x 8 in (E8810)" "FormKeyword"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms\4 x 8 in (E8810)" "ResourceNameID"
DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Control\Print\Monitors\E8810 Local Port\4 x 8 in (E8810)"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Print\Forms" "4 x 8 in (E8810)"

DetailPrint "Restarting Print Spooler service"
nsExec::Exec "net start Spooler"
sleep 1

  Delete /REBOOTOK "$INSTDIR\readme.txt"
  Delete /REBOOTOK "$INSTDIR\license.txt"
  Delete /REBOOTOK "$INSTDIR\uninst.exe"
  Delete /REBOOTOK "$INSTDIR\i386\E8810bm.dll"
  Delete /REBOOTOK "$INSTDIR\i386\E8810ui.dll"
  Delete /REBOOTOK "$INSTDIR\i386\E8810bm.gpd"
  Delete /REBOOTOK "$INSTDIR\i386\E8810bm.inf"
  Delete /REBOOTOK "$INSTDIR\i386\E8810bmxp.inf"
  Delete /REBOOTOK "$INSTDIR\i386\E8810bm.ini"
  Delete /REBOOTOK "$INSTDIR\i386\E8810gpd.dll"
  Delete /REBOOTOK "$INSTDIR\i386\E8810localmon.dll"
  Delete /REBOOTOK "$INSTDIR\i386\E8810localui.dll"
  Delete /REBOOTOK "$INSTDIR\i386\E8810rc.gpd"
  Delete /REBOOTOK "$INSTDIR\i386\InstallMonitorDriver.exe"
  Delete /REBOOTOK "$INSTDIR\i386\Microsoft.VC80.CRT.manifest"
  Delete /REBOOTOK "$INSTDIR\i386\msvcr80.dll"
  Delete /REBOOTOK "$INSTDIR\i386\msvcr90.dll"
  Delete /REBOOTOK "$INSTDIR\i386\msvcp90.dll"
  Delete /REBOOTOK "$INSTDIR\i386\Printer_Forms_Reg.reg"
  Delete /REBOOTOK "$INSTDIR\i386\stdnames.gpd"
  Delete /REBOOTOK "$INSTDIR\i386\uninstall.bat"
  Delete /REBOOTOK "$INSTDIR\i386\EK8810.dll"
  Delete /REBOOTOK "$INSTDIR\i386\EK8810ColorMatch.dll"
  Delete /REBOOTOK "$INSTDIR\i386\EK8810UsbCtrl.dll"
  Delete /REBOOTOK "$SMPROGRAMS\${PRODUCT_PRINTER_NAME}\Uninstall.lnk"  
  Delete /REBOOTOK "$INSTDIR\KODAKPRC.dll"
  Delete /REBOOTOK "$INSTDIR\msvcr80.dll"
  Delete /REBOOTOK "$INSTDIR\msvcr90.dll"
  Delete /REBOOTOK "$INSTDIR\msvcp90.dll"
  Delete /REBOOTOK "$INSTDIR\QtCore4.dll"
  Delete /REBOOTOK "$INSTDIR\QtGui4.dll"
  Delete /REBOOTOK "$INSTDIR\base.bmp"
  sleep 1
  RMDir /r /REBOOTOK "$SMPROGRAMS\${PRODUCT_PRINTER_NAME}"
  RMDir /r /REBOOTOK "$INSTDIR\i386"
  RMDir /r /REBOOTOK "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  
  DeleteRegKey HKCU "${PRODUCT_REGKEY}"
  DeleteRegKey HKLM "${PRODUCT_REGKEY}"
  DeleteRegKey HKU ".DEFAULT\${PRODUCT_REGKEY}"
  SetRebootFlag true
  SetAutoClose true
SectionEnd























