REM @echo off
SET PROMPT=[$T]$S

SET SECURE_PASSWORD=%1
if not defined SECURE_PASSWORD goto :missing_arguments


:rest_of_script
REM net user bigstepdevel %SECURE_PASSWORD% /add /Y
REM net user bigstepdevel %SECURE_PASSWORD%


SET VM_IP_ADDRESS=192.168.137.2
SET HOST_IP_ADDRESS=192.168.137.3

WMIC USERACCOUNT WHERE "Name='bigstepdevel'" SET PasswordExpires=FALSE

REM Add exception to Windows Firewall to make sharing work (most importantly).
netsh advfirewall firewall add rule name="Allow from %VM_IP_ADDRESS%" dir=in action=allow protocol=ANY remoteip=%VM_IP_ADDRESS%
REM When all else fails run this (without REM):
REM Netsh advfirewall set allprofiles state off

REM The ExportVHosts folder will contain folders with local subdomains as names. Each will automatically become an Apache2 document root.
mkdir C:\ExportVHosts
CACLS C:\ExportVHosts /T /E /G bigstepdevel:F
net share ExportVHosts=C:\ExportVHosts /grant:bigstepdevel,FULL /users:20

SET share_exit_code = %errorlevel%

mklink /d /j C:\ExportVHosts\192.168.137.2 C:\ExportVHosts\bsi.vmware.local

REM Hide the bigstepdevel user at the Logon screen.
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts" /f
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /f
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v bigstepdevel /t REG_DWORD /d 0 /f

REM Fix some memory issue with caching large file transfers, helps a file server which... shares files.
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v LargeSystemCache /t REG_DWORD /d 1 /f

REM Configure the share service to allow many simultaneous open file handles.
reg add "HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" /v Size /t REG_DWORD /d 3 /f

REM Fix Microsoft Windows KB281308 which prevents linux from mounting shares in certain conditions.
reg add "HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" /v DisableStrictNameChecking /t REG_DWORD /d 1 /f

REM Fix browsers to skip auto proxy settings and load much faster.
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v AutoDetect /t REG_DWORD /d 0 /f

REM Disable Windows DNS cache because it would cache "not found" queries, and this is a nuissance when using a local VM DNS cache.
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters" /v MaxCacheEntryTtlLimit /t REG_DWORD /d 1 /f
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters" /v MaxCacheTtl /t REG_DWORD /d 0 /f
reg add "HKLM\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters" /v MaxNegativeCacheTtl /t REG_DWORD /d 0 /f

REM Disable ICMP redirect because packets sent from VMs are duplicated by Windows.
reg add "HKLM\System\CurrentControlSet\Services\TcpIp\Parameters" /v EnableICMPRedirect /t REG_DWORD /d 0 /f


REM If you were using something else, sorry. You fix it back.
REM net config workstation
REM Does "Workstation domain"=="WORKGROUP"?
Wmic computersystem where name="%computername%" call joindomainorworkgroup name="WORKGROUP"

REM Load Windows registry settings into services
net stop lanmanserver /Y
net start lanmanserver
net start browser
REM Or open `services.msc` and restart the "Server" service.

REM TODO: keep hosts clean somehow?
echo: >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% vm >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% mysql.vm >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% ordering.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% billing.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% bsi.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% bsiadmin.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% rapid.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% rapid.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% bsa.bigstep.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% bsa.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% repo.vmware.local >> %SystemRoot%\System32\Drivers\etc\hosts
echo %VM_IP_ADDRESS% begisreports.vm >> %SystemRoot%\System32\Drivers\etc\hosts
echo %HOST_IP_ADDRESS% physicalhost >> %SystemRoot%\System32\Drivers\etc\hosts
echo: >> %SystemRoot%\System32\Drivers\etc\hosts


ipconfig /flushdns

echo The ExportVHosts password is now: %SECURE_PASSWORD%
echo If you already have a VM, configure this password in /etc/auto.smbshares
echo If you don't have a VM yet, then ignore it, it will be changed later.

exit /b %share_exit_code%


:missing_arguments
SET /P SECURE_PASSWORD=Enter ExportVHosts share password (can be changed with net user bigstepdevel newsecretpassword): 
if not defined SECURE_PASSWORD goto :missing_arguments
goto rest_of_script
