@echo off
setlocal enabledelayedexpansion

REM -------------------------
REM Apache Auto Update Script
REM -------------------------
REM Configurable Variables
set APACHE_SERVICE=Apache2.4
set APACHE_DIR=C:\ABSSolute\Apache24
set BACKUP_DIR=C:\ABSSolute\Apache24_backup
set TEMP_DIR=C:\ABSSolute\apache_update_temp
set ZIP_FILE=%TEMP_DIR%\apache-latest.zip

REM Hardcoded download URL (latest Apache binary for Windows x64, VS17 build)
set DOWNLOAD_URL=https://www.apachelounge.com/download/VS17/binaries/httpd-2.4.65-250724-win64-VS17.zip

REM Path to script directory (for local curl)
set SCRIPT_DIR=%~dp0

echo.
echo ==== Apache Auto Update Script ====
echo Using download URL: %DOWNLOAD_URL%
echo Script directory: %SCRIPT_DIR%
echo.

REM -------------------------
REM Stop Apache service
REM -------------------------
echo Stopping Apache service "%APACHE_SERVICE%"...
net stop "%APACHE_SERVICE%" >nul 2>&1

REM Prepare temporary folder
if exist "%TEMP_DIR%" rmdir /S /Q "%TEMP_DIR%"
mkdir "%TEMP_DIR%" 2>nul

REM Backup current Apache installation
echo Backing up current Apache installation...
if exist "%BACKUP_DIR%" rmdir /S /Q "%BACKUP_DIR%"
xcopy "%APACHE_DIR%" "%BACKUP_DIR%" /E /I /H /Y >nul

REM -------------------------
REM Download Apache ZIP
REM -------------------------
echo Downloading Apache using local curl...
"%SCRIPT_DIR%curl\bin\curl.exe" -L -o "%ZIP_FILE%" "%DOWNLOAD_URL%"
if errorlevel 1 (
    echo ERROR: Download failed. Aborting update.
    goto :end
)

REM -------------------------
REM Extract Apache ZIP
REM -------------------------
echo Extracting archive...
set SEVENZIP="C:\Program Files\7-Zip\7z.exe"
%SEVENZIP% x "%ZIP_FILE%" -o"%TEMP_DIR%" >nul
if errorlevel 1 (
    echo ERROR: Extraction failed. Aborting update.
    goto :end
)

REM -------------------------
REM Exclude critical config and data files
REM -------------------------
if not exist "%TEMP_DIR%" mkdir "%TEMP_DIR%"

(
    echo conf\
    echo conf\httpd.conf
    echo conf\extra\httpd-ssl.conf
    echo logs\
    echo htdocs\
) > "%TEMP_DIR%\exclude.txt"

REM -------------------------
REM Copy new files to Apache install
REM -------------------------
echo Updating Apache installation...
xcopy "%TEMP_DIR%\Apache24\*" "%APACHE_DIR%\" /E /I /H /Y /EXCLUDE:%TEMP_DIR%\exclude.txt
set COPY_RESULT=%ERRORLEVEL%

if %COPY_RESULT%==0 goto :copy_success
if %COPY_RESULT%==1 goto :copy_none
if %COPY_RESULT%==2 goto :copy_cancel
if %COPY_RESULT% GEQ 4 goto :copy_fail
goto :copy_end

:copy_success
echo Files copied successfully.
goto :copy_end

:copy_none
echo No files needed to be copied (already up-to-date or excluded).
goto :copy_end

:copy_cancel
echo Update canceled by user (CTRL+C).
goto :restore_and_exit

:copy_fail
echo ERROR: xcopy failed with code %COPY_RESULT%.
goto :restore_and_exit

:copy_end
REM -------------------------
REM Restart Apache
REM -------------------------
echo Starting Apache service "%APACHE_SERVICE%"...
net start "%APACHE_SERVICE%" >nul 2>&1

REM -------------------------
REM Clean up
REM -------------------------
echo Cleaning temporary files...
del "%ZIP_FILE%" >nul
rmdir /S /Q "%TEMP_DIR%" >nul

echo.
echo ==== Apache Update Completed Successfully ====
pause
goto :end

REM -------------------------
REM Restore backup & exit on error
REM -------------------------
:restore_and_exit
echo Restoring backup from "%BACKUP_DIR%" to "%APACHE_DIR%"...
if exist "%BACKUP_DIR%" (
    xcopy "%BACKUP_DIR%\*" "%APACHE_DIR%\" /E /I /H /Y >nul 2>&1
    echo Attempting to restart Apache service...
    net start "%APACHE_SERVICE%" >nul 2>&1
) else (
    echo No backup found to restore.
)
echo ABORTED: update failed.
pause
goto :end

:end
endlocal
exit /b
