!define PRODUCT_NAME "Winamp Icon Library Installer"
!define PRODUCT_VERSION "v1.0"
!define PRODUCT_DESCRIPTION "Winamp Icon Pack (Orange)"
!define PRODUCT_SUB_NAME "Winamp"
!define PRODUCT_SUB_VERSION "v5.58"
!define PRODUCT_DESCRIPTION_AUTHOR "Paweł Porwisz"
!define MUI_COMPONENTSPAGE_SMALLDESC
!define MUI_FINISHPAGE_RUN_TEXT "Run Winamp"
!define /date MyTIMESTAMP "%Y/%m/%d at %H:%M:%S"
!define Minimal_Version "5.5.8.2944" 

;Set Compression
SetCompress force
SetCompressor /solid lzma

; MUI 2.0 compatible ------
!include "MUI2.nsh"
!include "sections.nsh"
!include "LogicLib.nsh"
!include "WordFunc.nsh"
!include "winver.nsh" 

XPStyle on
BrandingText "${PRODUCT_NAME} ${PRODUCT_VERSION} by Koopa && Egg -- built on ${MyTIMESTAMP}"

; detect winamp path from uninstall string if available
InstallDirRegKey HKLM \
                 "Software\Microsoft\Windows\CurrentVersion\Uninstall\Winamp" \
                 "UninstallString"
				 
;Request Administrator Privileges
RequestExecutionLevel admin

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "graphics\modern-header.bmp"
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install-colorful.ico"
!define MUI_WELCOMEFINISHPAGE_BITMAP "graphics\modern-wizard.bmp"

;Run Custom Function On Start For Winamp Checks
!define MUI_CUSTOMFUNCTION_GUIINIT My_GUIInit

;Version information for Windows Explorer
VIProductVersion "1.0.0.0"
VIAddVersionKey "ProductName" "${PRODUCT_NAME} ${PRODUCT_VERSION}"
VIAddVersionKey "Comments" "${PRODUCT_NAME}"
VIAddVersionKey "LegalCopyright" "Copyright © 2010, Nullsoft Inc."
VIAddVersionKey "Company" "Nullsoft, Inc."
VIAddVersionKey "FileDescription" "${PRODUCT_DESCRIPTION} Installer"
VIAddVersionKey "FileVersion" "${PRODUCT_VERSION}"
VIAddVersionKey "ProductVersion" "${PRODUCT_VERSION}"


; Welcome page
	
!define MUI_WELCOMEPAGE_TITLE "Welcome to the ${PRODUCT_NAME} wizard"
!define MUI_WELCOMEPAGE_TEXT "$\r$\nThis will install ${PRODUCT_DESCRIPTION} by ${PRODUCT_DESCRIPTION_AUTHOR} for ${PRODUCT_SUB_NAME} ${PRODUCT_SUB_VERSION} or newer.$\r$\n"

!insertmacro MUI_PAGE_WELCOME				
													
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
DirText "Please select your Winamp path below; the installer will usually detect your Winamp installation path automatically (you will be able to proceed when Winamp is detected):"

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES


; Finish page
!define MUI_FINISHPAGE_RUN "$INSTDIR\winamp.exe"
!insertmacro MUI_PAGE_FINISH

; Language files
!insertmacro MUI_LANGUAGE "English"


; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Winamp_Icon_Pack_(Orange).exe"
InstallDir "$PROGRAMFILES\Winamp\"
ShowInstDetails show

Section "IconPack" SEC01
SetOutPath "$INSTDIR\Icons"
SetOverwrite on
File "Icons\WinampIconLibraryOrange.dll"
SetAutoClose true
SectionEnd

Function GetFileVersion

   !define GetFileVersion `!insertmacro GetFileVersionCall`
   !macro GetFileVersionCall _FILE _RESULT
      Push `${_FILE}`
      Call GetFileVersion
      Pop ${_RESULT}
   !macroend

   Exch $0
   Push $1
   Push $2
   Push $3
   Push $4
   Push $5
   Push $6
   ClearErrors

   GetDllVersion '$0' $1 $2
   IfErrors error
       IntOp $3 $1 / 0x00010000
       IntOp $4 $1 & 0x0000FFFF
       IntOp $5 $2 / 0x00010000
       IntOp $6 $2 & 0x0000FFFF
       StrCpy $0 '$3.$4.$5.$6'
   goto end

   error:
      SetErrors
      StrCpy $0 ''

   end:
   Pop $6
   Pop $5
   Pop $4
   Pop $3
   Pop $2
   Pop $1
   Exch $0

FunctionEnd

Function CheckWinampVersion

	${GetFileVersion} "$INSTDIR\winamp.exe" $R0 ; Get Winamp.exe version information, $R0 = Actual Version
		
	${if} $R0 != "" ; check if Version info is not empty
		${VersionCompare} $R0 ${Minimal_Version} $R1 ; $R1 = Result $R1=0  Versions are equal, $R1=1  Version1 is newer, $R1=2  Version2 is newer
		${if} $R1 = "2"
			MessageBox MB_OK "${PRODUCT_NAME} requires at least Winamp ${Minimal_Version} or above.$\r$\nPlease update your Winamp version first.$\r$\nInstallation will be aborted."
			Quit
		${EndIf}		
	${Else}
		MessageBox MB_OK "Winamp wasn't detected on this system.$\r$\nPlease install latest Winamp version from Winamp.com,$\r$\nbefore you can install this icon pack.$\r$\nInstallation will be aborted." ; version info is empty, something goes wrong. Display message and exit
		Quit
	${EndIf}
			
FunctionEnd

Function CheckWinampInstallation

	;MessageBox MB_OK "$INSTDIR\winamp.exe"

	${If} ${FileExists} "$INSTDIR\winamp.exe" ;check if Winamp.exe exists
		;
	${Else}
		MessageBox MB_OK "Winamp wasn't detected on this system.$\r$\nPlease install latest Winamp version from Winamp.com,$\r$\nbefore you can install ${PRODUCT_DESCRIPTION}.$\r$\nInstallation will be aborted." ;no winamp.exe
		Quit
	${EndIf}

FunctionEnd

Function CheckJTFEPresence

	${If} ${FileExists} "$INSTDIR\Plugins\gen_jumpex.dll"
	${Else}
		MessageBox MB_OK "The Jump to File plugin (gen_jumpex.dll) wasn't detected on this system.$\r$\nPlease reinstall latest Winamp and make sure JTFE is selected$\r$\nbefore you can install and use this icon library.$\r$\n$\r$\nInstallation will be aborted."
		Quit
	${EndIf}
FunctionEnd

;Detect running Winamp instances
Function .onInit

!define WINAMP_FILE_EXIT 40001

!ifdef INTERCEPT_MULTIPLE_INSTANCES
  System::Call 'kernel32::CreateMutexA(i 0, i 0, t "WinampMbApiSetup") i .r1 ?e'
  Pop $R0

  StrCmp $R0 0 noprevinst
    ReadRegStr $R0 HKCU "${PLUGIN_INSTREGKEY}" "WindowHandle"
    System::Call 'user32::SetForegroundWindow(i $R0) i ?e'
    Abort
	
  noprevinst:
!endif

  FindWindow $R0 "Winamp v1.x"
  IntCmp $R0 0 ok
    MessageBox MB_YESNO|MB_ICONEXCLAMATION "Please close all instances of Winamp before installing the icon library!$\r$\n\
Close Winamp now?" IDYES checkagain IDNO no
    checkagain:
      FindWindow $R0 "Winamp v1.x"
      IntCmp $R0 0 ok
      SendMessage $R0 ${WM_COMMAND} ${WINAMP_FILE_EXIT} 0
      Goto checkagain
    no:
       ; quit installer
  ok:
FunctionEnd

Function My_GUIInit
	Call CheckWinampInstallation
	Call CheckWinampVersion
	Call CheckJTFEPresence
FunctionEnd