!include LogicLib.nsh
!include Sections.nsh
!include "MUI.nsh"

!include "WordFunc.nsh"
!include "MUI2.nsh"

!insertmacro WordReplace
!insertmacro WordInsert

; Root directory
!define ProjectPath ".."
!define LicenseFile ${ProjectPath}\doc\license\BBPlayer\license.txt
!define InstallerPath ${ProjectPath}\INSTALLER

; registry key
!define BBPLAYERRegistryPath SOFTWARE\Cleinsoft\BBPlayer
!define BBPlayerRegistrySubkey 'Version'
!define BBPlayerRegistrySubDir 'BBPlayerDir'
!define InstallerVersion  1430

;!define ReleaseVersion

; Name of installer
Name "Black box Player 1.4.30"
; Installer package file name
!ifdef ReleaseVersion
OutFile "BBPlayer_${InstallerVersion}.exe"
!else
OutFile "BBPlayer_${InstallerVersion}_dbg.exe"
!endif

; install directory
!define APP_NAME "BBPlayer"
RequestExecutionLevel admin
InstallDir   "$PROGRAMFILES\${APP_NAME}"

;--------------------------------

; Pages
Var STARTMENU_FOLDER
Var MUI_TEMP
  !define MUI_ABORTWARNING
  !define MUI_ICON "..\src\BBPlayer\icon1.ico"
  !define MUI_UNICON "..\src\BBPlayer\icon1.ico"
  
  !define MUI_LICENSEPAGE_BUTTON "Confirm"
  
  !insertmacro MUI_PAGE_LICENSE "${LicenseFile}"
  !insertmacro MUI_PAGE_DIRECTORY

  ;Start Menu Folder Page Configuration
  !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKLM"
  !define MUI_STARTMENUPAGE_REGISTRY_KEY BBPLAYERRegistryPath
  !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Program Mannager"

  !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES

  !insertmacro MUI_LANGUAGE "English"

!define InstallAllFile '!insertmacro InstallAllFileMacro'

!macro InstallAllFileMacro InstallPath ComponentName DataPath
   SetOutPath ${InstallPath}\${ComponentName}
   File /r "${DataPath}\${ComponentName}\*.*"
!macroend

Var OldVersion
Var OldPath

InstType "Standard"
InstType "Full"

ShowInstDetails show
ShowUninstDetails show

;--------------------------------
; Install section
Section "-Hidden BBPLAYER Installer Basic" DESC_HiddenBBPLAYERInstallerBasic

; Install path
SetOutPath $INSTDIR
; copy an uninstaller
writeUninstaller $INSTDIR\uninstall.exe
; write current version
WriteRegDWORD HKLM ${BBPLAYERRegistryPath} ${BBPLAYERRegistrySubkey} ${InstallerVersion}
WriteRegStr HKLM ${BBPLAYERRegistryPath} ${BBPLAYERRegistrySubDir} $INSTDIR

; create short cuts
SetShellVarContext all
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
!insertmacro MUI_STARTMENU_WRITE_END

; convert the copywrite file
ExecWait '"$INSTDIR\someprogram.exe" some parameters'

SectionEnd

SectionGroup "Main"
Section "Main" MainSection
  SectionIn 1 2
  
  
  ; copy document files
   ${InstallAllFile} $INSTDIR 'doc' ${ProjectPath}
   CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\license.lnk" "$INSTDIR\doc\license\BBPlayer\license.txt" "" "$INSTDIR\doc\license\BBPlayer\license.txt" 0
   
  ; copy libraries and headers
  ${InstallAllFile} $INSTDIR 'captured_image' ${ProjectPath}
  ${InstallAllFile} $INSTDIR 'plugins' ${ProjectPath}
  ${InstallAllFile} $INSTDIR 'skin' ${ProjectPath}
  !ifndef ReleaseVersion
  ${InstallAllFile} $INSTDIR 'src' ${ProjectPath}
  ${InstallAllFile} $INSTDIR 'Installer' ${ProjectPath}
  ${InstallAllFile} $INSTDIR 'Install_tool' ${ProjectPath} 
  !endif
  
  SetOutPath "$INSTDIR"
  File "${ProjectPath}\*.*"
  
  SetShellVarContext all
  CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\BBPlayer.lnk" "$INSTDIR\BBPlayer.exe" "" "$INSTDIR\BBPlayer.exe" 0

  !ifdef ReleaseVersion
  Delete "$INSTDIR\BBPlayer_dbg.exe"
  !endif

SectionEnd 

SectionGroupEnd

section "Uninstall"

SetShellVarContext all
  delete $INSTDIR\*.*
  RMDir /r "$INSTDIR"

  Delete "$INSTDIR\Uninstall.exe"

  !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
  Delete "$SMPROGRAMS\$MUI_TEMP\Uninstall.lnk"
  Delete "$SMPROGRAMS\$STARTMENU_FOLDER\license.lnk"
  Delete "$SMPROGRAMS\$STARTMENU_FOLDER\BBPlayer.lnk"

  RMDir /r "$SMPROGRAMS\$MUI_TEMP"
  RMDir /r "$SMPROGRAMS\$STARTMENU"
  
  DeleteRegKey HKLM ${BBPLAYERRegistryPath}
  
sectionEnd

LangString DESC_SecInstallerBasic ${LANG_ENGLISH} "Install Program Mannager"
LangString DESC_MainSection ${LANG_ENGLISH} "Main"

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
   !insertmacro MUI_DESCRIPTION_TEXT ${MainSection} $(DESC_MainSection)
   !insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_HiddenBBPLAYERInstallerBasic)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

Function .onInit

   ReadRegDword $OldVersion HKLM ${BBPLAYERRegistryPath} ${BBPLAYERRegistrySubkey}
   ReadRegDword $OldPath HKLM ${BBPLAYERRegistryPath} ${BBPLAYERRegistrySubDir}

   ${If} $OldVersion != ""
   ${AndIf} $OldPath == ""
          MessageBox MB_OK  "Uninstall previous BBPLAYER. Cancel installation."
          abort
   ${EndIf}

   ${If} $OldVersion != ""
   ${AndIf} $OldPath != ""
          MessageBox MB_YESNO "Uninstall previous BBPLAYER ?" IDYES NoCancelAbort
          abort
          NoCancelAbort:
          ExecWait '"$OldPath\uninstall.exe" _?=$OldPath'
   ${EndIf}
   
FunctionEnd

Function un.onInit

FunctionEnd
