@echo off
title ARMCC Compiler Setup Tool
net session >nul 2>&1 
if not "%errorLevel%" == "0" ( 
echo Oops: This tools must run with administrator permissions! 
echo it will popup the UAC dialog, please click [Yes] to continue. 
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" 
echo UAC.ShellExecute "%~f0", "%*", "", "runas", 1 >> "%temp%\getadmin.vbs" 
"%temp%\getadmin.vbs" 
exit /b 0
)

echo.
echo CD work dir
%~d0 && cd "%~dp0"
echo  Compiler Root: %CD%

echo.
echo Setup Environment Variables ...
setx /M ARMLMD_LICENSE_FILE %CD%\ds_license.lic

echo.
echo Setup firewall to disable network for ARMCC ...
setlocal enabledelayedexpansion

set folder="bin"
set rulePrefix=AC5-%folder%-
echo setup folder: %folder%
for %%f in (%CD%\%folder%\*.exe) do (
  set name=%%~nf
  echo  block '!name!.exe'
  echo   - clean old rules
  netsh advfirewall firewall delete rule name="AUser Block %rulePrefix%!name!"
  echo   - create new rules
  netsh advfirewall firewall add rule name="AUser Block %rulePrefix%!name!" dir=out program="%%f" action=block
  netsh advfirewall firewall add rule name="AUser Block %rulePrefix%!name!" dir=in program="%%f" action=block
)

set folder="bin64"
set rulePrefix=AC5-%folder%-
echo setup folder: %folder%
for %%f in (%CD%\%folder%\*.exe) do (
  set name=%%~nf
  echo  block '!name!.exe'
  echo   - clean old rules
  netsh advfirewall firewall delete rule name="AUser Block %rulePrefix%!name!"
  echo   - create new rules
  netsh advfirewall firewall add rule name="AUser Block %rulePrefix%!name!" dir=out program="%%f" action=block
  netsh advfirewall firewall add rule name="AUser Block %rulePrefix%!name!" dir=in program="%%f" action=block
)

endlocal

echo.
echo All Done.
pause