@echo off

VER | find "XP" > NUL
IF %errorlevel% EQU 0 GOTO XP
VER | find "2000" > NUL
IF %errorlevel% EQU 0 GOTO 2000
VER | find "NT" > NUL
IF %errorlevel% EQU 0 GOTO NT
VER | find "Microsoft Windows [Version 6.0" > NUL
IF %errorlevel% EQU 0 GOTO Vista
VER | find "Microsoft Windows [Version 6.1" > NUL
IF %errorlevel% EQU 0 GOTO W7
VER | find "Microsoft Windows [Version 6.2" > NUL
IF %errorlevel% EQU 0 GOTO W8
VER | find "Microsoft Windows [Version 6.3" > NUL
IF %errorlevel% EQU 0 GOTO W81
VER | find "Microsoft Windows [Version 10." > NUL
IF %errorlevel% EQU 0 GOTO W10
VER | find "Microsoft Windows [Version 5" > NUL
IF %errorlevel% EQU 0 GOTO 2003

:NT
GOTO ERROR1

:2000
:XP
:2003
SET OSTYP=A
GOTO CHECKOSEND

:Vista
:W7
:W8
:W81
:W10
SET OSTYP=B

:CHECKOSEND

IF "%1" == "" GOTO ERROR2 >NUL 2>&1
IF %1 == A GOTO ACTIVATION >NUL 2>&1
IF %1 == D GOTO DEACTIVATION >NUL 2>&1
IF %1 == R GOTO RESETPROXYSETTINGS >NUL 2>&1
IF %1 == T GOTO STARTINT >NUL 2>&1
GOTO END

:ACTIVATION

REM -----------------------------------------------------------------------------------------------------
REM -- ProxySwitch settings -----------------------------------------------------------------------------
REM -----------------------------------------------------------------------------------------------------

SET "PROXYALL=ALL="
SET "PROXYHTTP=http="
SET "PROXYSECURE=https=127.0.0.1:8079"
SET "PROXYFTP=ftp="
SET "PROXYSOCKS=socks="

REM -----------------------------------------------------------------------------------------------------
REM -----------------------------------------------------------------------------------------------------
REM -----------------------------------------------------------------------------------------------------

IF NOT "%PROXYALL%" == "ALL=" SET "PROXYSERVERSTRING=%PROXYALL:~4%"&GOTO PROXYALL >NUL 2>&1

SET "HTTPTYP=NA"
SET "SECURETYP=NA"
SET "FTPTYPE=NA"
SET "SOCKSTYPE=NA"

IF "%PROXYHTTP%" == "http=" (SET "PROXYHTTP2=") ELSE (SET "PROXYHTTP2=%PROXYHTTP%"&SET "HTTPTYP=AV") >NUL 2>&1
IF "%PROXYSECURE%" == "https=" (SET "PROXYSECURE2=") ELSE (SET "PROXYSECURE2=%PROXYSECURE%"&SET "SECURETYP=AV") >NUL 2>&1
IF "%PROXYFTP%" == "ftp=" (SET "PROXYFTP2=") ELSE (SET "PROXYFTP2=%PROXYFTP%"&SET "FTPTYPE=AV") >NUL 2>&1
IF "%PROXYSOCKS%" == "socks=" (SET "PROXYSOCKS2=") ELSE (SET "PROXYSOCKS2=%PROXYSOCKS%"&SET "SOCKSTYPE=AV") >NUL 2>&1

SET "PROXYHTTP3="
SET "PROXYSECURE3="
SET "PROXYFTP3="

IF %HTTPTYP% == AV (IF %SECURETYP% == NA (IF %FTPTYPE% == NA (IF %SOCKSTYPE% == NA GOTO ENDTYPES))) >NUL 2>&1
IF %SECURETYP% == AV (IF %HTTPTYP% == NA (IF %FTPTYPE% == NA (IF %SOCKSTYPE% == NA GOTO ENDTYPES))) >NUL 2>&1
IF %FTPTYPE% == AV (IF %HTTPTYP% == NA (IF %SECURETYP% == NA (IF %SOCKSTYPE% == NA GOTO ENDTYPES))) >NUL 2>&1
IF %SOCKSTYPE% == AV (IF %HTTPTYP% == NA (IF %SECURETYP% == NA (IF %FTPTYPE% == NA GOTO ENDTYPES))) >NUL 2>&1

IF %HTTPTYP% == AV (IF %SECURETYP% == AV SET "PROXYHTTP3=;") >NUL 2>&1
IF %HTTPTYP% == AV (IF %FTPTYPE% == AV SET "PROXYHTTP3=;") >NUL 2>&1
IF %HTTPTYP% == AV (IF %SOCKSTYPE% == AV SET "PROXYHTTP3=;") >NUL 2>&1
IF %SECURETYP% == AV (IF %FTPTYPE% == AV SET "PROXYSECURE3=;") >NUL 2>&1
IF %SECURETYP% == AV (IF %SOCKSTYPE% == AV SET "PROXYSECURE3=;") >NUL 2>&1
IF %FTPTYPE% == AV (IF %SOCKSTYPE% == AV SET "PROXYFTP3=;") >NUL 2>&1

:ENDTYPES

SET "PROXYSERVERSTRING=%PROXYHTTP2%%PROXYHTTP3%%PROXYSECURE2%%PROXYSECURE3%%PROXYFTP2%%PROXYFTP3%%PROXYSOCKS2%"

:PROXYALL

SET "DataProxyEnable=NA"
SET "DataProxyEnable2=NA"
(FOR /F "tokens=2* delims=	 " %%a IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "-ProxyEnable"') DO SET "DataProxyEnable2=%%a"&SET "DataProxyEnable=%%b") >NUL 2>&1
IF NOT "%DataProxyEnable2%" == "REG_DWORD" GOTO NEXT1 >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyEnable" /t REG_DWORD /d %DataProxyEnable%) >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyEnable") >NUL 2>&1
:NEXT1

SET "DataProxyServer=NA"
SET "DataProxyServer2=NA"
(FOR /F "tokens=2* delims=	 " %%a IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "-ProxyServer"') DO SET "DataProxyServer2=%%a"&SET "DataProxyServer=%%b") >NUL 2>&1
IF NOT "%DataProxyServer2%" == "REG_SZ" GOTO NEXT2 >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyServer" /t REG_SZ /d %DataProxyServer%) >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyServer") >NUL 2>&1
:NEXT2
IF "%DataProxyServer%" == "NOAVIABLESETTINGS" (REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyServer") >NUL 2>&1

SET "DataProxyEnable=NA"
SET "DataProxyServer=NA"
SET "DataProxyEnable2=NA"
SET "DataProxyServer2=NA"
(FOR /F "tokens=2* delims=	 " %%a IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxyEnable"') DO SET "DataProxyEnable2=%%a"&SET "DataProxyEnable=%%b") >NUL 2>&1
(FOR /F "tokens=2* delims=	 " %%a IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "ProxyServer"') DO SET "DataProxyServer2=%%a"&SET "DataProxyServer=%%b") >NUL 2>&1
IF NOT "%DataProxyEnable2%" == "REG_DWORD" (SET "DataProxyEnable=0x0") >NUL 2>&1
IF NOT "%DataProxyServer2%" == "REG_SZ" (SET "DataProxyServer=NOAVIABLESETTINGS") >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyEnable" /t REG_DWORD /d %DataProxyEnable%) >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyServer" /t REG_SZ /d %DataProxyServer%) >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyEnable" /t REG_DWORD /d 0x1) >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyServer" /t REG_SZ /d %PROXYSERVERSTRING%) >NUL 2>&1

GOTO INITIAL

:DEACTIVATION

SET "DataProxyEnable=NA"
SET "DataProxyEnable2=NA"
(FOR /F "tokens=2* delims=	 " %%a IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "-ProxyEnable"') DO SET "DataProxyEnable2=%%a"&SET "DataProxyEnable=%%b") >NUL 2>&1
IF NOT "%DataProxyEnable2%" == "REG_DWORD" GOTO NEXT3 >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyEnable" /t REG_DWORD /d %DataProxyEnable%) >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyEnable") >NUL 2>&1
:NEXT3

SET "DataProxyServer=NA"
SET "DataProxyServer2=NA"
(FOR /F "tokens=2* delims=	 " %%a IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v "-ProxyServer"') DO SET "DataProxyServer2=%%a"&SET "DataProxyServer=%%b") >NUL 2>&1
IF NOT "%DataProxyServer2%" == "REG_SZ" GOTO NEXT4 >NUL 2>&1
(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyServer" /t REG_SZ /d %DataProxyServer%) >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyServer") >NUL 2>&1
:NEXT4
IF "%DataProxyServer%" == "NOAVIABLESETTINGS" (REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyServer") >NUL 2>&1

GOTO INITIAL

:RESETPROXYSETTINGS

(REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyEnable" /t REG_DWORD /d 0x0) >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyEnable") >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "-ProxyServer") >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyServer") >NUL 2>&1
(REG DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v "ProxyOverride") >NUL 2>&1

:INITIAL

cd Application
("InitialWindowsProxy.exe" current)
cd..

IF %1 == A GOTO END >NUL 2>&1
IF %1 == R GOTO END >NUL 2>&1

:STARTINT
Set DELTMPDIR=NA
(FOR /F "tokens=*" %%f in ('Dir "%TEMP%" /A:D /B /O:N ^|^ find /I "tmp" ^|^ find /I /V "."') DO FOR /F "tokens=*" %%g in ('Dir "%TEMP%\%%f" /A:D /B /O:N ^|^ find /I "py"') DO Set DELTMPDIR=%%f) >NUL 2>&1
IF "%DELTMPDIR%" == "NA" GOTO END
RMDIR /S /Q "%TEMP%\%DELTMPDIR%"
GOTO STARTINT

GOTO END

:ERROR1

CLS
ECHO.
ECHO.
ECHO  Wrong OS Version!
ECHO.
ECHO.
Pause

GOTO END

:ERROR2

CLS
ECHO.
ECHO.
ECHO  This batch file is used to change the proxy settings
ECHO  at the start of a program and this is terminated
ECHO  the proxy settings are restored to the previous state!
ECHO.
ECHO.
ECHO  To Activated the proxy setting, run batch file with parameter A
ECHO  ^(ProxySwitch.bat A^)
ECHO.
ECHO  To Deactivated the proxy setting, run batch file with parameter D
ECHO  ^(ProxySwitch.bat D^)
ECHO.
ECHO  To Reset the proxy setting, run batch file with parameter R
ECHO  ^(ProxySwitch.bat R^)
ECHO.
ECHO. 
ECHO. 
ECHO. 
ECHO. 
Pause

:END