@echo off
SETLOCAL EnableDelayedExpansion
REM  Manual Vegas Pro installer
REM  Made by Paper because the Vegas installer
REM  wasn't fucking working...
REM
REM  Run as admin!!!

if not exist "%~dp0\bin\7zr.exe" (
	call :downloadfile "https://www.7-zip.org/a/7zr.exe" "%~dp0\bin\7zr.exe"
)

REM cls
echo Extracting Vegas files... please wait...
echo.

set "shared=C:\Program Files (x86)\Sony"
"%~dp0\bin\7zr.exe" x -y "%~dp0\7z\Vegas Pro 13.0.7z" -o"C:\Program Files\Sony"
"%~dp0\bin\7zr.exe" x -y "%~dp0\7z\Shared Plug-ins.7z" -o"C:\Program Files (x86)\Sony"
"%~dp0\bin\7zr.exe" x -y "%~dp0\7z\Start Menu.7z" -o"C:\ProgramData\Microsoft\Windows\Start Menu"
"%~dp0\bin\7zr.exe" x -y "%~dp0\7z\Installer.7z" -o"C:\Windows\Installer"
"%~dp0\bin\7zr.exe" x -y "%~dp0\7z\ProgramData.7z" -o"C:\ProgramData\Sony"

REM  need this to get audio plugins working...
for %%i in ("%shared%\Shared Plug-Ins\Audio_x64\*.dll") do (
	regsvr32 /s "%%i"
	if %errorlevel% equ 0 echo %%~nxi registered!
)

reg.exe import "%~dp0\vegas.reg"

echo Windows Registry Editor Version 5.00 > vegasuser.reg
echo.>> vegasuser.reg
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\vegas130.exe]>> vegasuser.reg
echo "C:\\Users\\%USERNAME%\\AppData\\Local\\Sony\\ErrorReport\\"="1">> vegasuser.reg
echo "C:\\Users\\%USERNAME%\\AppData\\Local\\Sony\\"="1">> vegasuser.reg
echo.>> vegasuser.reg
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\EEB0EEE18CB05E111AE90FD42AA3C585\InstallProperties]>> vegasuser.reg
echo "InstallSource"="C:\\Users\\Paper\\AppData\\Local\\Temp\\SonyInstall_1\\vegas130\\">>vegasuser.reg
echo.>> vegasuser.reg
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EEE0BEE-0BC8-11E5-A19E-F04DA23A5C58}]>> vegasuser.reg
echo "InstallSource"="C:\\Users\\Paper\\AppData\\Local\\Temp\\SonyInstall_1\\vegas130\\">>vegasuser.reg

reg.exe import "%~dp0\vegasuser.reg"

pause
goto :eof

:downloadfile
REM Downloads a file from a URL
REM Input 1 is the URL, input 2 is the file to output to
REM Compatible with PowerShell 2
set "major="
for /f "skip=3 delims=" %%i in ('powershell -Command "Get-Host | Select-Object Version"') do ( 
	if not defined major set "major=%%i"
)

if %major:~0,1% GEQ 2 (
	powershell -Command "& {$WebClient = New-Object System.Net.WebClient; $WebClient.DownloadFile('%~1', '%~2')}"
) else (
	"%~dp0\bin\curl.exe" -o "%~2" "%~1"
)
goto :eof
