@echo off

echo Starting Uninstallation of Nmap...  Time = %time% %date% >> ../../log/NmapUninstallation.log

:: Define the paths to Uninstall.exe
set "path1=C:\Program Files\Npcap\Uninstall.exe"
set "path2=C:\Program Files (x86)\Npcap\Uninstall.exe"
:: Set the current directory to network
cd ..\..\plugin\network
:: Define the path to the "nmap" folder
set "nmapDir=%cd%\nmap"
:: Define nmap exe
set "nmapExe=nmap.exe"

echo current working directory is: "%cd%" >> ../../log/NmapUninstallation.log
echo Nmap directory is: "%nmapDir%" >> ../../log/NmapUninstallation.log
for /d %%D in (*) do (
    echo folder %%D >> ../../log/NmapUninstallation.log
)

:: Task 1: Check and execute Uninstall.exe in the first path
if exist "%path1%" (
    echo Task 1: Uninstall.exe found in "%path1%". Executing silently... >> ../../log/NmapUninstallation.log
    start /wait "" "%path1%" /S  :: Use /S or /VERYSILENT for silent uninstall
) else (
    :: Task 1: Check and execute Uninstall.exe in the second path
    if exist "%path2%" (
        echo Task 1: Uninstall.exe found in "%path2%". Executing silently... >> ../../log/NmapUninstallation.log
        start /wait "" "%path2%" /S  :: Use /S or /VERYSILENT for silent uninstall
    ) else (
        echo Task 1: Uninstall.exe not found in either path. >> ../../log/NmapUninstallation.log
    )
)


:: Check if the "nmap" folder exists
if not exist "%nmapDir%" (
    echo "nmap" folder does not exist. Exiting... >> ../../log/NmapUninstallation.log
    exit /b
)
:: Change the working directory to the Nmap folder
pushd "%nmapDir"

:: Task 2: Check and execute vc_redist.x86.exe process
tasklist /fi "IMAGENAME eq vc_redist.x86.exe" 2>nul | find /i "vc_redist.x86.exe" >nul

:: If the process is found, kill it
if %errorlevel% == 0 (
    taskkill /f /im "vc_redist.x86.exe"
    echo Task 2: Process vc_redist.x86.exe terminated. >> ../../log/NmapUninstallation.log
) else (
    echo Task 2: vc_redist.x86.exe is not running. >> ../../log/NmapUninstallation.log
)

:: Task 3: Check and execute nmap.exe process
tasklist /fi "IMAGENAME eq %nmapExe%" 2>nul | find /i "%nmapExe%" >nul

:: If the process is found, kill it
if %errorlevel% == 0 (
    taskkill /f /im "%nmapExe%"
    echo Task 3: Process %nmapExe% terminated. >> ../../log/NmapUninstallation.log
) else (
    echo Task 3: %nmapExe% is not running. >> ../../log/NmapUninstallation.log
)

:: Restore the previous working directory
popd

echo Invoked Uninstallation of Nmap...  Time = %time% %date% >> ../../log/NmapUninstallation.log

echo =============================================================================================== >> ../../log/NmapUninstallation.log