@echo off
:: Set the code page to UTF-8 for proper character encoding
CHCP 65001 >nul

:: Enable delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION

:: Define the log file path
SET "LOG_FILE=%~dp0install_log.txt"

:: Initialize error flag
SET "ERROR_OCCURRED=0"

:: Start logging
(
    echo ===========================
    echo OpenSSL Installation Script Started
    echo Date and Time: %DATE% %TIME%
    echo ===========================
    echo.

    :: Check for administrative privileges using 'net session'
    echo Checking for administrative privileges...
    >nul 2>&1 net session
    IF !ERRORLEVEL! NEQ 0 (
        echo [ERROR] Administrative privileges are required. Please run the script as an administrator.
        echo Press any key to exit...
        pause >nul
        EXIT /B 1
    )
    echo [SUCCESS] Administrative privileges confirmed.
    echo.

    :: Define system directories
    SET "SYS32=%SYSTEMROOT%\System32"
    SET "SYSWOW64=%SYSTEMROOT%\SysWOW64"

    echo System directories set:
    echo SYS32 = "!SYS32!"
    echo SYSWOW64 = "!SYSWOW64!"
    echo.

    :: Define the script directory
    SET "SCRIPT_DIR=%~dp0"

    :: Ensure SCRIPT_DIR ends with a backslash
    IF NOT "!SCRIPT_DIR:~-1!"=="\" (
        SET "SCRIPT_DIR=!SCRIPT_DIR!\"
    )

    echo Script directory: "!SCRIPT_DIR!"
    echo.

    :: List of libraries to copy
    SET "LIBS=libcrypto-3.dll libssl-3.dll libcrypto-3-x64.dll libssl-3-x64.dll libcrypto-1_1.dll libssl-1_1.dll libcrypto-1_1-x64.dll libssl-1_1-x64.dll"

    echo Libraries to copy:
    echo !LIBS!
    echo.

    :: Start the installation process
    echo Starting OpenSSL libraries installation...
    echo.

    :: Loop through each library and copy accordingly
    FOR %%L IN (!LIBS!) DO (
        CALL :copy_lib "%%L"
    )

    :: Check if any errors occurred
    IF "!ERROR_OCCURRED!"=="0" (
        echo All operations completed successfully.
        echo [SUCCESS] OpenSSL libraries installed successfully!
    ) ELSE (
        echo Some errors occurred during the installation.
        echo [ERROR] OpenSSL libraries installation completed with errors.
    )

    echo.
    echo ===========================
    echo OpenSSL Installation Script Finished
    echo Date and Time: %DATE% %TIME%
    echo ===========================
) >"%LOG_FILE%" 2>&1

:: Display the log file contents to the user
TYPE "%LOG_FILE%"

:: Wait for user input before closing
echo.
echo Press any key to close...
pause >nul

ENDLOCAL
EXIT /B 0

:: Function to copy libraries
:copy_lib
SET "FILE=%~1"
SET "FILENAME=%~nx1"

echo Processing file: "!FILE!"

IF EXIST "!SCRIPT_DIR!!FILE!" (
    echo File found: "!SCRIPT_DIR!!FILE!"
    
    :: Determine if the file is 64-bit or 32-bit based on the filename
    ECHO "!FILENAME!" | FIND /I "-x64" >nul
    IF !ERRORLEVEL! EQU 0 (
        :: 64-bit libraries are copied to System32
        SET "DEST_DIR=!SYS32!"
        echo Determined as 64-bit. Target directory: "!DEST_DIR!"
    ) ELSE (
        :: 32-bit libraries are copied to SysWOW64
        SET "DEST_DIR=!SYSWOW64!"
        echo Determined as 32-bit. Target directory: "!DEST_DIR!"
    )

    :: Check if the destination directory exists
    IF EXIST "!DEST_DIR!" (
        :: Check if the file already exists in the destination
        IF EXIST "!DEST_DIR!\!FILENAME!" (
            echo Creating a backup of the existing "!FILENAME!"...
            COPY /Y "!DEST_DIR!\!FILENAME!" "!DEST_DIR!\!FILENAME!.bak" >nul
            IF !ERRORLEVEL! NEQ 0 (
                echo [ERROR] Failed to create a backup for "!FILENAME!".
                SET /A ERROR_OCCURRED+=1
            ) ELSE (
                echo [SUCCESS] Backup for "!FILENAME!" created.
            )
        ) ELSE (
            echo No existing file found in the target directory. Backup not required.
        )

        :: Copy the library to the destination directory
        echo Copying "!FILENAME!" to "!DEST_DIR!"...
        COPY /Y "!SCRIPT_DIR!!FILE!" "!DEST_DIR!\" >nul
        IF !ERRORLEVEL! NEQ 0 (
            echo [ERROR] Failed to copy "!FILENAME!" to "!DEST_DIR!".
            SET /A ERROR_OCCURRED+=1
        ) ELSE (
            echo [SUCCESS] "!FILENAME!" successfully copied to "!DEST_DIR!".
        )
    ) ELSE (
        echo [ERROR] Destination directory "!DEST_DIR!" does not exist. Skipping copy.
        SET /A ERROR_OCCURRED+=1
    )
) ELSE (
    echo [ERROR] File "!FILE!" not found in "!SCRIPT_DIR!". Skipping.
    SET /A ERROR_OCCURRED+=1
)
echo.
GOTO :EOF
