!define SNAME "Portable Virtual PC"
!define APPX "$EXEDIR\Appdata\app\Virtual PC.exe"
!define SPLASH "splash.gif"
!define MUSIC "splash.wav"
!define SETTINGS "$EXEDIR\Appdata\Virtual Machines\Settings.ini"

!include WinVer.nsh
!include StrFunc.nsh
${StrRep}
!include "FileFunc.nsh"
!include "nsProcess.nsh"
!include "Registry.nsh"
!include "Locate.nsh"
!insertmacro "GetParameters"

var TempResult
var MajorVersion
var MinorVersion
var BuildNumber
var PlatformID
var CSDVersion

CRCCheck off
AutoCloseWindow True
SilentInstall silent
WindowIcon off
XPSTYLE on 

Name "${SNAME}"
Icon "${SNAME}.ico"
Caption "${SNAME} Launcher"
OutFile "${SNAME}.exe"

LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf"
VIProductVersion "0.1.0.0"
VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "${SNAME} Launcher"
VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "© tpr 2007"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "${SNAME}"
VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "0.1"

Section "Check if ${SNAME} is already running"
    ${GetParameters} $R0
    System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${SNAME}") i .r1 ?e'
    Pop $1
    StrCmp $1 0 main_exe abort
    main_exe:
    ${nsProcess::FindProcess} "Virtual PC.exe" $1
    StrCmp $1 603 launch abort1
    abort:
    MessageBox MB_OK "${SNAME} is already running."
    Abort
    abort1:
    MessageBox MB_OK "Virtual PC is already running."
    Abort
    launch:
SectionEnd

Section "Splash"
    SetOutPath "$EXEDIR\Appdata\bin\Options"
    IfFileExists "$OUTDIR\${SPLASH}" music
    File "${SPLASH}"
    music:
    IfFileExists "$OUTDIR\${MUSIC}" splash
    File "${MUSIC}"   
    splash:
    newadvsplash::play /NOUNLOAD "${MUSIC}"
    newadvsplash::show /NOUNLOAD 20000 100 1200 -1 /BANNER "$OUTDIR\${SPLASH}"
    newadvsplash::hwnd /NOUNLOAD
    Pop $0
    AnimGif::play /NOUNLOAD /HALIGN=CENTER /VALIGN=CENTER /hwnd=$0 "$OUTDIR\${SPLASH}"
SectionEnd

Section "MSXML6"
    IfFileExists "$SYSDIR\msxml6.dll" noinstall
    Version::IsWindowsXP
    Pop $TempResult
    StrCmp $TempResult "0" vista
    StrCpy "$TempResult" "Windows XP"
    Goto SP
    vista:
    Call GetWindowsVersion
    Pop $TempResult
    StrCmp $TempResult "0" SP
    StrCpy "$TempResult" "Vista"
    Goto SP
    SP:
    Version::GetWindowsVersion
    Pop $MajorVersion
    Pop $MinorVersion
    Pop $BuildNumber
    Pop $PlatformID
    Pop $CSDVersion
    Goto winxp
    winxp:
    StrCmp "$TempResult" "Windows XP" sp1 windowsvista
    sp1:
    StrCmp "$CSDVersion" "Service Pack 1" msxml_xpsp1 sp2
    sp2:
    StrCmp "$CSDVersion" "Service Pack 2" msxml_xpsp2 End
    msxml_xpsp1:
    ExecWait 'MSIEXEC.EXE /i "$EXEDIR\Appdata\bin\MSXML\msxml6.msi" /qn /norestart' $0
    WriteINIStr "${SETTINGS}" "Settings" "msxml" "msxml6.msi"
    Goto End
    msxml_xpsp2:
    ExecWait 'MSIEXEC.EXE /i "$EXEDIR\Appdata\bin\MSXML\msxml6_x86.msi" /qn /norestart' $0
    WriteINIStr "${SETTINGS}" "Settings" "msxml" "msxml6_x86.msi"
    Goto End
    noinstall:
    WriteINIStr "${SETTINGS}" "Settings" "msxml" "already installed"
    Goto End
    windowsvista:
    StrCmp "$TempResult" "Vista" End
    newadvsplash::stop
    AnimGif::stop
    MessageBox MB_OK "Your OS is not supported by Virtual PC 2007"
    End:
SectionEnd

Section "VMM"
    nsSCM::QueryStatus "VMMDriver" 
    Pop $0    
    StrCmp $0 "success" novmm
    nsSCM::Install "VMMDriver" "VMM Driver" 1 3 "$EXEDIR\Appdata\bin\VMM\VMM.sys" "" "" "" ""
    Sleep 100
    nsSCM::Start "VMMDriver" 
    novmm:
SectionEnd

Section "VMNetService"
    nsExec::Exec '$EXEDIR\Appdata\bin\VMNetSrv\snetcfg.exe -v -l "$EXEDIR\Appdata\bin\VMNetSrv\VMNetSrv.inf" -c s -i cntx_vpcnets2'
SectionEnd

Section "Registry"
    ${registry::SaveKey} "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual PC\5.0\Registration" "$EXEDIR\Appdata\bin\Options\localreg" "/G=1" $R0
    Sleep 200
    DeleteRegKey HKLM "Software\Microsoft\Virtual PC"
    Sleep 100
    WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Virtual PC\5.0\Registration" "PID" "74216-270-0016043-41167"
SectionEnd

Section "Options.xml"
    IfFileExists "$EXEDIR\Appdata\bin\Options\Options.xml" skip    
    SetOutPath "$EXEDIR\Appdata\bin\Options"
    File Options.xml
    File VPCKeyboard.dll
    Sleep 200
    skip:
    Rename "$APPDATA\Microsoft\Virtual PC\Options.xml" "$APPDATA\Microsoft\Virtual PC\Options.xml.old"
    Rename "$APPDATA\Microsoft\Virtual PC\VPCKeyboard.dll" "$APPDATA\Microsoft\Virtual PC\VPCKeyboard.dll.old"
    CreateDirectory "$APPDATA\Microsoft\Virtual PC\Virtual Machines"
    Sleep 100
    CopyFiles /SILENT "$EXEDIR\Appdata\bin\Options\Options.xml" "$APPDATA\Microsoft\Virtual PC"
    CopyFiles /SILENT "$EXEDIR\Appdata\bin\Options\VPCKeyboard.dll" "$APPDATA\Microsoft\Virtual PC"
SectionEnd

Section "Virtual Machines"
    Rename "$DOCUMENTS\My Virtual Machines" "$DOCUMENTS\My Virtual Machines_backup"
    Rename "$APPDATA\Microsoft\Virtual PC\Virtual Machines" "$APPDATA\Microsoft\Virtual PC\Virtual Machines_backup"
    Sleep 200
    CreateDirectory "$DOCUMENTS\My Virtual Machines"
    CreateDirectory "$APPDATA\Microsoft\Virtual PC\Virtual Machines"
    Sleep 300
    ClearErrors
    FindFirst $0 $1 "$EXEDIR\Appdata\Virtual Machines\*.vmc"
    loop1:
    StrCmp "$1" "" done
    ${StrRep} "$1" "$1" ".vmc" ""
    IfFileExists "$EXEDIR\Appdata\Virtual Machines\$1.vmc" shortcut next
    shortcut:
    CreateShortCut "$APPDATA\Microsoft\Virtual PC\Virtual Machines\$1_portable.lnk" "$EXEDIR\Appdata\Virtual Machines\$1.vmc"
    next:
    FindNext $0 $1
    Goto loop1
    done:
SectionEnd

Section "Killa"
    ${nsProcess::KillProcess} "msiexec.exe" $R0
    ${nsProcess::KillProcess} "snetcfg.exe" $R0
	;${nsProcess::KillProcess} "SERVIC~1.exe" $R0
SectionEnd

Section "Launch"
	newadvsplash::stop
	AnimGif::stop
	Sleep 200
	ExecWait "$EXEDIR\Appdata\app\Virtual PC.exe"
	Sleep 500
SectionEnd

Section "Cleanup"
	Sleep 500
	Delete "$EXEDIR\Appdata\Virtual Machines\*.lnk"
	Sleep 200
	CopyFiles /SILENT "$APPDATA\Microsoft\Virtual PC\Virtual Machines\*.lnk" "$EXEDIR\Appdata\Virtual Machines"
	Sleep 300
	nsSCM::Stop "VMMDriver" 
	Sleep 200
	nsSCM::Remove "VMMDriver"
	CopyFiles /SILENT "$APPDATA\Microsoft\Virtual PC\Options.xml" "$EXEDIR\Appdata\bin\Options"
	CopyFiles /SILENT "$APPDATA\Microsoft\Virtual PC\VPCKeyboard.dll" "$EXEDIR\Appdata\bin\Options"
	Sleep 500
	Delete "$APPDATA\Microsoft\Virtual PC\Options.xml"
	Delete "$APPDATA\Microsoft\Virtual PC\VPCKeyboard.dll"
	Sleep 200
	DeleteRegValue HKLM "Software\Microsoft\Virtual PC\5.0\Registration" "PID"
	Sleep 100
	DeleteRegKey /ifempty HKLM "Software\Microsoft\Virtual PC\5.0\Registration"
	DeleteRegKey /ifempty HKLM "Software\Microsoft\Virtual PC\5.0"
	DeleteRegKey /ifempty HKLM "Software\Microsoft\Virtual PC"
	${registry::RestoreKey} "$EXEDIR\Appdata\bin\Options\localreg" $R0
	Sleep 200
	Delete "$EXEDIR\Appdata\bin\Options\localreg"
	ClearErrors
	FindFirst $0 $1 "$EXEDIR\Appdata\Virtual Machines\*.vmc"
	loop:
	StrCmp "$1" "" done
	${StrRep} "$1" "$1" ".vmc" ""
	CopyFiles /SILENT "$APPDATA\Microsoft\Virtual PC\Virtual Machines\$1_portable.lnk" "$EXEDIR\Appdata\Virtual Machines"
	Sleep 100
	Delete "$APPDATA\Microsoft\Virtual PC\Virtual Machines\$1_portable.lnk"
	FindNext $0 $1
	Goto loop
	done:
	Rename "$APPDATA\Microsoft\Virtual PC\Options.xml.old" "$APPDATA\Microsoft\Virtual PC\Options.xml"
	Rename "$APPDATA\Microsoft\Virtual PC\VPCKeyboard.dll.old" "$APPDATA\Microsoft\Virtual PC\VPCKeyboard.dll"
	Sleep 300
	RMDir "$APPDATA\Microsoft\Virtual PC\Virtual Machines"
	Sleep 100
	RMDir "$APPDATA\Microsoft\Virtual PC"

	${locate::Open} "$DOCUMENTS\My Virtual Machines" "/D=0 /DE=1 /M=*.vmc" $9
	list:
	${locate::Find} $9 $1 $2 $3 $4 $5 $6
	StrCmp $1 "" nomore
	${StrRep} "$2" "$1" "$2" ""
	${StrRep} "$2" "$2" ".vmc" ""
	${StrRep} "$2" "$2" "\" ""
	
	CopyFiles /SILENT "$1" "$EXEDIR\Appdata\Virtual Machines"
	CreateShortCut "$EXEDIR\Appdata\Virtual Machines\$2_portable.lnk" "$EXEDIR\Appdata\Virtual Machines\$2.vmc"
	Sleep 100
	Delete "$DOCUMENTS\My Virtual Machines\$2\$2.vmc"
	RMDir /r "$DOCUMENTS\My Virtual Machines\$2"
	Goto list
	nomore:
	${locate::Close} $9
	
	Sleep 300
	RMDir "$DOCUMENTS\My Virtual Machines"
	nsExec::Exec '$EXEDIR\Appdata\bin\VMNetSrv\snetcfg.exe -u cntx_vpcnets2'
	
;MSXML6 Uninstall
    ReadINIStr $0 "${SETTINGS}" "Settings" "msxml"
    StrCmp $0 "already installed" no_uninstall
    StrCmp $0 "msxml6.msi" sp1_uninstall sp2_uninstall
    sp1_uninstall:
    ExecWait 'MSIEXEC.EXE /x "$EXEDIR\Appdata\bin\MSXML\msxml6.msi" /qn /norestart' $0
    sp2_uninstall:
    ExecWait 'MSIEXEC.EXE /x "$EXEDIR\Appdata\bin\MSXML\msxml6_x86.msi" /qn /norestart' $0
    no_uninstall:
SectionEnd