;========================================================================
;=== Created by OwD : 15.05.2003. Last modified : 17.02.2004. ver : 3 ===
;========================================================================
;General
;=======
!define VER_NAME "X Codec Pack"
!define VER_NAMES "xcodecpack"
!define VER_MAJOR 1
!define VER_MINOR 6
!define VER_REVISION 1
!define VER_BUILD 83
!define VER_FILE "${VER_MAJOR}${VER_MINOR}${VER_REVISION}"
!define VER_DISPLAY "${VER_MAJOR}.${VER_MINOR}${VER_REVISION} build ${VER_BUILD}"

;Compression
;===========
SetCompressor lzma

;Configuration
;=============
OutFile "exe\${VER_NAMES}.exe"

InstType "full install"
InstType "codecs only"

InstallDir "$PROGRAMFILES\${VER_NAME}"
InstallDirRegKey HKLM "Software\${VER_NAME}" ""

ShowInstDetails nevershow
ShowUninstDetails nevershow
;BrandingText " | Nullsoft Install System | "
XPStyle on

;Header Files
;============
!include "MUI.nsh"
!include "Sections.nsh"
!include "UpgradeDLL.nsh"
!include "LogicLib.nsh"
!include "include\AddShareDLL.nsh"
!include "include\RemoveSharedDLL.nsh"
;!include "include\WindowsVer.nsh"

;Configuration / Names
;=====================
Name "${VER_NAME}"
Caption "${VER_NAME} ${VER_DISPLAY} Setup"

;User / Variables
;================
Var STARTMENU_FOLDER

;Interface Settings
;==================
!define MUI_ABORTWARNING
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "images\modern-header.bmp"
!define MUI_WELCOMEFINISHPAGE_BITMAP "images\modern-wizard.bmp"
!define MUI_HEADERIMAGE_UNBITMAP "images\modern-header.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "images\modern-wizard.bmp"
!define MUI_ICON "icons\setup.ico"
!define MUI_UNICON "icons\unsetup.ico"
!define MUI_COMPONENTSPAGE_SMALLDESC
;!define MUI_COMPONENTSPAGE_NODESC

;Pages
;=====
 !define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the installation of\r\n ${VER_NAME}.\r\n\r\n ${VER_NAME} ${VER_DISPLAY} containes:\r\n\r\t - DivX Codec 5.05b / 5.11\r\t - XviD Codec 0.92 CVS / 1.0 RC1\r\t - AC3 Filter 0.70b\r\t - Matrix Mixer 0.30b\r\t - DivX AntiFreeze 0.4\r\t - DivX G400 Filter 2.83\r\n\r\n\r\nClick Next to continue ..."
!insertmacro MUI_PAGE_WELCOME
Page custom PageReinstall PageLeaveReinstall
!insertmacro MUI_PAGE_DIRECTORY
 !define MUI_PAGE_CUSTOMFUNCTION_LEAVE PageLeaveComponents
!insertmacro MUI_PAGE_COMPONENTS
 !define MUI_STARTMENUPAGE_REGISTRY_ROOT HKCU
 !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\${VER_NAME}"
 !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
!insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
!insertmacro MUI_PAGE_INSTFILES
 !define MUI_FINISHPAGE_LINK "Visit the website for the latest news, FAQs and support"
 !define MUI_FINISHPAGE_LINK_LOCATION "http://www.owd.go.ro"
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_WELCOME
 !define MUI_PAGE_CUSTOMFUNCTION_LEAVE un.PageLeaveComponents
!insertmacro MUI_UNPAGE_COMPONENTS
!insertmacro MUI_UNPAGE_INSTFILES

;Languages
;=========
!insertmacro MUI_LANGUAGE "English"

;Reserve Files
;=============
ReserveFile "reinstall.ini"
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS

;Version Information
;===================
VIProductVersion "1.6.0.0"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "${VER_NAME}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "OwD Software"
VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "© OwD Software"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "X Codec Pack"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "${VER_FILE}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "Build Number" "${VER_BUILD}"
;VIAddVersionKey /LANG=${LANG_ENGLISH} "Build Date" "${__TIMESTAMP__}"

;Installer Sections
;==================
SubSection /e !Codecs

SubSection /e "DivX Codec"
	
Section /O "version 5.05b" SecDivX01
	SetDetailsPrint textonly
	DetailPrint "X Codec Pack | Installing DivX Codec..."
	SetDetailsPrint listonly
	SectionIn 1 2
	SetOutPath "$INSTDIR\DivX"
	File "Files\DivX\5.05\README.TXT"
	File "Files\DivX\5.05\LICENSE.TXT"
	File "Files\DivX\5.05\CONFIG.EXE"
	SetOutPath "$SYSDIR"
	File "Files\DivX\5.05\DIVX.DLL"
	!insertmacro UpgradeDLL "Files\DivX\5.05\DIVXDEC.AX" "$SYSDIR\DIVXDEC.AX" $SYSDIR
	Push "$SYSDIR\DIVXDEC.AX"
	Call AddSharedDLL
	SetOutPath "$INSTDIR"
SectionEnd

Section "version 5.11" SecDivX02
	SetDetailsPrint textonly
	DetailPrint "X Codec Pack | Installing DivX Codec..."
	SetDetailsPrint listonly
	SetOutPath "$INSTDIR\DivX"
	File "Files\DivX\5.11\README.TXT"
	File "Files\DivX\5.11\LICENSE.TXT"
	File "Files\DivX\5.11\CONFIG.EXE"
	SetOutPath "$SYSDIR"
	File "Files\DivX\5.11\DIVX.DLL"
	!insertmacro UpgradeDLL "Files\DivX\5.11\DIVXDEC.AX" "$SYSDIR\DIVXDEC.AX" $SYSDIR
	Push "$SYSDIR\DIVXDEC.AX"
	Call AddSharedDLL
	SetOutPath "$INSTDIR"
SectionEnd

SubSectionEnd

SubSection /e "XviD Codec"

Section "version 0.92 CVS (Nic's)" SecXviD01
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Installing XviD Codec..."
  SetDetailsPrint listonly
	SetOutPath "$INSTDIR\XviD"
	File "Files\XviD\0.92\AVIC.EXE"
	File "Files\XviD\0.92\MINICALC.EXE"
	File "Files\XviD\0.92\MINICALC.TXT"
	File "Files\XviD\0.92\README.TXT"
	SetOutPath "$SYSDIR"
	File "Files\XviD\0.92\XVID.DLL"
	File "Files\XviD\0.92\XVID.AX"
	RegDLL "$SYSDIR\XVID.AX"
	SetOutPath "$INSTDIR"
SectionEnd

Section /O "version 1.0 RC1 (Koepi's)" SecXviD02
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Installing XviD Codec..."
  SetDetailsPrint listonly
	SectionIn 1 2
	SetOutPath "$INSTDIR\XviD"
	File "Files\XviD\1.0 RC1\AVIC.EXE"
	File "Files\XviD\1.0 RC1\OGMCALC.EXE"
	File "Files\XviD\1.0 RC1\STATSREADER.EXE"
	File "Files\XviD\1.0 RC1\MINICALC.EXE"
	File "Files\XviD\1.0 RC1\RELEASENOTES.TXT"
	File "Files\XviD\1.0 RC1\STATSREADER.TXT"
	File "Files\XviD\1.0 RC1\XviD_Quant_Matrices.zip"
	SetOutPath "$SYSDIR"
	File "Files\XviD\1.0 RC1\XVIDCORE.DLL"
	File "Files\XviD\1.0 RC1\XVIDVFW.DLL"
	File "Files\XviD\1.0 RC1\XVID.AX"
	RegDLL "$SYSDIR\XVID.AX"
	SetOutPath "$INSTDIR"
SectionEnd

SubSectionEnd

SubSectionEnd

SubSection /e !Filters

Section "AC3 Filter 0.70b" SecAC3
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Installing AC3 Filter..."
  SetDetailsPrint listonly
	SectionIn 1
	SetOutPath "$INSTDIR\AC3Filter"
	File "Files\AC3 Filter\*.*"
	SetOutPath "$INSTDIR\AC3Filter\pic"
	File "Files\AC3 Filter\pic\*.*"
	SetOutPath "$INSTDIR\AC3Filter"
	File "Files\AC3 Filter\ax\ac3filter.ax"
	RegDLL "ac3filter.ax"
	SetOutPath "$SYSDIR"
	File "Files\AC3 Filter\control\ac3filter.cpl"
	SetOutPath "$INSTDIR"
	;File "Files\AC3 Filter\filter\AC3Filter.exe"
	;ExecWait "$INSTDIR\AC3Filter.exe /S"
	;Delete "$INSTDIR\AC3Filter.exe"
SectionEnd

Section "Matrix Mixer 0.30b" SecMM
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Installing Matrix Mixer..."
  SetDetailsPrint listonly
	SectionIn 1
	SetOutPath "$INSTDIR\MatrixMixer"
	File "Files\Matrix Mixer\*.*"
	SetOutPath "$INSTDIR\MatrixMixer\pic"
	File "Files\Matrix Mixer\pic\*.*"
	SetOutPath "$INSTDIR\MatrixMixer"
	File "Files\Matrix Mixer\ax\matrix_mixer.ax"
	RegDLL "matrix_mixer.ax"
	SetOutPath "$INSTDIR"
SectionEnd

Section "DivX AntiFreeze 0.4" SecAF
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Installing DivX AntiFreeze Filter..."
  SetDetailsPrint listonly
	SectionIn 1
	SetOutPath "$SYSDIR"
	!insertmacro UpgradeDLL "Files\DivX AntiFreeze\DIVXAF.AX" "$SYSDIR\DIVXAF.AX" $SYSDIR
	Push "$SYSDIR\DIVXAF.AX"
	Call AddSharedDLL
	SetOutPath "$INSTDIR"
SectionEnd

Section "DivX G400 2.83" SecG400
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Installing DivX G400 Filter..."
  SetDetailsPrint listonly
	SectionIn 1
	SetOutPath "$INSTDIR\DivX G400"
	File "Files\DivX G400\DIVXG400.HTM"
	SetOutPath "$SYSDIR"
	File "Files\DivX G400\DIVXG400.AX"
	RegDLL "$SYSDIR\DIVXG400.AX"
	SetOutPath "$INSTDIR"
SectionEnd

SubSectionEnd

;Installer Functions
;===================
Function .onInit
	!insertmacro MUI_INSTALLOPTIONS_EXTRACT "tasks.ini"
	!insertmacro MUI_INSTALLOPTIONS_EXTRACT "reinstall.ini"
	StrCpy $1 -1
FunctionEnd

Function .onSelChange
	!insertmacro SectionFlagIsSet ${SecDivX01} ${SF_SELECTED} "" next1
	StrCmp $1 ${SecDivX02} "" dontUnselect1
	!insertmacro UnselectSection ${SecDivX02}

	dontUnselect1:
		StrCpy $1 ${SecDivX01}
	next1:
		!insertmacro SectionFlagIsSet ${SecDivX02} ${SF_SELECTED} "" next11
		StrCmp $1 ${SecDivX01} "" dontUnselect11
		!insertmacro UnselectSection ${SecDivX01}
dontUnselect11:
	StrCpy $1 ${SecDivX02}
next11:

	!insertmacro SectionFlagIsSet ${SecXviD01} ${SF_SELECTED} "" next2
	StrCmp $2 ${SecXviD02} "" dontUnselect2
	!insertmacro UnselectSection ${SecXviD02}

	dontUnselect2:
		StrCpy $2 ${SecXviD01}
	next2:
		!insertmacro SectionFlagIsSet ${SecXviD02} ${SF_SELECTED} "" next22
		StrCmp $2 ${SecXviD01} "" dontUnselect22
		!insertmacro UnselectSection ${SecXviD01}
dontUnselect22:
	StrCpy $2 ${SecXviD02}
next22:

FunctionEnd

Function PageReinstall
	ReadRegStr $R0 HKLM "Software\${VER_NAME}" ""
	StrCmp $R0 "" 0 +2
	Abort
	;Detect version
		ReadRegDWORD $R0 HKLM "Software\${VER_NAME}" "VersionMajor"
		IntCmp $R0 ${VER_MAJOR} minor_check new_version older_version
	minor_check:
		ReadRegDWORD $R0 HKLM "Software\${VER_NAME}" "VersionMinor"
		IntCmp $R0 ${VER_MINOR} revision_check new_version older_version
	revision_check:
		ReadRegDWORD $R0 HKLM "Software\${VER_NAME}" "VersionRevision"
		IntCmp $R0 ${VER_REVISION} build_check new_version older_version
	build_check:
		ReadRegDWORD $R0 HKLM "Software\${VER_NAME}" "VersionBuild"
		IntCmp $R0 ${VER_BUILD} same_version new_version older_version

	new_version:
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 1" "Text" "An older version of ${VER_NAME} is installed on your system. It's recommended that you uninstall the current version before installing. Select the operation you want to perform and click Next to continue."
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 2" "Text" "Uninstall before installing"
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 3" "Text" "Do not uninstall"
		!insertmacro MUI_HEADER_TEXT "Already Installed" "Choose how you want to install ${VER_NAME}."
		StrCpy $R0 "1"
		Goto reinst_start

	older_version:
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 1" "Text" "A newer version of ${VER_NAME} is already installed! It is not recommended that you install an older version. If you really want to install this older version, it's better to uninstall the current version first. Select the operation you want to perform and click Next to continue."
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 2" "Text" "Uninstall before installing"
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 3" "Text" "Do not uninstall"
		!insertmacro MUI_HEADER_TEXT "Already Installed" "Choose how you want to install ${VER_NAME}."
		StrCpy $R0 "1"
		Goto reinst_start

	same_version:
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 1" "Text" "${VER_NAME} ${VER_DISPLAY} is already installed. Select the operation you want to perform and click Next to continue."
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 2" "Text" "Add/Reinstall components"
		!insertmacro MUI_INSTALLOPTIONS_WRITE "reinstall.ini" "Field 3" "Text" "Uninstall ${VER_NAME}"
		!insertmacro MUI_HEADER_TEXT "Already Installed" "Choose the maintenance option to perform."
		StrCpy $R0 "2"

	reinst_start:
	!insertmacro MUI_INSTALLOPTIONS_DISPLAY "reinstall.ini"
FunctionEnd

Function PageLeaveReinstall
	!insertmacro MUI_INSTALLOPTIONS_READ $R1 "reinstall.ini" "Field 2" "State"

	StrCmp $R0 "1" 0 +2
		StrCmp $R1 "1" reinst_uninstall reinst_done
	StrCmp $R0 "2" 0 +3
		StrCmp $R1 "1" reinst_done reinst_uninstall

	reinst_uninstall:
	ReadRegStr $R1 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "UninstallString"

  ;Run uninstaller
	HideWindow
		ClearErrors
		ExecWait '$R1 _?=$INSTDIR'
		IfErrors no_remove_uninstaller
			Delete $R1
			RMDir $INSTDIR
		no_remove_uninstaller:
	StrCmp $R0 "2" 0 +2
		Quit
	BringToFront
	reinst_done:
FunctionEnd

Function PageLeaveComponents
	SectionGetFlags ${SecDivX01} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecDivX02} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecXviD01} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecXviD02} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecAC3} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecMM} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecAF} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${SecG400} $R0
	StrCmp $R0 1 done

  MessageBox MB_OK|MB_ICONEXCLAMATION "Please select at least one component!$\n  Then you will be able to proceed."
  Abort
  done:
FunctionEnd

Section -post
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Creating shortcuts & registry entries..."
  SetDetailsPrint listonly

	SetOutPath $INSTDIR
	File "icons\icons.icl"

  SectionGetFlags ${SecDivX01} $0
	SectionGetFlags ${SecDivX02} $1
	${if} $0 == 1
		WriteRegStr HKCU "Software\${VER_NAME}\Sections" "01" $0
	${else}
		WriteRegStr HKCU "Software\${VER_NAME}\Sections" "01" $1
	${endif}
  SectionGetFlags ${SecXviD01} $0
	SectionGetFlags ${SecXviD02} $1
	${if} $0 == 1
		WriteRegStr HKCU "Software\${VER_NAME}\Sections" "02" $0
	${else}
		WriteRegStr HKCU "Software\${VER_NAME}\Sections" "02" $1
	${endif}
  SectionGetFlags ${SecAC3} $0
  WriteRegStr HKCU "Software\${VER_NAME}\Sections" "03" $0
	SectionGetFlags ${SecMM} $0
  WriteRegStr HKCU "Software\${VER_NAME}\Sections" "04" $0
  SectionGetFlags ${SecAF} $0
  WriteRegStr HKCU "Software\${VER_NAME}\Sections" "05" $0
  SectionGetFlags ${SecG400} $0
  WriteRegStr HKCU "Software\${VER_NAME}\Sections" "06" $0

	WriteUninstaller "$INSTDIR\Uninstall.exe"
	WriteRegStr HKCU "Software\${VER_NAME}" "" $INSTDIR
	WriteRegStr HKCU "Software\${VER_NAME}" "Installer Language" $LANGUAGE
	WriteRegExpandStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "UninstallString" "$INSTDIR\Uninstall.exe"
	WriteRegExpandStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "InstallLocation" "$INSTDIR"

	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "DisplayName" "${VER_NAME}"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "DisplayIcon" "$INSTDIR\Uninstall.exe"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "DisplayVersion" "${VER_DISPLAY}"
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "VersionMajor" "${VER_MAJOR}"
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "VersionMinor" "${VER_MINOR}${VER_REVISION}"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "URLInfoAbout" "http://www.owd.go.ro"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}}" "Publisher" "OwD Software"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}" "Contact" "owd@go.ro"
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS" "NoRepair" "1"

	WriteRegStr HKLM "Software\${VER_NAME}" "" $INSTDIR
	WriteRegDWORD HKLM "Software\${VER_NAME}" "VersionMajor" "${VER_MAJOR}"
	WriteRegDWORD HKLM "Software\${VER_NAME}" "VersionMinor" "${VER_MINOR}"
	WriteRegDWORD HKLM "Software\${VER_NAME}" "VersionRevision" "${VER_REVISION}"
	WriteRegDWORD HKLM "Software\${VER_NAME}" "VersionBuild" "${VER_BUILD}"

	Call SMShortCuts
SectionEnd

Function SMShortCuts
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
			CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
		  SectionGetFlags ${SecDivX01} $0
			${if} $0 == '1'
				CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\DivX Codec Configuration.lnk" "$INSTDIR\DivX\Config.exe"
			${endif}
			SectionGetFlags ${SecDivX02} $0
			${if} $0 == '1'
				CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\DivX Codec Configuration.lnk" "$INSTDIR\DivX\Config.exe"
			${endif}
			SectionGetFlags ${SecAC3} $0
			${if} $0 == '1'
				CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\AC3 Configuration.lnk" "$SYSDIR\ac3filter.cpl" "" "$INSTDIR\icons.icl" 3 "SW_SHOWNORMAL" "" ""
			${endif}
			SectionGetFlags ${SecMM} $0
			${if} $0 == '1'
				Call SMShortCutsMM
			${endif}
			SectionGetFlags ${SecAF} $0
			${if} $0 == '1'
				Call SMShortCutsAF
			${endif}
			SectionGetFlags ${SecG400} $0
			${if} $0 == '1'
				Call SMShortCutsG400
			${endif}
	!insertmacro MUI_STARTMENU_WRITE_END
FunctionEnd

Function SMShortCutsMM
	Call GetWindowsVersion
	Pop $R0
	${Select} $R0
		${Case} "95"
			Goto win9x
		${Case} "98"
			Goto win9x
		${Case} "ME"
			Goto win9x
    ${Case} "2000"
      Goto winnt
		${Case} "XP"
			Goto winnt
		${Case} "2003"
			Goto winnt
  ${EndSelect}
	win9x:
		CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Matrix Mixer Configuration.lnk" "$WINDIR\rundll32.exe" '"$INSTDIR\MatrixMixer\matrix_mixer.ax",config'
	Goto end
	winnt:
		CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Matrix Mixer Configuration.lnk" "$SYSDIR\rundll32.exe" '"$INSTDIR\MatrixMixer\matrix_mixer.ax",config'
	Goto end
	end:
FunctionEnd

Function SMShortCutsAF
	Call GetWindowsVersion
	Pop $R0
	${Select} $R0
		${Case} "95"
			Goto win9x
		${Case} "98"
			Goto win9x
		${Case} "ME"
			Goto win9x
    ${Case} "2000"
      Goto winnt
		${Case} "XP"
			Goto winnt
		${Case} "2003"
			Goto winnt
  ${EndSelect}
	win9x:
		CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\DivX Anti-Freeze Configuration.lnk" "$WINDIR\rundll32.exe" DivXAF.ax,Configure
	Goto end
	winnt:
		CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\DivX Anti-Freeze Configuration.lnk" "$SYSDIR\rundll32.exe" DivXAF.ax,Configure
	Goto end
	end:
FunctionEnd

Function SMShortCutsG400
	Call GetWindowsVersion
	Pop $R0
	${Select} $R0
		${Case} "95"
			Goto win9x
		${Case} "98"
			Goto win9x
		${Case} "ME"
			Goto win9x
    ${Case} "2000"
      Goto winnt
		${Case} "XP"
			Goto winnt
		${Case} "2003"
			Goto winnt
  ${EndSelect}
	win9x:
		CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\DivX G400 Filter Configuration.lnk" "$WINDIR\rundll32.exe" DivXG400.ax,Configure
	Goto end
	winnt:
		CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\DivX G400 Filter Configuration.lnk" "$SYSDIR\rundll32.exe" DivXG400.ax,Configure
	Goto end
	end:
FunctionEnd

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

;Descriptions
;============
LangString DESC_SecDivX01 ${LANG_ENGLISH} "DivX® is a new format for digital video, much like MP3 is a format for digital music."
LangString DESC_SecDivX02 ${LANG_ENGLISH} "DivX® is a new format for digital video, much like MP3 is a format for digital music."
LangString DESC_SecXviD01 ${LANG_ENGLISH} "XviD is an ISO MPEG-4 compliant video codec. XviD is open source and just for educational purposes only."
LangString DESC_SecXviD02 ${LANG_ENGLISH} "XviD is an ISO MPEG-4 compliant video codec. XviD is open source and just for educational purposes only."
LangString DESC_SecAC3 ${LANG_ENGLISH} "AC3 format decoder for playback DVD and .AVI with AC3 audio track."
LangString DESC_SecMM ${LANG_ENGLISH} "Allows to upmix any audio source (not only ac3s) up to 5.1 format."
LangString DESC_SecAF ${LANG_ENGLISH} "Can be used for DivX and MS MPEG4 clips. DivX AF just avoids permanent freezings during playback."
LangString DESC_SecG400 ${LANG_ENGLISH} "Is codec-independent and should work within all players that use automatic DirectShow Filter Graph generation."

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
	!insertmacro MUI_DESCRIPTION_TEXT ${SecDivX01} $(DESC_SecDivX01)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecDivX02} $(DESC_SecDivX02)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecXviD01} $(DESC_SecXviD01)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecXviD02} $(DESC_SecXviD02)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecAC3} $(DESC_SecAC3)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecMM} $(DESC_SecMM)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecAF} $(DESC_SecAF)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecG400} $(DESC_SecG400)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

;Uninstaller Sections
;====================
Section "un.DivX Codec" unSecDivX
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing DivX Codec..."
  SetDetailsPrint listonly
	AddSize -1000
	Delete "$INSTDIR\DivX\config.exe"
	Delete "$INSTDIR\DivX\license.txt"
	Delete "$INSTDIR\DivX\readme.txt"
	RMDir "$INSTDIR\DivX"
	Delete $SYSDIR\divx.dll
	Push $SYSDIR\divxdec.ax
	Call un.RemoveSharedDLL
	Delete SYSDIR\divxdec.ax
	WriteRegStr HKCU "Software\${VER_NAME}\Sections" "01" "0"
SectionEnd

Section "un.XviD Codec" unSecXviD
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing XviD Codec..."
  SetDetailsPrint listonly
	AddSize -1100
	Delete "$INSTDIR\XviD\*.*"
	RMDir "$INSTDIR\XviD"
	Delete $SYSDIR\xvid.dll
	Delete $SYSDIR\xvidcore.dll
	Delete $SYSDIR\xvidvfw.dll
	UnRegDLL $SYSDIR\xvid.ax
	Delete $SYSDIR\xvid.ax
	WriteRegStr HKCU "Software\${VER_NAME}\Sections" "02" "0"
SectionEnd

Section "un.AC3 Filter" unSecAC3
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing AC3 Filter..."
  SetDetailsPrint listonly
	AddSize -747
	UnRegDLL "$INSTDIR\AC3Filter\ac3filter.ax"
	Delete "$INSTDIR\AC3Filter\ac3filter.ax"
	Delete "$INSTDIR\AC3Filter\pic\*.*"
	Delete "$INSTDIR\AC3Filter\*.*"
	RMDir "$INSTDIR\AC3Filter\pic"
	RMDir "$INSTDIR\AC3Filter"
	Delete "$SYSDIR\ac3filter.cpl"
	WriteRegStr HKCU "Software\${VER_NAME}\Sections" "03" "0"
SectionEnd

Section "un.Matrix Mixer" unSecMM
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing Matrix Mixer..."
  SetDetailsPrint listonly
	AddSize -417
	UnRegDLL "$INSTDIR\MatrixMixer\matrix_mixer.ax"
	Delete "$INSTDIR\MatrixMixer\matrix_mixer.ax"
	Delete "$INSTDIR\MatrixMixer\pic\*.*"
	Delete "$INSTDIR\MatrixMixer\*.*"
	RMDir "$INSTDIR\MatrixMixer\pic"
	RMDir "$INSTDIR\MatrixMixer"
	WriteRegStr HKCU "Software\${VER_NAME}\Sections" "04" "0"
SectionEnd

Section "un.DivX AntiFreeze" unSecAF
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing DivX AntiFreeze Filter..."
  SetDetailsPrint listonly
	AddSize -191
	Push $SYSDIR\DivXAF.ax
	Call un.RemoveSharedDLL
	Delete $SYSDIR\DivXAF.ax
	WriteRegStr HKCU "Software\${VER_NAME}\Sections" "05" "0"
SectionEnd

Section "un.DivX G400" unSecG400
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing DivX G400 Filter..."
  SetDetailsPrint listonly
	AddSize -401
	Delete "$INSTDIR\DivX G400\divxg400.htm"
	RMDir "$INSTDIR\DivX G400"
	UnRegDLL $SYSDIR\DivXG400.ax
	Delete $SYSDIR\DivXG400.ax
	WriteRegStr HKCU "Software\${VER_NAME}\Sections" "06" "0"
SectionEnd

Function un.onInit
	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "01"
	${if} $0 == 0
		!insertmacro UnselectSection ${unSecDivX}
		SectionSetText ${unSecDivX} ""
	${endif}

	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "02"
	${if} $0 == 0
		!insertmacro UnselectSection ${unSecXviD}
		SectionSetText ${unSecXviD} ""
	${endif}

	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "03"
	${if} $0 == 0
		!insertmacro UnselectSection ${unSecAC3}
		SectionSetText ${unSecAC3} ""
	${endif}

	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "04"
	${if} $0 == 0
		!insertmacro UnselectSection ${unSecMM}
		SectionSetText ${unSecMM} ""
	${endif}

	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "05"
	${if} $0 == 0
		!insertmacro UnselectSection ${unSecAF}
		SectionSetText ${unSecAF} ""
	${endif}

	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "06"
	${if} $0 == 0
		!insertmacro UnselectSection ${unSecG400}
		SectionSetText ${unSecG400} ""
	${endif}
FunctionEnd

Function un.PageLeaveComponents
	SectionGetFlags ${unSecDivX} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${unSecXviD} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${unSecAC3} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${unSecMM} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${unSecAF} $R0
	StrCmp $R0 1 done
	SectionGetFlags ${unSecG400} $R0
	StrCmp $R0 1 done

  MessageBox MB_OK|MB_ICONEXCLAMATION "Please select at least one component!$\n  Then you will be able to proceed."
  Abort
  done:
FunctionEnd

Section -un.post
  SetDetailsPrint textonly
  DetailPrint "X Codec Pack | Removing shortcuts & registry entries..."
  SetDetailsPrint listonly
	
	!insertmacro MUI_STARTMENU_GETFOLDER Application $1

	ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "01"
	${if} $0 == 0
		Delete "$SMPROGRAMS\$1\DivX Codec Configuration.lnk"
		ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "02"
		${if} $0 == 0
			ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "03"
			${if} $0 == 0
				Delete "$SMPROGRAMS\$1\AC3 Configuration.lnk"
				ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "04"
				${if} $0 == 0
					Delete "$SMPROGRAMS\$1\Matrix Mixer Configuration.lnk"
					ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "05"
					${if} $0 == 0
						Delete "$SMPROGRAMS\$1\DivX Anti-Freeze Configuration.lnk"
						ReadRegStr $0 HKCU "Software\${VER_NAME}\Sections" "06"
						${if} $0 == 0
							Delete "$SMPROGRAMS\$1\DivX G400 Filter Configuration.lnk"
							DeleteRegKey HKLM "SOFTWARE\${VER_NAME}"
							DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${VER_NAME}"
							DeleteRegKey HKCU "Software\${VER_NAME}\Sections"
							DeleteRegKey HKCU "Software\${VER_NAME}"
							Delete "$SMPROGRAMS\$1\Uninstall.lnk"
							RMDir "$SMPROGRAMS\$1"
							Delete "$INSTDIR\icons.icl"
							Delete "$INSTDIR\uninstall.exe"
							RMDir "$INSTDIR"
						${endif}
					${endif}
				${endif}
			${endif}
		${endif}
	${endif}
	SetAutoClose true
SectionEnd

LangString DESC_unSecDivX ${LANG_ENGLISH} "DivX® is a new format for digital video, much like MP3 is a format for digital music."
LangString DESC_unSecXviD ${LANG_ENGLISH} "XviD is an ISO MPEG-4 compliant video codec. XviD is open source and just for educational purposes only."
LangString DESC_unSecAC3 ${LANG_ENGLISH} "AC3 format decoder for playback DVD and .AVI with AC3 audio track."
LangString DESC_unSecMM ${LANG_ENGLISH} "Allows to upmix any audio source (not only ac3s) up to 5.1 format."
LangString DESC_unSecAF ${LANG_ENGLISH} "Can be used for DivX and MS MPEG4 clips. DivX AF just avoids permanent freezings during playback."
LangString DESC_unSecG400 ${LANG_ENGLISH} "Is codec-independent and should work within all players that use automatic DirectShow Filter Graph generation."

!insertmacro MUI_UNFUNCTION_DESCRIPTION_BEGIN
	!insertmacro MUI_DESCRIPTION_TEXT ${unSecDivX} $(DESC_unSecDivX)
	!insertmacro MUI_DESCRIPTION_TEXT ${unSecXviD} $(DESC_unSecXviD)
	!insertmacro MUI_DESCRIPTION_TEXT ${unSecAC3} $(DESC_unSecAC3)
	!insertmacro MUI_DESCRIPTION_TEXT ${unSecMM} $(DESC_unSecMM)
	!insertmacro MUI_DESCRIPTION_TEXT ${unSecAF} $(DESC_unSecAF)
	!insertmacro MUI_DESCRIPTION_TEXT ${unSecG400} $(DESC_unSecG400)
!insertmacro MUI_UNFUNCTION_DESCRIPTION_END