; Script generated by the HM NIS Edit Script Wizard.

; a temporary fixed hack to not make guile, will be variable at some point
; Could make with a guile version
!define WITH_GUILE
; Do not make a guile version
!undef WITH_GUILE

;for bat change
!include "ReplaceOnLine.nsh"
!include "AdvReplaceInFile.nsh"
!include "TextCompare.nsh"

; for logicals
!include "LogicLib.nsh"

; for command line options
!include FileFunc.nsh
!insertmacro GetParameters
!insertmacro GetOptions

; for removal of installer
!include "StrStr.nsh"

; to detect windows version
!include "WinVer.nsh"

; pre setting of Coot version
!include "${src_dir}\coot-version"

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "WinCoot"
!define PRODUCT_VERSION "${WinCootVersion}"
!define PRODUCT_PUBLISHER "Bernhard Lohkamp & Paul Emsley"
!define PRODUCT_WEB_SITE "http://www.ysbl.york.ac.uk/~lohkamp/coot/wincoot.html"
!define PRODUCT_WEB_SITE_2 "http://www2.mrc-lmb.cam.ac.uk/personal/pemsley/coot/"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\uninst.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"

SetCompressor lzma

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

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install-colorful.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall-colorful.ico"

; Some variables
Var ICONS_GROUP
Var STARTDIR

; Welcome page (allow 3 lines if long pre-release)
!define MUI_WELCOMEPAGE_TITLE_3LINES
!define MUI_WELCOMEFINISHPAGE_BITMAP_NOSTRETCH
!define MUI_WELCOMEFINISHPAGE_BITMAP "C:\MinGW\msys\1.0\home\bernhard\installer\coot_pic_welcome.bmp"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP_NOSTRETCH
!define MUI_HEADERIMAGE_BITMAP "C:\MinGW\msys\1.0\home\bernhard\installer\coot_pic_header.bmp"
!define MUI_PAGE_CUSTOMFUNCTION_PRE InitPreFunction
!insertmacro MUI_PAGE_WELCOME
; License page
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckForUpdate
!insertmacro MUI_PAGE_LICENSE "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\COPYING"
; Components page
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckForUpdate
!insertmacro MUI_PAGE_COMPONENTS
; Directory page (install directory)
!define MUI_PAGE_CUSTOMFUNCTION_PRE ComponentPost
!define MUI_PAGE_CUSTOMFUNCTION_SHOW DirectoryShow
!insertmacro MUI_PAGE_DIRECTORY
; Start menu page
!define MUI_STARTMENUPAGE_NODISABLE
!define MUI_STARTMENUPAGE_DEFAULTFOLDER "WinCoot"
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}"
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckForUpdate
!insertmacro MUI_PAGE_STARTMENU Application $ICONS_GROUP
; Directory page to set the start directory
!define MUI_DIRECTORYPAGE_VARIABLE $STARTDIR
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION $STARTDIR
!define MUI_PAGE_CUSTOMFUNCTION_SHOW DirectoryShow
!define MUI_PAGE_CUSTOMFUNCTION_PRE CheckForUpdate
!insertmacro MUI_PAGE_DIRECTORY

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
; run the bat file changes and gdk-pixbuf before we finish the installation
!define MUI_PAGE_CUSTOMFUNCTION_PRE FinishPagePreFunction
!define MUI_FINISHPAGE_RUN "$INSTDIR\runwincoot.bat"
;!define MUI_FINISHPAGE_RUN_PARAMETERS '/c "$INSTDIR\runwincoot.bat"'
!define MUI_FINISHPAGE_TITLE_3LINES
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English_WinCoot"
;!include "${NSISDIR}\Contrib\Language Files\English_WinCoot.nsh"

; for replacing strings
!include "WordFunc.nsh"
!insertmacro WordReplace

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"

!ifndef binary_dir
; default to nightlies output dir
!define binary_dir "C:\MinGW\msys\1.0\home\bernhard\public_html\software\binaries\nightlies\pre-release"
!endif
; just in case something goes wrong, we define src to be pre-release (default)
!ifndef src_dir
!define src_dir "C:\MinGW\msys\1.0\home\bernhard\autobuild\MINGW32_NT-6.1-bernie-pc-pre-release-gtk2"
!endif
!define outputname "${binary_dir}\${PRODUCT_NAME}-${PRODUCT_VERSION}.exe"
OutFile "${outputname}"

XPStyle on

InstallDir "C:\WinCoot"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show



#####################
# SECTIONS
#####################

Section "!WinCoot" SEC01
  SectionIn RO
  ; first check for Admin rights and if we shall install for all users
  ; change logic here. Install for everyone if possible otherwise only
  ; for current user. Done later in Shortcut section. Based on LilyPond
  ;Var /GLOBAL install_all_users
  ;StrCpy $install_all_users "False"
  ;userInfo::getAccountType
  ;pop $0
  ;StrCmp $0 "Admin" +2
  ;Goto install_single_user
  ; if silent installer, install for all
  ;IfSilent install_all_users
  ;messageBox MB_ICONQUESTION|MB_YESNO "Detected Administrator rights.$\nDo you want to install WinCoot for all Users?" IDYES install_all_users IDNO install_single_user
  ;install_all_users:
  ;  SetShellVarContext all
  ;  StrCpy $install_all_users "True"
  ;  Goto continue_user
  ;install_single_user:
  ;  SetShellVarContext current
  ;continue_user:
  
;  !insertmacro BIMAGE "C:\MinGW\msys\1.0\home\bernhard\installer\coot_pic.bmp" /RESIZETOFIT
  SetOverwrite ifnewer
  SetOutPath "$INSTDIR"
  
; check if runwincoot.bat exists, if so ask if overwrite (or not)
; New logic:
; check if runwincoot.bat exists, if so make a backup copy
; (this is a tmp file, doesnt seem to be deleted, maybe virus thing? not sure what's happening)
; install new runwincoot.bat
; deal with rest in FinishPagePreFunction
  Var /GLOBAL have_bat
  StrCpy $have_bat "False"

  IfFileExists "$INSTDIR\runwincoot.bat" "" endifbat
    StrCpy $have_bat "True"
  endifbat:

  SetOverwrite on
  File /oname=$INSTDIR\runwincoot.bat.tmp "C:\MinGW\msys\1.0\home\bernhard\Projects\coot\windows\runwincoot.bat"

  SetOverwrite ifnewer
; libexec DIR
  SetOutPath "$INSTDIR\libexec"
  SetOverwrite on
  File "${src_dir}\libexec\coot-bin.exe"
  File "${src_dir}\libexec\density-score-by-residue-bin.exe"
  File "${src_dir}\libexec\findligand-bin.exe"
  File "${src_dir}\libexec\findwaters-bin.exe"
  File "${src_dir}\libexec\mini-rsr-bin.exe"
  SetOverwrite ifnewer
; bin DIR
  SetOutPath "$INSTDIR\bin"
  SetOverwrite ifnewer
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\coot-icon.ico"
  File "${src_dir}\bin\*.dll"
  File "${src_dir}\bin\bfactan.exe"
  File "${src_dir}\bin\coot"
  File "${src_dir}\bin\coot-mini-rsr"
  File "${src_dir}\bin\coot-available-comp-id.exe"
  File "${src_dir}\bin\coot-compare-dictionaries.exe"
  File "${src_dir}\bin\coot-dictionary-bond-distributions.exe"
  File "${src_dir}\bin\coot-make-shelx-restraints.exe"
  File "${src_dir}\bin\density-score-by-residue"
  File "${src_dir}\bin\findligand"
  File "${src_dir}\bin\findwaters"
  File "${src_dir}\bin\fix-nomenclature-errors.exe"
  File "${src_dir}\bin\gdk-pixbuf-csource.exe"
  File "${src_dir}\bin\gdk-pixbuf-query-loaders.exe"
  File "${src_dir}\bin\glib-genmarshal.exe"
  File "${src_dir}\bin\glib-gettextize"
  File "${src_dir}\bin\glib-mkenums"
  File "${src_dir}\bin\gobject-query.exe"
  File "${src_dir}\bin\gsl-config"
  File "${src_dir}\bin\gspawn-win32-helper-console.exe"
  File "${src_dir}\bin\gspawn-win32-helper.exe"
  File "${src_dir}\bin\gtk-builder-convert"
  File "${src_dir}\bin\gtk-demo.exe"
  File "${src_dir}\bin\gtk-query-immodules-2.0.exe"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\gunzip"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\gzip.exe"
  File "${src_dir}\bin\iconv.exe"
  File "${src_dir}\bin\lidia.exe"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\msvcr90.dll"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\Microsoft.VC90.CRT.manifest"
  File "${src_dir}\bin\pango-querymodules.exe"
  File "${src_dir}\bin\pango-view.exe"
  File "${src_dir}\bin\pkg-config.exe"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\ppm2bmp.exe"
  ; now the new mingw files for static compilation (or not)
  ; FIXME:: seems to be only needed in newer versions of msys
  ; ....... and maybe if we have shared compilation - not yet
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\libstdc++-6.dll"
  ;File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\libgcc_s_dw2-1.dll"
; PYTHON stuff new
  SetOutPath "$INSTDIR\python27"
  File /r "${src_dir}\python27\*.*"
; etc things
  SetOutPath "$INSTDIR\etc\fonts"
  File "${src_dir}\etc\fonts\*"
  SetOutPath "$INSTDIR\etc\gtk-2.0"
  File "${src_dir}\etc\gtk-2.0\gtk.immodules"
  SetOutPath "$INSTDIR\etc\pango"
  File "${src_dir}\etc\pango\pango.modules"
; for ccp4 version
  SetOutPath "$INSTDIR\etc"
  File "C:\MinGW\msys\1.0\home\bernhard\Projects\coot\windows\runwincoot_ccp4.bat"
  File "C:\MinGW\msys\1.0\home\bernhard\Projects\coot\windows\runwincoot_ccp4_vista.bat"
; SHARE
  SetOutPath "$INSTDIR\share\icons"
  File /r "${src_dir}\share\icons\*.*"
  SetOutPath "$INSTDIR\share\coot"
  File "${src_dir}\share\coot\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers"
  File "${src_dir}\share\coot\lib\data\monomers\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\0"
  File "${src_dir}\share\coot\lib\data\monomers\0\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\1"
  File "${src_dir}\share\coot\lib\data\monomers\1\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\2"
  File "${src_dir}\share\coot\lib\data\monomers\2\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\3"
  File "${src_dir}\share\coot\lib\data\monomers\3\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\4"
  File "${src_dir}\share\coot\lib\data\monomers\4\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\5"
  File "${src_dir}\share\coot\lib\data\monomers\5\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\6"
  File "${src_dir}\share\coot\lib\data\monomers\6\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\7"
  File "${src_dir}\share\coot\lib\data\monomers\7\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\8"
  File "${src_dir}\share\coot\lib\data\monomers\8\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\9"
  File "${src_dir}\share\coot\lib\data\monomers\9\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\a"
  File "${src_dir}\share\coot\lib\data\monomers\a\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\b"
  File "${src_dir}\share\coot\lib\data\monomers\b\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\c"
  File "${src_dir}\share\coot\lib\data\monomers\c\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\d"
  File "${src_dir}\share\coot\lib\data\monomers\d\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\e"
  File "${src_dir}\share\coot\lib\data\monomers\e\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\f"
  File "${src_dir}\share\coot\lib\data\monomers\f\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\g"
  File "${src_dir}\share\coot\lib\data\monomers\g\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\h"
  File "${src_dir}\share\coot\lib\data\monomers\h\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\i"
  File "${src_dir}\share\coot\lib\data\monomers\i\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\j"
  File "${src_dir}\share\coot\lib\data\monomers\j\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\k"
  File "${src_dir}\share\coot\lib\data\monomers\k\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\l"
  File "${src_dir}\share\coot\lib\data\monomers\l\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\list"
  File "${src_dir}\share\coot\lib\data\monomers\list\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\m"
  File "${src_dir}\share\coot\lib\data\monomers\m\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\n"
  File "${src_dir}\share\coot\lib\data\monomers\n\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\o"
  File "${src_dir}\share\coot\lib\data\monomers\o\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\p"
  File "${src_dir}\share\coot\lib\data\monomers\p\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\q"
  File "${src_dir}\share\coot\lib\data\monomers\q\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\r"
  File "${src_dir}\share\coot\lib\data\monomers\r\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\s"
  File "${src_dir}\share\coot\lib\data\monomers\s\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\t"
  File "${src_dir}\share\coot\lib\data\monomers\t\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\u"
  File "${src_dir}\share\coot\lib\data\monomers\u\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\v"
  File "${src_dir}\share\coot\lib\data\monomers\v\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\w"
  File "${src_dir}\share\coot\lib\data\monomers\w\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\x"
  File "${src_dir}\share\coot\lib\data\monomers\x\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\y"
  File "${src_dir}\share\coot\lib\data\monomers\y\*"
  SetOutPath "$INSTDIR\share\coot\lib\data\monomers\z"
  File "${src_dir}\share\coot\lib\data\monomers\z\*"
  SetOutPath "$INSTDIR\share\coot"
  File "${src_dir}\share\coot\*"
  SetOutPath "$INSTDIR\share\coot\pixmaps"
  File "${src_dir}\share\coot\pixmaps\*"
  SetOutPath "$INSTDIR\share\coot\protein_db"
  File "${src_dir}\share\coot\protein_db\*"
  SetOutPath "$INSTDIR\share\coot\reference-structures"
  File "${src_dir}\share\coot\reference-structures\*"
  SetOutPath "$INSTDIR\share\coot\rama-data"
  File "${src_dir}\share\coot\rama-data\*"
  SetOutPath "$INSTDIR\share\coot\scheme"
  File "${src_dir}\share\coot\scheme\*"
  ;lib
  SetOutPath "$INSTDIR\lib\gdk-pixbuf-2.0"
  File /r "${src_dir}\lib\gdk-pixbuf-2.0\*.*"
  SetOutPath "$INSTDIR\lib\gtk-2.0"
  File /r "${src_dir}\lib\gtk-2.0\*.*"
!ifdef WITH_GUILE
  ;guile things (shouldnt they be in Guile section?! Never mind)
  SetOutPath "$INSTDIR\bin"
  File "${src_dir}-guile\bin\*guile*"
  File "${src_dir}-guile\bin\libgmp-3.dll"
  File "${src_dir}-guile\bin\readline5.dll"
  File "${src_dir}-guile\bin\libltdl3.dll"
  SetOutPath "$INSTDIR\share\guile\1.8"
  File "${src_dir}-guile\share\guile\1.8\*"
  SetOutPath "$INSTDIR\share\guile\1.8\ice-9"
  File "${src_dir}-guile\share\guile\1.8\ice-9\*"
  SetOutPath "$INSTDIR\share\guile\1.8\ice-9\debugger"
  File "${src_dir}-guile\share\guile\1.8\ice-9\debugger\*"
  SetOutPath "$INSTDIR\share\guile\1.8\lang\elisp"
  File "${src_dir}-guile\share\guile\1.8\lang\elisp\*"
  SetOutPath "$INSTDIR\share\guile\1.8\lang\elisp\internals"
  File "${src_dir}-guile\share\guile\1.8\lang\elisp\internals\*"
  SetOutPath "$INSTDIR\share\guile\1.8\lang\elisp\primitives"
  File "${src_dir}-guile\share\guile\1.8\lang\elisp\primitives\*"
  SetOutPath "$INSTDIR\share\guile\1.8\oop"
  File "${src_dir}-guile\share\guile\1.8\oop\*"
  SetOutPath "$INSTDIR\share\guile\1.8\oop\goops"
  File "${src_dir}-guile\share\guile\1.8\oop\goops\*"
  SetOutPath "$INSTDIR\share\guile\1.8\scripts"
  File "${src_dir}-guile\share\guile\1.8\scripts\*"
  SetOutPath "$INSTDIR\share\guile\1.8\srfi"
  File "${src_dir}-guile\share\guile\1.8\srfi\*"
  SetOutPath "$INSTDIR\share\guile\gtk"
  File "${src_dir}-guile\share\guile\gtk\*"
  SetOutPath "$INSTDIR\share\guile\gtk-2.0"
  File "${src_dir}-guile\share\guile\gtk-2.0\*"
  SetOutPath "$INSTDIR\share\guile\gui"
  File "${src_dir}-guile\share\guile\gui\*"
  SetOutPath "$INSTDIR\share\guile\site"
  File "${src_dir}-guile\share\guile\site\*"
!endif
; WITH_GUILE
  ;examples
  SetOutPath "$INSTDIR\examples"
  File "${src_dir}\share\coot\data\*"
  ; 'NEW; data dir
  SetOutPath "$INSTDIR\share\coot\data"
  File "${src_dir}\share\coot\data\*"
  ;docs
  SetOutPath "$INSTDIR\doc"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\coot-user-manual.pdf"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\crib-sheet.pdf"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\tutorial.pdf"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\tutorial-2.pdf"
  ;;secondary structure(s)
  ;SetOutPath "$INSTDIR\share\coot\ss-reference-structures"
  ;File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\ss-reference-structures\*"
  ;make a backupdir, so that COOT_BACKUP_DIR has a defined directory
  SetOutPath "$INSTDIR\coot-backup"
  ; set outpath to $INSTDIR so that shortcuts are started in $INSTDIR
  SetOutPath "$INSTDIR"

SectionEnd


Section /o "Windows feel" SEC02
  SetOverwrite on
  SetOutPath "$INSTDIR\share\coot"
  File "C:\MinGW\msys\1.0\home\bernhard\autobuild\extras\cootrc"
  SetOverwrite ifnewer
;  maybe here the other guile things?!
SectionEnd

; we dont want guile for now
; 2nd section for guile
!ifdef WITH_GUIL
Section /o "Guile/Scheme Add-On" SEC03
  SetOverwrite on
  SetOutPath "$INSTDIR\bin"
  File "${src_dir}-guile\bin\coot-real.exe"
  SetOverwrite ifnewer
  maybe here the other guile things?!
SectionEnd
!endif
; WITH_GUILE

Section -AddIcons
  ;; First install for all users, if anything fails, install
  ;; for current user only.
  ClearErrors

  SetShellVarContext all
  ; let's see what happens when we try for all
  Call MakeIcons

  ; if error delete what may be there (but there shouldnt be anything?)
  IfErrors 0 exit
  SetShellVarContext current
  Call MakeIcons

 exit:
   SetShellVarContext current
  ; in case we want to install silently (no user intervention)
  ; we have to call the finish page function, otherwise runwincoot.bat
  ; won't be edited.
  IfSilent 0 +2
    Call FinishPagePreFunction
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
;  NO MESSING WITH THE REGISTRY!!!!
;  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\uninst.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}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
;  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd



; Section descriptions
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC01} "This is 'default' WinCoot (${WinCootVersion}) $\n$\nPython scripting only"
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC02} "Tick if you want a $\nWindowsy feeling to WinCoot"
; disable guile for now
!ifdef WITH_GUILE
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC02} "Tick if you want additionally $\nGuile/Scheme scripting"
!endif
!insertmacro MUI_FUNCTION_DESCRIPTION_END


Section Uninstall
  Delete "$INSTDIR\${PRODUCT_NAME}.url"
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\share\coot\*"
  Delete "$INSTDIR\share\coot\ss-reference-structures\*"
  Delete "$INSTDIR\share\coot\scheme\*"
  Delete "$INSTDIR\share\coot\reference-structures\*"
  Delete "$INSTDIR\share\coot\rama-data\*"
;keep the python dirs here for now so it will be uninstalled if old exists
  Delete "$INSTDIR\share\coot\python\rcrane\data\*"
  Delete "$INSTDIR\share\coot\python\rcrane\*"
  Delete "$INSTDIR\share\coot\python\*"
  Delete "$INSTDIR\share\coot\pixmaps\*"
  Delete "$INSTDIR\share\coot\protein_db\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\z\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\y\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\x\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\w\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\v\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\u\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\t\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\s\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\r\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\q\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\p\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\o\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\n\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\m\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\list\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\l\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\k\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\j\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\i\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\h\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\g\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\f\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\e\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\d\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\c\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\b\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\a\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\9\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\8\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\7\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\6\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\5\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\4\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\3\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\2\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\1\*"
  Delete "$INSTDIR\share\coot\lib\data\monomers\0\*"
  Delete "$INSTDIR\share\coot\data\*"
  Delete "$INSTDIR\share\guile\site\*"
  Delete "$INSTDIR\share\guile\gui\*"
  Delete "$INSTDIR\share\guile\gtk-2.0\*"
  Delete "$INSTDIR\share\guile\gtk\*"
  Delete "$INSTDIR\share\guile\1.8\srfi\*"
  Delete "$INSTDIR\share\guile\1.8\scripts\*"
  Delete "$INSTDIR\share\guile\1.8\oop\goops\*"
  Delete "$INSTDIR\share\guile\1.8\oop\*"
  Delete "$INSTDIR\share\guile\1.8\lang\elisp\primitives\*"
  Delete "$INSTDIR\share\guile\1.8\lang\elisp\internals\*"
  Delete "$INSTDIR\share\guile\1.8\lang\elisp\*"
  Delete "$INSTDIR\share\guile\1.8\ice-9\debugger\*"
  Delete "$INSTDIR\share\guile\1.8\ice-9\*"
  Delete "$INSTDIR\share\guile\1.8\*"
  Delete "$INSTDIR\share\*"
  ; keep the next 2 in case it was there from previous installations
  Delete "$INSTDIR\bin\Lib\*"
  Delete "$INSTDIR\bin\DLLs\*"
  ; only remove installed files in bin
  ; (in case there exists probe, reduce e.g.)
  Delete "$INSTDIR\bin\coot-icon.ico"
  Delete "$INSTDIR\bin\*.dll"
  Delete "$INSTDIR\bin\bfactan.exe"
  Delete "$INSTDIR\bin\coot"
  Delete "$INSTDIR\bin\coot-real.exe"
  Delete "$INSTDIR\libexec\coot-bin.exe"
  Delete "$INSTDIR\libexec\density-score-by-residue-bin.exe"
  Delete "$INSTDIR\libexec\findligand-bin.exe"
  Delete "$INSTDIR\libexec\findwaters-bin.exe"
  Delete "$INSTDIR\libexec\mini-rsr-bin.exe"
  Delete "$INSTDIR\bin\density-score-by-residue"
  Delete "$INSTDIR\bin\findligand"
  Delete "$INSTDIR\bin\findligand-real.exe"
  Delete "$INSTDIR\bin\findwaters"
  Delete "$INSTDIR\bin\findwaters-real.exe"
  Delete "$INSTDIR\bin\fix-nomenclature-errors.exe"
  Delete "$INSTDIR\bin\gdk-pixbuf-csource.exe"
  Delete "$INSTDIR\bin\gdk-pixbuf-query-loaders.exe"
  Delete "$INSTDIR\bin\glib-genmarshal.exe"
  Delete "$INSTDIR\bin\glib-gettextize"
  Delete "$INSTDIR\bin\glib-mkenums"
  Delete "$INSTDIR\bin\gobject-query.exe"
  Delete "$INSTDIR\bin\gsl-config"
  Delete "$INSTDIR\bin\gspawn-win32-helper-console.exe"
  Delete "$INSTDIR\bin\gspawn-win32-helper.exe"
  Delete "$INSTDIR\bin\gtk-builder-convert"
  Delete "$INSTDIR\bin\gtk-demo.exe"
  Delete "$INSTDIR\bin\gtk-query-immodules-2.0.exe"
  Delete "$INSTDIR\bin\gunzip"
  Delete "$INSTDIR\bin\gzip.exe"
  Delete "$INSTDIR\bin\iconv.exe"
  Delete "$INSTDIR\bin\lidia.exe"
  Delete "$INSTDIR\bin\Microsoft.VC90.CRT.manifest"
  Delete "$INSTDIR\bin\pango-querymodules.exe"
  Delete "$INSTDIR\bin\pango-view.exe"
  Delete "$INSTDIR\bin\pkg-config.exe"
  Delete "$INSTDIR\bin\ppm2bmp.exe"
  ; guile things
  Delete "$INSTDIR\bin\*guile*"
  Delete "$INSTDIR\bin\libgmp-3.dll"
  Delete "$INSTDIR\bin\readline5.dll"
  Delete "$INSTDIR\bin\libltdl3.dll"
  ; continue deleting everything
  Delete "$INSTDIR\etc\pango\*"
  Delete "$INSTDIR\etc\gtk-2.0\*"
  Delete "$INSTDIR\etc\fonts\*"
  Delete "$INSTDIR\etc\*"
  Delete "$INSTDIR\lib\gdk-pixbuf-2.0\*"
  Delete "$INSTDIR\lib\gtk-2.0\engines\*"
  Delete "$INSTDIR\lib\gtk-2.0\2.10.0\loaders\*"
  Delete "$INSTDIR\examples\*"
  Delete "$INSTDIR\doc\*"
  ; we shall only remove installed files here (to e.g. keep .coot.py untouched)
  Delete "$INSTDIR\runwincoot.bat"
  Delete "$INSTDIR\Coot.url"

  !insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP
  SetShellVarContext all
  Delete "$SMPROGRAMS\$ICONS_GROUP\*"
  Delete "$DESKTOP\WinCoot.lnk"
  Delete "$QUICKLAUNCH\WinCoot.lnk"
  RMDir "$SMPROGRAMS\$ICONS_GROUP"
  SetShellVarContext current
  Delete "$SMPROGRAMS\$ICONS_GROUP\*"
  Delete "$DESKTOP\WinCoot.lnk"
  Delete "$QUICKLAUNCH\WinCoot.lnk"
  RMDir "$SMPROGRAMS\$ICONS_GROUP"

  RMDir "$INSTDIR\share\coot\ss-reference-structures"
  RMDir "$INSTDIR\share\coot\scheme"
  RMDir "$INSTDIR\share\coot\reference-structures"
  RMDir "$INSTDIR\share\coot\rama-data"
  RMDir "$INSTDIR\share\coot\python\rcrane\data"
  RMDir "$INSTDIR\share\coot\python\rcrane"
  RMDir "$INSTDIR\share\coot\python"
  RMDir "$INSTDIR\share\coot\pixmaps"
  RMDir "$INSTDIR\share\coot\protein_db"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\z"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\y"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\x"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\w"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\v"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\u"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\t"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\s"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\r"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\q"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\p"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\o"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\n"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\m"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\list"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\l"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\k"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\j"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\i"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\h"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\g"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\f"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\e"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\d"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\c"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\b"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\a"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\9"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\8"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\7"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\6"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\5"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\4"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\3"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\2"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\1"
  RMDir "$INSTDIR\share\coot\lib\data\monomers\0"
  RMDir "$INSTDIR\share\coot\lib\data\monomers"
  RMDir "$INSTDIR\share\coot\lib\data"
  RMDir "$INSTDIR\share\coot\lib"
  RMDir "$INSTDIR\share\coot\data"
  RMDir "$INSTDIR\share\coot"
  RMDir "$INSTDIR\share\guile\site"
  RMDir "$INSTDIR\share\guile\gui"
  RMDir "$INSTDIR\share\guile\gtk-2.0"
  RMDir "$INSTDIR\share\guile\gtk"
  RMDir "$INSTDIR\share\guile\1.8\srfi"
  RMDir "$INSTDIR\share\guile\1.8\scripts"
  RMDir "$INSTDIR\share\guile\1.8\oop\goops"
  RMDir "$INSTDIR\share\guile\1.8\oop"
  RMDir "$INSTDIR\share\guile\1.8\lang\elisp\primitives"
  RMDir "$INSTDIR\share\guile\1.8\lang\elisp\internals"
  RMDir "$INSTDIR\share\guile\1.8\lang\elisp"
  RMDir "$INSTDIR\share\guile\1.8\lang"
  RMDir "$INSTDIR\share\guile\1.8\ice-9\debugger"
  RMDir "$INSTDIR\share\guile\1.8\ice-9"
  RMDir "$INSTDIR\share\guile\1.8"
  RMDir "$INSTDIR\share\guile"
  RMDir /r "$INSTDIR\share\icons"
  RMDir "$INSTDIR\share"
  ; keep these in case they exist from previous versions
  RMDir /r "$INSTDIR\bin\Lib"
  RMDir "$INSTDIR\bin\DLLs"
  RMDir "$INSTDIR\etc\pango"
  RMDir "$INSTDIR\etc\gtk-2.0"
  RMDir "$INSTDIR\etc\fonts"
  RMDir "$INSTDIR\etc"
  RMDir "$INSTDIR\bin"
  RMDir /r "$INSTDIR\lib\gdk-pixbuf-2.0"
  RMDir /r "$INSTDIR\lib\gtk-2.0"
  RMDir "$INSTDIR\lib"
  RMDir "$INSTDIR\libexec"
  ; FIXME:: what to do with python? 
  RMDir /r "$INSTDIR\python27"
  RMDir "$INSTDIR\examples"
  RMDir "$INSTDIR\doc"
  ; will only be removed if empty
  RMDir "$INSTDIR\.coot-preferences"
  RMDir "$INSTDIR\coot-backup"
  RMDir "$INSTDIR\"

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

######################
# 'BUILD-IN' FUNCTIONS
######################

# BL says:: disable for now, since no guile available anyway 
!ifdef WITH_GUILE
Function .onSelChange
   ${If} ${SectionIsSelected} ${SEC03}
      MessageBox MB_OK|MB_ICONEXCLAMATION "You have with guile selected. Sure? This may not work perfectly.$\r$\n\"
   ${EndIf}
FunctionEnd
!endif

Function .onInit

    ; Get Command line parameters
        var /GLOBAL INSTDIR_TMP
        StrCpy $INSTDIR_TMP $INSTDIR
        StrCpy $INSTDIR ""

	var /GLOBAL cmdLineParams
	Push $R0

	${GetParameters} $cmdLineParams

	; /? param (help)
	ClearErrors
	${GetOptions} $cmdLineParams '/?' $R0
	IfErrors +3 0
        Call ShowOptions
	Abort

	ClearErrors
	${GetOptions} $cmdLineParams '/help' $R0
	IfErrors +3 0
        Call ShowOptions
	Abort

	Pop $R0

	ClearErrors

    ; Initialise options

	Var /GLOBAL update
	Var /GLOBAL delete_installer
	Var /GLOBAL start_coot

	StrCpy $update             	  0
	StrCpy $delete_installer      	  0
	StrCpy $start_coot      	  0

    ; Parse Parameters

	Push $R0
	Call parseParameters
	Pop $R0

        ${If} $INSTDIR == ""
          ${If} $update = 1
           Call NoInstDirGiven
          ${Else}
           StrCpy $INSTDIR $INSTDIR_TMP
          ${EndIf}
        ${EndIf}
; insert params END
  ${If} $STARTDIR == ""
    StrCpy $STARTDIR "$INSTDIR\examples"
  ${EndIf}

FunctionEnd

; finallising
Function .onGUIEnd
  ; remove tmp bat file if exists (Delete wont do, need to do it via DOS shell command)
  ; possibly an anti virus thing?!?
  IfFileExists "$INSTDIR\runwincoot.bat.tmp" "" cont
     ;Delete "$INSTDIR\runwincoot.bat.tmp"
     Exec 'cmd /c del "$INSTDIR\runwincoot.bat.tmp"'
  cont:

  ; delete the installer
  ${If} $delete_installer = 1
   !insertmacro StrStr $0 "$EXEDIR" "pending-install"
   ${If} $0 == ""
    Exec 'cmd /c del "$EXEDIR\${PRODUCT_NAME}-${PRODUCT_VERSION}.exe"'
   ${Else}
    Exec 'cmd /c rmdir /s /q "$EXEDIR"'
   ${EndIf}
  ${EndIf}
  
  ; Finally if run then start WinCoot in StartDir
  ${If} $start_coot = 1
    SetOutPath $STARTDIR
    Exec $INSTDIR\runwincoot.bat
  ${EndIf}

FunctionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer." /SD IDOK
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?" /SD IDYES IDYES +2
  Abort
FunctionEnd

###################
# GENERAL FUNCTIONS
###################

Function MakeBackground
  ; background depends on update
  ${If} $update = 0
    ; from dark green to dark red : 0 0x80 0 0x80 0 0
    BgImage::SetBg /GRADIENT 0 0x80 0 0x80 0 0
    BgImage::AddText "$(^Name) Installer"
    ;BgImage::AddImage C:\MinGW\msys\1.0\home\bernhard\installer\coot_pic_welcome.bmp 150 0
    CreateFont $R0 "Comic Sans MS" 30 700
    # add a blue shadow for the text
    BgImage::AddText "$(^Name) Installer" $R0 0 0 255 48 48 798 198
    # add a green shadow for the text
    BgImage::AddText "$(^Name) Installer" $R0 0 255 0 52 52 802 202
    # add the text
    BgImage::AddText "$(^Name) Installer" $R0 255 0 0 50 50 800 200
    BgImage::Redraw
  ${EndIf}
FunctionEnd

Function parseParameters

    ; /instdir=
    ; What about spaces in dir name?? USE quotes!?
    ${GetOptions} $cmdLineParams '/instdir=' $R0
    IfErrors continue0 0
    ${If} $R0 == ""
    MessageBox MB_ICONSTOP "No install directory given.$\n$\n\
               Please use command line option /instdir=instdir$\n$\n\
               Abort installation!"
    Abort
    ${Else}
    StrCpy $INSTDIR $R0
    ${EndIf}
    continue0:

    ; /update
    ${GetOptions} $cmdLineParams '/update' $R0
    IfErrors continue1 0
    StrCpy $update 1
    continue1:

    ; /autoupdate
    ${GetOptions} $cmdLineParams '/autoupdate' $R0
    IfErrors continue2 0
    StrCpy $update 1
    StrCpy $delete_installer 1
    StrCpy $start_coot 1
    continue2:

    ; /startdir=
    ; What about spaces in dir name?? USE quotes!?
    ${GetOptions} $cmdLineParams '/startdir=' $R0
    IfErrors continue3 0
    ${If} $R0 == ""
    MessageBox MB_ICONSTOP "No start directory given.$\n$\n\
               Please use command line option /startdir=startdir$\n$\n\
               Abort installation!"
    Abort
    ${Else}
    StrCpy $STARTDIR $R0
    ${EndIf}
    continue3:

    ; /run
    ${GetOptions} $cmdLineParams '/run' $R0
    IfErrors +2 0
    StrCpy $start_coot 1

    ; /del_inst
    ${GetOptions} $cmdLineParams '/del_inst' $R0
    IfErrors +2 0
    StrCpy $delete_installer 1

FunctionEnd

Function NoInstDirGiven
    MessageBox MB_ICONSTOP "No installation directory given.$\n$\n\
               Please use command line option /instdir=instdir$\n$\n\
               Abort installation!"
    Abort
FunctionEnd

Function ShowOptions
	MessageBox MB_ICONQUESTION "WinCoot Installer Command Line Help$\n$\n\
        Options:$\n\
          /D=instdir         - defines installation directory to instdir$\n\
          /instdir=instdir   - as above (preferred!!! needs to be first)$\n\
          /startdir=statdir  - defines dir where Coot will start (after install/update)$\n\
          /update            - update WinCoot (no questions asked, requires /D)$\n\
          /autoupdate        - as /update but removes installer and starts WinCoot$\n\
          /del_inst          - removes the installer after installation$\n\
          /run               - run WinCoot after installation$\n\
          /S                 - silent installation (and uninstall)$\n\
          /help | /?         - this information"
FunctionEnd

; Dont show pages when updating!
Function CheckForUpdate
  ${If} $update = 1
     Abort
  ${EndIf}
FunctionEnd

; before we start pages
Function InitPreFunction
  Call CheckForUpdate
  Call MakeBackground
FunctionEnd

; inistiallize the directory page count
Function ComponentPost
  Call CheckForUpdate
  Var /GLOBAL DIRCOUNT
  StrCpy $DIRCOUNT "0"
FunctionEnd

Function DirectoryShow
  StrCmp $DIRCOUNT "0" InstDirPage
  StrCmp $DIRCOUNT "1" StartDirPage
  InstDirPage:
    StrCpy $DIRCOUNT "1"
    Goto EndDirShow
  StartDirPage:
     StrCpy $DIRCOUNT "2"
     !insertmacro MUI_HEADER_TEXT "Choose WinCoot Start Location" "Choose the folder in which to start WinCoot."
     !insertmacro MUI_INNERDIALOG_TEXT 1041 "WinCoot Start Folder"
     !insertmacro MUI_INNERDIALOG_TEXT 1006 "Setup will make WinCoot icon shortcuts to start in the following folder.$\r$\n$\r$\nTo install in a different folder, click Browse and select another folder. Click Install to continue the installation."
     Goto EndDirShow
  EndDirShow:
FunctionEnd

; for changing wincoot [and gdk-pixbuf-loader] Not sure if here is a good place? Get's a bit messy
Function FinishPagePreFunction
   ; first apply changes to runwincoot.bat
   Var /GLOBAL GUILE_INST_DIR
   ${WordReplace} "$INSTDIR" "\" "/" "+" $GUILE_INST_DIR
   !insertmacro ReplaceOnLine "yourWinCootdirectory" "$INSTDIR" "5" "$INSTDIR\runwincoot.bat.tmp"
   !insertmacro ReplaceOnLine "yourWinCootdirectoryGuile" "$GUILE_INST_DIR" "6" "$INSTDIR\runwincoot.bat.tmp"
   ;we want to change more for Vista (and possibly for Windows 7 too FIXME!)
   ; Maybe not too much any more... (since graphics card issue)
   ${If} ${AtLeastWinVista}
       ; change to run on 1 core only (to enable compositing!)
       !insertmacro AdvReplaceInFile "coot-bin.exe" "start /affinity 1 coot-bin.exe" "0" "1" "$INSTDIR\runwincoot.bat.tmp"
   ${EndIf}
   ; if we have an old bat file
   ${If} $have_bat == "True"
     ; check if wincootbats are different
     Var /Global bat_differ
     StrCpy $bat_differ "False"
     ${TextCompare} "$INSTDIR\runwincoot.bat" "$INSTDIR\runwincoot.bat.tmp" "FastDiff" "TxtCompResult"

     ${If} $bat_differ == "True"
        ; ask or if silent/update keep old
        Var /GLOBAL keep_old_bat
        StrCpy $keep_old_bat "True"
        ${If} $update = 0
           IfSilent endifbat
            MessageBox MB_ICONQUESTION|MB_YESNO "You already have a (modified) WinCoot batch file (runwincoot.bat).$\r$\n\
            Do you want to keep it (dont if you upgrade from <0.8)?" IDYES endifbat
            StrCpy $keep_old_bat "False"
           endifbat:
        ${EndIf}  ; update

        ; replace old with new if requested
        ${If} $keep_old_bat == "False"
             CopyFiles "$INSTDIR\runwincoot.bat.tmp" "$INSTDIR\runwincoot.bat"
        ${EndIf}

     ${EndIf}  ; bat_differ

  ${Else}
     ; dont have any runwincoot.bat
     Rename "$INSTDIR\runwincoot.bat.tmp" "$INSTDIR\runwincoot.bat"
  ${EndIf}  ; have_bat nothing further to be one
  ; executable access to everyone
  AccessControl::GrantOnFile /NOINHERIT "$INSTDIR\runwincoot.bat" "(BA)" "FullAccess"
  AccessControl::GrantOnFile /NOINHERIT "$INSTDIR\runwincoot.bat" "(BU)" "GenericExecute"

;  for now dont mess with pixbuf query loader
;  ExecWait 'cmd /c ""$INSTDIR\bin\gdk-pixbuf-query-loaders.exe" > "$INSTDIR\etc\gtk-2.0\gdk-pixbuf.loaders""'
  ; set outpath to run where we want to

  SetOutPath "$STARTDIR"
  Call CheckForUpdate
FunctionEnd

Function TxtCompResult
     StrCpy $bat_differ "True"
FunctionEnd

Function MakeIcons
# run only if not update
${If} $update = 0
  SetOutPath "$INSTDIR"
; Shortcuts
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  CreateDirectory "$SMPROGRAMS\$ICONS_GROUP"
  SetOutPath "$STARTDIR"
  CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\WinCoot.lnk" "$SYSDIR\cmd.exe" '/c "$INSTDIR\runwincoot.bat"' "$INSTDIR\bin\coot-icon.ico"
  CreateShortCut "$QUICKLAUNCH\WinCoot.lnk" "$SYSDIR\cmd.exe" '/c "$INSTDIR\runwincoot.bat"' "$INSTDIR\bin\coot-icon.ico"
  CreateShortCut "$DESKTOP\WinCoot.lnk" "$SYSDIR\cmd.exe" '/c "$INSTDIR\runwincoot.bat"' "$INSTDIR\bin\coot-icon.ico"
  SetOutPath "$INSTDIR"
  WriteIniStr "$INSTDIR\WinCoot.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  WriteIniStr "$INSTDIR\Coot.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE_2}"
  CreateShortCut "$SMPROGRAMS\WinCoot\WinCoot Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  CreateShortCut "$SMPROGRAMS\WinCoot\Coot Website.lnk" "$INSTDIR\Coot.url"
  CreateShortCut "$SMPROGRAMS\WinCoot\Uninstall.lnk" "$INSTDIR\uninst.exe"
  CreateShortCut "$SMPROGRAMS\WinCoot\Coot Manual.lnk" "$INSTDIR\doc\coot-user-manual.pdf"
  CreateShortCut "$SMPROGRAMS\WinCoot\Coot Keys and Buttons.lnk" "$INSTDIR\doc\crib-sheet.pdf"
  CreateShortCut "$SMPROGRAMS\WinCoot\Coot Tutorial.lnk" "$INSTDIR\doc\tutorial.pdf"
  !insertmacro MUI_STARTMENU_WRITE_END
  SetOutPath "$INSTDIR"
 ${Endif}
FunctionEnd

