@echo off
:: Check admin
reg.exe query "HKU\S-1-5-19">nul 2>&1
if %errorlevel% equ 1 goto UACPrompt

echo CONNECTOR

set SOURCE_WEB=https://850.co.il/REP/VPN/
set SOURCE_FOLDER=\\10.0.0.222\850\REP\VPN\
set DEFENDER_RUNNING="((get-service | where {$_.name -like '*windefend*'}).status -eq 'running')"
set PWRSHELL="%systemroot%\system32\windowspowershell\v1.0\powershell.exe" -ExecutionPolicy ByPass -NoProfile -command
set DWNLOAD=Import-Module BitsTransfer; Start-BitsTransfer
set DOWNLOAD_FILE=VpnClient2.zip

SET SOWA_DIR=%APPDATA%\SOWA

:: KILL VPNCLIENT
taskkill /f /im VpnClient2.exe >nul 2>&1

::MAKE A FOLDERS
set VPNFOLDER="%APPDATA%\VPNCLIENT"
if not exist %VPNFOLDER% MD %VPNFOLDER%
::CREATE CURRENT BACKUP FOLDER 
if not exist %VPNFOLDER%\BAK MD %VPNFOLDER%\BAK
for /f %%i in ('powershell -Command "Get-Date -Format 'dd-MM-yyyy_HH-mm-ss'"') do set DATETIME=%%i
set DIRNAME=%VPNFOLDER%\BAK\%DATETIME%
echo %DIRNAME%
mkdir %DIRNAME%

:: BACKUP CONFIG FILES
move %VPNFOLDER%\*.xml %DIRNAME% >nul 2>&1
copy %VPNFOLDER%\*.cfg %DIRNAME% >nul 2>&1
xcopy %VPNFOLDER%\APP %VPNFOLDER%\BAK\APP /Y /E /I

::ADD ANTIVIRUS EXCLUSION
%PWRSHELL% IF %DEFENDER_RUNNING% {add-mppreference -exclusionpath %VPNFOLDER%}

:: REMOVE FOLDERS AND FILES
if exist %VPNFOLDER%\%DOWNLOAD_FILE% del /q %VPNFOLDER%\%DOWNLOAD_FILE%
if exist %VPNFOLDER%\VpnClient2.exe del /q %VPNFOLDER%\VpnClient2.exe
if exist %VPNFOLDER%\VpnClient2.exe.xml del /q %VPNFOLDER%\VpnClient2.exe.xml
del /q %VPNFOLDER%\v*.txt
if exist %VPNFOLDER%\APP RMDIR  /s /q %VPNFOLDER%\APP
if exist %VPNFOLDER%\RESOURCES RMDIR  /s /q %VPNFOLDER%\RESOURCES
if exist %VPNFOLDER%\SUPPORT RMDIR  /s /q %VPNFOLDER%\SUPPORT


:: REMOVING OLD SOWA FILES
if exist %windir%\.sowad.exe del %windir%\.sowad.exe /Q
if exist %windir%\sowa.exe del %windir%\sowa.exe /Q
:: REMOVING OLD SOWA EXCLUSIONS
%PWRSHELL% IF %DEFENDER_RUNNING% {Remove-MpPreference -exclusionpath %SOWA_DIR%}"
%PWRSHELL% IF %DEFENDER_RUNNING% {Remove-MpPreference -exclusionpath c:\windows\.sowad.exe}"
%PWRSHELL% IF %DEFENDER_RUNNING% {Remove-MpPreference -exclusionpath c:\windows\sowa.exe}"
%PWRSHELL% IF %DEFENDER_RUNNING% {Remove-mppreference -exclusionpath %temp%}"

:: DOWNLOAD FILE FROM LAN
set NAU=primanas& set NAP=Ch11kch@
set NAS=10.0.10.100
set WEBSERVERLOCAL=10.0.0.222
ping -n 2 -w 1000 %NAS%|find "TTL=">nul&&net use \\%NAS%\04belka /user:%NAU% %NAP%
::ping -n 2 -w 1000 %WEBSERVERLOCAL%|find "TTL=">nul&&copy "%SOURCE_FOLDER%\%DOWNLOAD_FILE%" %VPNFOLDER%\.
ping -n 2 -w 1000 %NAS%|find "TTL=">nul&&if not exist "%VPNFOLDER%\%A_FULLUSERNAME%.cfg" copy "\\%NAS%\04belka\BELKO\ExportToExcel\%A_COMPANY_NAME%\%A_FULLUSERNAME%\*.cfg" %VPNFOLDER%\. /Y

:: DOWNLOAD FILE FROM INTERNET
if not exist %VPNFOLDER%\%DOWNLOAD_FILE% (
	%PWRSHELL% "%DWNLOAD% '%SOURCE_WEB%//%DOWNLOAD_FILE%' '%VPNFOLDER%\.'"
)
%PWRSHELL% "Expand-Archive '%VPNFOLDER%\%DOWNLOAD_FILE%'  -DestinationPath '%VPNFOLDER%' -Force"
:: REMOVE ZIP FILE
del /q %VPNFOLDER%\%DOWNLOAD_FILE%

:: DOWNLOAD SHORTCUT MAKER
set SCM=%PROGRAMDATA%\INSTALL\SHORTCUTMAKER\
if not exist %SCM% md %SCM%
if not exist %SCM%Shortcut.exe %PWRSHELL% "%DWNLOAD% '%SOURCE_WEB%//Shortcut.exe' '%SCM%.'"

:: CREATE LINK TO DESKTOP
del "%USERPROFILE%\Desktop\ISRANET CONNECTOR.lnk"
IF NOT EXIST "%USERPROFILE%\Desktop\ISRANET CONNECTOR.lnk" (
	%SCM%Shortcut.exe /f:"%USERPROFILE%\Desktop\ISRANET CONNECTOR.lnk" /a:c /t:%VPNFOLDER%\VpnClient2.exe /w:%VPNFOLDER%
)

IF NOT EXIST "%USERPROFILE%\Desktop\ISRANET CONNECTOR.lnk" (
	mklink "%userprofile%\desktop\ISRANET CONNECTOR" %VPNFOLDER%\VpnClient2.exe
)

explorer %VPNFOLDER%
cd  %VPNFOLDER%
start "" VpnClient2.exe
del "%~dpnx0" /Q

exit /B

::RUN AS ADMIN
:UACPrompt
@powershell Start-Process %~fs0 -Verb ruNAS
exit
::END RUN AS ADMIN
