--------------------------------------------------
; Useful FX.nsi
;
; This script installs Useful FX - by Casey Green www.UsefulFX.com

!define SRC_DIR "W:\"
!define SUB_DIR "Useful FX"

; Define this to prevent tight compression for faster builds
; Undefine it for final builds
; define NOCOMPRESS

FileBufSize 64

!ifdef NOCOMPRESS
SetCompress auto
SetCompressor zlib
!else
SetCompress auto
SetCompressor lzma
SetCompressorDictSize 96
!endif

Var DoAllNone

;--------------------------------

!include "MUI.nsh"
!include "Sections.nsh"
;!include "UpgradeDll.nsh"
;!include "LogicLib.nsh"

;--------------------------------
;Interface Settings

!define MUI_ABORTWARNING
!define MUI_COMPONENTSPAGE_NODESC
  
BrandingText " "

!ifdef HAVE_UPX
!packhdr tmp.dat "upx\upx -9 tmp.dat"
!endif

;--------------------------------

Name "Useful FX"
Caption "Useful FX Content"
;Icon "${NSISDIR}\Contrib\Graphics\Icons\nsis1-install.ico"
OutFile "setupUseful FX.exe"

ShowInstDetails show
SetDateSave on
SetDatablockOptimize on
CRCCheck on
SilentInstall normal
BGGradient 000000 000080 FFFFFF
InstallColors FF8080 000030
XPStyle on

InstallDir "C:\GlobeCaster\Bins"
InstallDirRegKey HKLM "Software\Play\Trinity" "Uninstall"

CheckBitmap "${NSISDIR}\Contrib\Graphics\Checks\classic-cross.bmp"

;LicenseText "A test text, make sure it's all there"
;LicenseData "Useful FX License Agreement.rtf"
LicenseForceSelection checkbox
;--------------------------------

;--------------------------------
;Pages

  !insertmacro MUI_PAGE_LICENSE "Useful FX License Agreement.rtf"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_PAGE_FINISH

  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
  !insertmacro MUI_UNPAGE_FINISH

;--------------------------------
;Languages

  !insertmacro MUI_LANGUAGE "English"
;Page license
;Page components
;Page directory
;Page instfiles

;UninstPage uninstConfirm
;UninstPage instfiles

;--------------------------------

AutoCloseWindow false

Function .oninit

  ; This tells the uninstaller whether to ask for each DLL, do all or do none
  ; 0 = Ask, 1 = Do All, 2 = Do None
  IntOp $DoAllNone 0 + 

  IntOp $8 0 +  ; This is a variable for doing RunOnce keys

  ;Call GetWindowsVersion
  ;Pop $R4
  ;StrCpy $R0 $R4 2
  ;StrCpy $R1 $R4 4
  ;IntOp $R8 0 +
  ;StrCmp $R0 "XP" is_xp
  ;StrCmp $R1 "2003" is_2k03
  ;StrCmp $R0 "20" is_2k
  ;MessageBox MB_OK|MB_ICONSTOP "Useful FX content will only run on Windows 2000 or higher"
  ;Quit
  
;is_2k03:
 ; IntOp $R8 $R8 + 1
;is_xp:
 ; IntOp $R8 $R8 + 1
;is_2k:
 ; IntOp $R8 $R8 + 1

  IntOp $1 0 +
  ReadRegStr $2 HKLM "Software\Play\Trinity" "Uninstall"
  IfErrors no_path
  IntOp $1 1 +  ; Set this to 1 to flag that we are overwriting an existing install
no_path:
  Push $CMDLINE
  Call GetNextParm
  Pop $R0 ; name of installer
  Call GetNextParm
  Pop $R1 ; first parameter
  Call GetNextParm
  Pop $R2 ; second parameter
  Pop $R9 ; third paramter (if any)

  Push $R0
  Call GetFileName
  Pop $R0
;  MessageBox MB_OK "Installer name: $R0"

  StrCmp $R1 "auto" is_auto no_auto ; If Auto is specified, then exit if we're already installed
is_auto:
  IntCmp $1 1 already_there no_auto
already_there:
  Quit
no_auto:
  StrCmp $R1 "fromHD" fromHD
  ;fromCD:
  ;  MessageBox MB_OK "Installer called from CD-Rom"
  ;  MessageBox MB_OK "Will now copy installer file to temp."
    CopyFiles "$EXEDIR\$R0" "$TEMP"
  ;  MessageBox MB_OK "Will now call installer from HD."
    ;FIXME: Replace 'E:\' with $EXEDIR for release
    Exec '$TEMP\$R0 fromHD "$EXEDIR"'
    Abort
fromHD:
  ;  MessageBox MB_OK "Installer called from HD"
  ;  MessageBox MB_OK "CD Rom root: $R2\"

FunctionEnd

; GetNextParm - by Dave Laundon
;   Processes the next parameter, which may be quoted, from the string on the
;   top of the stack.
; Usage:
;   Push <parameters>
;   Call GetNextParm
;   Pop <next parameter>
;   Pop <remaining parameters>
;
; 2003-10-30
; Modifications: replaced $0 with $R0, $1 with $R1, etc
Function GetNextParm
  Exch $R0
  Push $R1
  Push $R9
  Push $R8
  StrCpy $R1 ""

; Trim leading space
TrimLeading:
  StrCpy $R9 $R0 1
  StrCmp $R9 "" Done
  StrCmp $R9 '"' DelimitQuote
  StrCmp $R9 " " "" DelimitSpace
  StrCpy $R0 $R0 "" 1
  Goto TrimLeading

; Begin a quote-delimited parameter
DelimitQuote:
  StrCpy $R0 $R0 "" 1
  Goto CopyParm

; Begin a space-delimited parameter
DelimitSpace:
  StrCpy $R9 " "

; Extract the parameter
CopyParm:
  StrCpy $R8 $R0 1
  StrCmp $R8 "" Done
  StrCpy $R0 $R0 "" 1
  StrCmp $R8 $R9 Done
  StrCpy $R1 $R1$R8
  Goto CopyParm

Done:
  Pop $R8
  Pop $R9
  Exch $R1
  Exch
  Exch $R0
  Exch
FunctionEnd

Function GetFileName
  Exch $0 ; input string
  Push $1
  Push $2
  StrCpy $1 0
  loop:
    IntOp $1 $1 - 1
    StrCpy $2 $0 1 $1
    StrCmp $2 "" exit2
    StrCmp $2 "\" exit1 ; replace "\" with "." if you want file extension
    Goto loop
  exit1:
    IntOp $1 $1 + 1
    StrCpy $0 $0 "" $1
  exit2:
    Pop $2
    Pop $1
    Exch $0
FunctionEnd

;--------------------------------

; Now define all of our sections

Section "Aspect Ratio FX" SEC01
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  ; write uninstall strings
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Useful FX" "DisplayName" "Useful FX Content (remove only)"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Useful FX" "UninstallString" '"$INSTDIR\${SUB_DIR}\Useful FX-uninst.exe"'

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  WriteRegStr HKLM SOFTWARE\Play\Trinity "Useful FX" "$INSTDIR\${SUB_DIR}"
  SetDetailsPrint both
  DetailPrint "Installing files..."
  DetailPrint "Writing to $INSTDIR..."
  SetDetailsPrint none
  SetOutPath $INSTDIR
  File /r "${SRC_DIR}\${SUB_DIR}"

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Aspect Ratio FX"
  SetDetailsPrint both
  DetailPrint "Installing Aspect Ratio FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Aspect Ratio FX"

SectionEnd

Section "Blur FX" SEC02
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Blur FX"
  SetDetailsPrint both
  DetailPrint "Installing Blur FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Blur FX"

SectionEnd

Section "Color Bars" SEC03
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Color Bars"
  SetDetailsPrint both
  DetailPrint "Installing Color Bars..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Color Bars"

SectionEnd

Section "Color Correction" SEC04
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Color Correction"
  SetDetailsPrint both
  DetailPrint "Installing Color Correction..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Color Correction"

SectionEnd

Section "Color FX" SEC05
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Color FX"
  SetDetailsPrint both
  DetailPrint "Installing Color FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Color FX"

SectionEnd

Section "Countdowns" SEC06
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Countdowns"
  SetDetailsPrint both
  DetailPrint "Installing Countdowns..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Countdowns"

SectionEnd

Section "Dissolves" SEC07
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Dissolves"
  SetDetailsPrint both
  DetailPrint "Installing Dissolves..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Dissolves"

SectionEnd

Section "Elements" SEC08
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Elements"
  SetDetailsPrint both
  DetailPrint "Installing Elements..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Elements"

SectionEnd

Section "Film Look" SEC09
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Film Look"
  SetDetailsPrint both
  DetailPrint "Installing Film Look..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Film Look"

SectionEnd

Section "Frames" SEC10
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Frames"
  SetDetailsPrint both
  DetailPrint "Installing Frames..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Frames"

SectionEnd

Section "Key FX" SEC11
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Key FX"
  SetDetailsPrint both
  DetailPrint "Installing Key FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Key FX"

SectionEnd

Section "Pan and Zoom" SEC12
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Pan and Zoom"
  SetDetailsPrint both
  DetailPrint "Installing Pan and Zoom..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Pan and Zoom"

SectionEnd

Section "PIP FX" SEC13
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\PIP FX"
  SetDetailsPrint both
  DetailPrint "Installing PIP FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\PIP FX"

SectionEnd

Section "Recursive FX" SEC14
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Recursive FX"
  SetDetailsPrint both
  DetailPrint "Installing Recursive FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Recursive FX"

SectionEnd

Section "Spheres" SEC15
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Spheres"
  SetDetailsPrint both
  DetailPrint "Installing Spheres..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Spheres"

SectionEnd

Section "Timers" SEC16
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Timers"
  SetDetailsPrint both
  DetailPrint "Installing Timers..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Timers"

SectionEnd

Section "Video Wall" SEC17
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Video Wall"
  SetDetailsPrint both
  DetailPrint "Installing Video Wall..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Video Wall"

SectionEnd

Section "Warp FX" SEC18
;  LogSet on
  SetDetailsView show
  SetShellVarContext all

  SetOverwrite ifnewer ; NOT AN INSTRUCTION, NOT COUNTED IN SKIPPINGS

  SetDetailsPrint none

  SetOutPath $INSTDIR
  CreateDirectory "$INSTDIR\Warp FX"
  SetDetailsPrint both
  DetailPrint "Installing Warp FX..."
  SetDetailsPrint none
  File /r "${SRC_DIR}\${SUB_DIR}\Warp FX"

SectionEnd

Section "-last"
  SetDetailsView show
  SetShellVarContext all
  SetDetailsPrint none
  SetDetailsPrint both
;  WriteUninstaller "gc-uninst.exe"
  WriteUninstaller "$INSTDIR\${SUB_DIR}\Useful FX-uninst.exe"

  SetDetailsPrint none
  SetOutPath "$SMPROGRAMS\GlobeCaster\Bins\Useful FX"
  CreateShortcut "Uninstall Useful FX.lnk" "$INSTDIR\${SUB_DIR}\Useful FX-uninst.exe" "" "" "" "" "" "Uninstall Useful FX Content"
  SetDetailsPrint both

  SetDetailsPrint both
  DetailPrint "Finished"
  SetDetailsPrint none
SectionEnd


;--------------------------------

; Uninstaller

UninstallText "This will uninstall the Useful FX content.  Hit Uninstall to continue."

Section "Uninstall"

  ReadRegStr $INSTDIR HKLM "Software\Play\Trinity" "Useful FX"
  IfErrors 0 got_path
  MessageBox MB_OK|MB_ICONSTOP "Unable to locate Useful FX content installation path."
  Quit
got_path:

  SetDetailsPrint both
  DetailPrint "Removing shared files..."
  SetDetailsPrint none

  IntOp $DoAllNone 0 +
  ; Dec share counts for Useful FX files and unregister DLLs

  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Useful FX"

  SetDetailsPrint both
  
  RMDir /r "$INSTDIR"

  Delete /REBOOTOK "$SMPROGRAMS\GlobeCaster\Bins\Useful FX\Uninstall Useful FX.lnk"
  RMDir "$SMPROGRAMS\GlobeCaster\Bins\Useful FX"

  DeleteRegValue HKLM "Software\Play\Trinity" "Useful FX"

  Delete "$INSTDIR\Useful FX-uninst.exe"
SectionEnd

 ; GetWindowsVersion
 ;
 ; Based on Yazno's function, [url]http://yazno.tripod.com/powerpimpit/[/url]
 ; Updated by Joost Verburg
 ;
 ; Returns on top of stack
 ;
 ; Windows Version (95, 98, ME, NT x.x, 2000, XP, 2003)
 ; or
 ; '' (Unknown Windows Version)
 ;
 ; Usage:
 ;   Call GetWindowsVersion
 ;   Pop $R0
 ;   ; at this point $R0 is "NT 4.0" or whatnot

Function GetWindowsVersion

   Push $R0
   Push $R1

   ReadRegStr $R0 HKLM \
   "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion

   IfErrors 0 lbl_winnt

   ; we are not NT
   ReadRegStr $R0 HKLM \
   "SOFTWARE\Microsoft\Windows\CurrentVersion" VersionNumber

   StrCpy $R1 $R0 1
   StrCmp $R1 '4' 0 lbl_error

   StrCpy $R1 $R0 3

   StrCmp $R1 '4.0' lbl_win32_95
   StrCmp $R1 '4.9' lbl_win32_ME lbl_win32_98

   lbl_win32_95:
     StrCpy $R0 '95'
   Goto lbl_done

   lbl_win32_98:
     StrCpy $R0 '98'
   Goto lbl_done

   lbl_win32_ME:
     StrCpy $R0 'ME'
   Goto lbl_done

   lbl_winnt:

   StrCpy $R1 $R0 1

   StrCmp $R1 '3' lbl_winnt_x
   StrCmp $R1 '4' lbl_winnt_x

   StrCpy $R1 $R0 3

   StrCmp $R1 '5.0' lbl_winnt_2000
   StrCmp $R1 '5.1' lbl_winnt_XP
   StrCmp $R1 '5.2' lbl_winnt_2003 lbl_error

   lbl_winnt_x:
     StrCpy $R0 "NT $R0" 6
   Goto lbl_done

   lbl_winnt_2000:
     Strcpy $R0 '2000'
   Goto lbl_done

   lbl_winnt_XP:
     Strcpy $R0 'XP'
   Goto lbl_done

   lbl_winnt_2003:
     Strcpy $R0 '2003'
   Goto lbl_done

   lbl_error:
     Strcpy $R0 ''
   lbl_done:

   Pop $R1
   Exch $R0

FunctionEnd