;; Installer script file for PCB for windows.  See http://nsis.sourceforge.net
;; for details on NSIS.
;;

; Started with:
;
;NSIS Modern User Interface
;Start Menu Folder Selection Example Script
;Written by Joost Verburg
;
; and modified for wcalc by Dan McMahill
;
; Later copied for use by PCB by Dan McMahill

;--------------------------------
;Include Modern UI

  !include "MUI.nsh"


;--------------------------------
;Include macros for associating file 
;extensions
!include "registerExtension.nsh"


;General

  ;Name and file
  Name "PCB-@prog_version@"
  OutFile "pcbinst-@prog_version@.exe"


  ;See http://nsis.sourceforge.net/Shortcuts_removal_fails_on_Windows_Vista
  ;for more details.
  RequestExecutionLevel admin

  ;Default installation folder
  InstallDir "$PROGRAMFILES\PCB-@prog_version@"
  
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\pcb-@prog_version@" ""

;--------------------------------
;Variables

  Var MUI_TEMP
  Var STARTMENU_FOLDER

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING

;--------------------------------
;Pages

  !insertmacro MUI_PAGE_LICENSE "../COPYING"

  !include "license_include.nsh"
;  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  
  ;Start Menu Folder Page Configuration
  !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKCU" 
  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\PCB-@prog_version@" 
  !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
  
  !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  
  !insertmacro MUI_PAGE_INSTFILES
  
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Installer Sections

Section "Dummy Section" SecDummy

  SetOutPath "$INSTDIR"
  SetShellVarContext all

  
  ;ADD YOUR OWN FILES HERE...
  ;;File /a /r "someother_dir/*.*"
  File /a /r "result/*.*"

  ;Associate file types and icons
  ${registerExtension} "$INSTDIR\bin\pcb.exe" "" ".pcb" "PCB Layout File"
  ${registerExtension} "$INSTDIR\bin\pcb.exe" "" ".fp" "PCB Footprint File"
 

  ;Store installation folder
  WriteRegStr HKCU "Software\pcb-@prog_version@" "" $INSTDIR
  
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"

  ;Generate GDK loaders cache
  ExecWait '"$INSTDIR\bin\gdk-pixbuf-query-loaders.exe" "--update-cache"'

  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    
    ;Create shortcuts
    CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\HTML Manual.lnk" "$INSTDIR\doc\html\pcb.html"
    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\PDF Manual.lnk" "$INSTDIR\doc\pdf\pcb.pdf"
    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Readme.lnk" "$INSTDIR\doc\Readme.txt"

    SetShellVarContext current
    SetOutPath "$DOCUMENTS"
    SetShellVarContext all

    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\PCB.lnk" "$INSTDIR\bin\pcb.exe" "" "$INSTDIR\bin\pcb.exe" 0 "" "" "PCB Printed Circuit Board Layout Editor"
    SetOutPath "$INSTDIR"

;; CreateShortCut  link.lnk target.file [parameters [icon.file [icon_index_number [start_options [keyboard_shortcut [description]]]]]]

  !insertmacro MUI_STARTMENU_WRITE_END

SectionEnd

;--------------------------------
;Descriptions

  ;Language strings
;  LangString DESC_SecDummy ${LANG_ENGLISH} "A test section."

  ;Assign language strings to sections
;  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
;    !insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
;  !insertmacro MUI_FUNCTION_DESCRIPTION_END

;--------------------------------
;Uninstaller Section

Section "Uninstall"
  SetShellVarContext all

  ;ADD YOUR OWN FILES HERE...

  ${unregisterExtension} ".pcb" "PCB Layout File"
  ${unregisterExtension} ".fp"  "PCB Footprint File"

  Delete "$INSTDIR\Uninstall.exe"
  Delete "$INSTDIR\*.*"

  RMDir /r "$INSTDIR\*.*"
  RMDir "$INSTDIR"
  
  !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
    
  Delete "$SMPROGRAMS\$MUI_TEMP\HTML Manual.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\PCB.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\PDF Manual.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\Readme.lnk"
  Delete "$SMPROGRAMS\$MUI_TEMP\Uninstall.lnk"
  
  ;Delete empty start menu parent diretories
  StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
 
  startMenuDeleteLoop:
	ClearErrors
    RMDir $MUI_TEMP
    GetFullPathName $MUI_TEMP "$MUI_TEMP\.."
    
    IfErrors startMenuDeleteLoopDone
  
    StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
  startMenuDeleteLoopDone:

  DeleteRegKey /ifempty HKCU "Software\pcb-@prog_version@"

SectionEnd
