@echo off

:: Copyright (c) 2011, Intel Corporation
:: All rights reserved.
::  
:: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
:: 
::     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
::     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
::     * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
:: 
:: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

:: rename winvnc4 - virus scanner work around
move winvnc4.exe.old winvnc4.exe

:: Apply VNC settings
regedit /s "%~dp0settings.reg"

:: generate password
set CHR1=%RANDOM%
set /a CHR1=(CHR1*10/32767)
set CHR2=%RANDOM%
set /a CHR2=(CHR2*10/32767)
set CHR3=%RANDOM%
set /a CHR3=(CHR3*10/32767)
set CHR4=%RANDOM%
set /a CHR4=(CHR4*10/32767)
set CHR5=%RANDOM%
set /a CHR5=(CHR5*10/32767)
set CHR6=%RANDOM%
set /a CHR6=(CHR6*10/32767)
set CHR7=%RANDOM%
set /a CHR7=(CHR7*10/32767)
set CHR8=%RANDOM%
set /a CHR8=(CHR8*10/32767)

set VNCPASS=%CHR1%%CHR2%%CHR3%%CHR4%%CHR5%%CHR6%%CHR7%%CHR8%

vncpasshash %VNCPASS% > vnchash.log
set /p HASH=<vnchash.log
reg ADD HKCU\Software\RealVNC\WinVNC4 /v Password /t REG_BINARY /d %HASH%
:: Turn off WinPE firewall
wpeutil disablefirewall
:: Start VNC Server minimized
start /MIN "" "%~dp0winvnc4.exe" -noconsole

:: Next Version
::
:: 1 - Optionally alert local user with IP & Random password. ~ to user consent.
:: 2 - Optionally ask local user for IP of console. Then, establist a VNC Connection.

:: Check for SOL. If loaded, use it to send messages to console.
start "" cmd /c vnc_alert_sol.bat %VNCPASS%

:end

