;Force installation script
;Copyright SoftCapital ApS 2003
;Lars Pehrsson

!define MUI_PRODUCT "Force" ;Define your own software name here
!define MUI_VERSION "0.9" ;Define your own software version here

!include "MUI.nsh"

;--------------------------------
;Configuration

  ;General
  OutFile "Streamer.exe"

  ;Folder selection page
  InstallDir "$PROGRAMFILES\SoftCapital\${MUI_PRODUCT}"

  ;Remember install folder
  InstallDirRegKey HKCU "Software\${MUI_PRODUCT}" ""

;--------------------------------
;Modern UI Configuration


  !define MUI_WELCOMEPAGE
  !define MUI_LICENSEPAGE
  !define MUI_COMPONENTSPAGE
  !define MUI_DIRECTORYPAGE

  !define MUI_ABORTWARNING
  !define MUI_FINISHPAGE
  !define MUI_FINISHPAGE_RUN "$INSTDIR\force.exe"

  !define MUI_UNINSTALLER
  !define MUI_UNCONFIRMPAGE

  !define MUI_HEADERBITMAP "${NSISDIR}\Contrib\Icons\modern-header 2.bmp"
  !define MUI_SPECIALBITMAP "${NSISDIR}\Contrib\Icons\modern-wizard.bmp"



;--------------------------------
;Languages

  !define MUI_TEXT_WELCOME_INFO_TEXT "Denne installations guide vil lede dig igennem Installationen af Force, markedets hurtigste Streamer og handelssystem.\r\n\r\n\r\n"

  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Language Strings

  ;Description
  LangString DESC_SecCopyUI ${LANG_ENGLISH} "Copy Force files file to the application folder."

;--------------------------------
;Data

  LicenseData "${NSISDIR}\Contrib\Modern UI\License.txt"

;--------------------------------
;Reserve Files

  ;Things that need to be extracted on first (keep these lines before any File command!)
  ;Only useful for BZIP2 compression

  ReserveFile "${NSISDIR}\Contrib\Icons\modern-header.bmp"
  !insertmacro MUI_RESERVEFILE_WELCOMEFINISHPAGE

;--------------------------------

ComponentText "please choose just one but the default"

# defines SF_*, SECTION_OFF and some macros
!include Sections.nsh

Section !Force
  SectionIn RO
 ;Add files!
   SetOutPath "$INSTDIR"
  File "${NSISDIR}\force\Force.exe"
  File "${NSISDIR}\Force\BCGCBPRO651.dll"
  File "${NSISDIR}\Force\columns.def"
  File "${NSISDIR}\Force\TeeChart5.ocx"
  File "${NSISDIR}\Force\ForceAqua60.dll"
  ;File "${NSISDIR}\Force\Denmark.reg"
  ;File "${NSISDIR}\Force\Germany.reg"
  ;File "${NSISDIR}\Force\Sweden.reg"
  ;File "${NSISDIR}\Force\Russian.reg"
  ;File "${NSISDIR}\Force\Simulated.reg"
   SetOutPath $INSTDIR\Cache
  File "${NSISDIR}\Force\Cache\wait.html"
  File "${NSISDIR}\Force\Cache\error.html"
  File "${NSISDIR}\Force\Cache\wait.jpg"
  File "${NSISDIR}\Force\error.html"
  File "${NSISDIR}\Force\wait.jpg"

  SetOutPath "$INSTDIR"
  ;Register ActiveX?s and DLL?s
  nsExec::Exec "cmd /c $INSTDIR\force\regsvr32.exe /s TeeChart5.ocx"


  ; Add menu shortcuts

  CreateDirectory "$SMPROGRAMS\SoftCapital"
  CreateShortCut "$SMPROGRAMS\SoftCapital\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortCut "$SMPROGRAMS\SoftCapital\Force.lnk" "$INSTDIR\force.exe" "" "$INSTDIR\force.exe"
  CreateShortCut "$DESKTOP\Force Streamer.lnk" "$INSTDIR\Force.exe"

  ;Store install folder
  WriteRegStr HKCU "Software\${MUI_PRODUCT}" "" $INSTDIR


  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\Force "Install_Dir" "$INSTDIR"
  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Force" "DisplayName" "Force Streamer"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Force" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteUninstaller "uninstall.exe"
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"

  ; Write to hosts file
FileOpen $4 $SYSDIR\drivers\etc\hosts a
FileSeek $4 0 END
FileWrite $4 "$\r$\n" ; we write a new line
FileWrite $4 "194.239.182.5	www.softcapital.com	odin"
FileWrite $4 "$\r$\n" ; we write an extra line
FileClose $4 ; and close the file


SectionEnd

!ifdef USE_SUBSECTION
  SubSection /e choose one
!endif

Section "Denmark Stream" sec1
  GetTempFileName $R0
  File /oname=$R0 Denmark.reg       ; copy myreg.reg to <uniquefilename> on target PC
  ExecWait 'Regedit.exe /S "$R0"'   ; merge the copied file into the registry (silently)
  Delete $R0                        ; delete the temporary file

SectionEnd

Section "Moscow Stream" sec2
GetTempFileName $R0
  File /oname=$R0 Russian.reg     
  ExecWait 'Regedit.exe /S "$R0"'  
  Delete $R0     
SectionEnd

Section "Germany" sec3
GetTempFileName $R0
  File /oname=$R0 Germany.reg     
  ExecWait 'Regedit.exe /S "$R0"'   
  Delete $R0     
SectionEnd

Section "Simulated" sec4
GetTempFileName $R0
  File /oname=$R0 Simulated.reg     
  ExecWait 'Regedit.exe /S "$R0"'  
  Delete $R0     
SectionEnd

!ifdef USE_SUBSECTION
  SubSectionEnd
!endif

Function .onInit
  Push $0

  StrCpy $1 ${sec1} ; Gotta remember which section we are at now...
  SectionGetFlags ${sec1} $0
  IntOp $0 $0 | ${SF_SELECTED}
  SectionSetFlags ${sec1} $0
  # !insertmacro SelectSection ${sec1}

  SectionGetFlags ${sec2} $0
  IntOp $0 $0 & ${SECTION_OFF}
  SectionSetFlags ${sec2} $0
  # !insertmacro UnselectSection ${sec2}

  SectionGetFlags ${sec3} $0
  IntOp $0 $0 & ${SECTION_OFF}
  SectionSetFlags ${sec3} $0
  # !insertmacro UnselectSection ${sec3}

  SectionGetFlags ${sec4} $0
  IntOp $0 $0 & ${SECTION_OFF}
  SectionSetFlags ${sec4} $0
  # !insertmacro UnselectSection ${sec4}

  Pop $0
FunctionEnd

Function .onSelChange
  Push $0

!ifdef USE_SUBSECTION
  Push $2
  StrCpy $2 ${SF_SELECTED}
  SectionGetFlags ${sec1} $0
  IntOp $2 $2 & $0
  SectionGetFlags ${sec2} $0
  IntOp $2 $2 & $0
  SectionGetFlags ${sec3} $0
  IntOp $2 $2 & $0
  SectionGetFlags ${sec4} $0
  IntOp $2 $2 & $0
  StrCmp $2 0 skip
    SectionSetFlags ${sec1} 0
    SectionSetFlags ${sec2} 0
    SectionSetFlags ${sec3} 0
    SectionSetFlags ${sec4} 0
  skip:
  Pop $2
!endif

  ; Turn off old selected section
  SectionGetFlags $1 $0
  IntOp $0 $0 & ${SECTION_OFF}
  SectionSetFlags $1 $0
  # !insertmacro UnselectSection $1

  ; Now remember the current selection
  Push $2
  StrCpy $2 $1

  SectionGetFlags ${sec1} $0
  IntOp $0 $0 & ${SF_SELECTED}
  IntCmp $0 ${SF_SELECTED} 0 +2 +2
    StrCpy $1 ${sec1}
  SectionGetFlags ${sec2} $0
  IntOp $0 $0 & ${SF_SELECTED}
  IntCmp $0 ${SF_SELECTED} 0 +2 +2
    StrCpy $1 ${sec2}
  SectionGetFlags ${sec3} $0
  IntOp $0 $0 & ${SF_SELECTED}
  IntCmp $0 ${SF_SELECTED} 0 +2 +2
    StrCpy $1 ${sec3}
  SectionGetFlags ${sec4} $0
  IntOp $0 $0 & ${SF_SELECTED}
  IntCmp $0 ${SF_SELECTED} 0 +2 +2
    StrCpy $1 ${sec4}

  StrCmp $2 $1 0 +4 ; selection hasn't changed
    SectionGetFlags $1 $0
    IntOp $0 $0 | ${SF_SELECTED}
    SectionSetFlags $1 $0
    # !insertmacro SelectSection $1
  Pop $2
  Pop $0
FunctionEnd

;Display the Finish header
;Insert this macro after the sections if you are not using a finish page
!insertmacro MUI_SECTIONS_FINISHHEADER

;--------------------------------
;Descriptions

!insertmacro MUI_FUNCTIONS_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecCopyUI} $(DESC_SecCopyUI)
!insertmacro MUI_FUNCTIONS_DESCRIPTION_END

;--------------------------------


;Uninstaller Section

Section "Uninstall"

  ;Delete files, dirs, and shortcuts!

  Delete "$INSTDIR\force.exe"
  Delete "$INSTDIR\BCGCBPRO651.dll"
  Delete "$INSTDIR\Columns.def"
  Delete "$INSTDIR\TeeChart5.ocx"
  Delete "$INSTDIR\Uninstall.exe"
  Delete "$INSTDIR\forceAqua60.dll"
  Delete "$INSTDIR\Uninstall.exe"
  Delete "$INSTDIR\Denmark.reg"
  Delete "$INSTDIR\Russian.reg"
  Delete "$INSTDIR\Sweden.reg"
  Delete "$INSTDIR\Germany.reg"
  Delete "$INSTDIR\Simulated.reg"
  Delete "$INSTDIR\Cache\wait.html"
  Delete "$INSTDIR\Cache\wait.jpg"
  Delete "$INSTDIR\Cache\error.html"
 Delete "$INSTDIR\Cache\*.trd"
 Delete "$INSTDIR\Cache\*.hist"
 Delete "$INSTDIR\Cache\*.news"
  RMDir "$INSTDIR\Cache"
  RMDir "$INSTDIR"
  Delete "$SMPROGRAMS\SoftCapital\*.*"
  RMDir /r $SMPROGRAMS\SoftCapital
  Delete "$DESKTOP\Force Streamer.lnk"
  nsExec::Exec "cmd /c $INSTDIR\regsvr32.exe /u TeeChart5.ocx"

  DeleteRegKey HKCU "Software\${MUI_PRODUCT}"

  ;Display the Finish header
  !insertmacro MUI_UNFINISHHEADER

SectionEnd