@echo off
setlocal enabledelayedexpansion

set INSTALL_SCRIPT=install.sh

echo Building %INSTALL_SCRIPT%...

> "%INSTALL_SCRIPT%" echo #!/bin/bash
>> "%INSTALL_SCRIPT%" echo # WireGuard Policy Firewall Installer/Uninstaller
>> "%INSTALL_SCRIPT%" echo # This file is auto-generated. Do not edit directly. Run build.sh or build.bat instead.
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo set -euo pipefail
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo if [[ $EUID -ne 0 ]]; then
>> "%INSTALL_SCRIPT%" echo    echo "This script must be run as root." 
>> "%INSTALL_SCRIPT%" echo    exit 1
>> "%INSTALL_SCRIPT%" echo fi
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo install_policy^(^) {
>> "%INSTALL_SCRIPT%" echo     echo "Installing WireGuard Policy Firewall..."
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Checking dependencies..."
>> "%INSTALL_SCRIPT%" echo     apt-get update -y ^|^| true
>> "%INSTALL_SCRIPT%" echo     apt-get install -y jq inotify-tools ipset iptables ^|^| true
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Writing scripts to /usr/local/bin/..."
>> "%INSTALL_SCRIPT%" echo.

call :AppendFile wg-policy-lib.sh /usr/local/bin/wg-policy-lib.sh EOF_WG_POLICY_LIB
call :AppendFile wg-policy-engine.sh /usr/local/bin/wg-policy-engine.sh EOF_WG_POLICY_ENGINE
call :AppendFile wg-policy-cleanup.sh /usr/local/bin/wg-policy-cleanup.sh EOF_WG_POLICY_CLEANUP
call :AppendFile wg-sync-policy.sh /usr/local/bin/wg-sync-policy.sh EOF_WG_SYNC_POLICY
call :AppendFile wg-sync-watch.sh /usr/local/bin/wg-sync-watch.sh EOF_WG_SYNC_WATCH
call :AppendFile wg-policy-ctl /usr/local/bin/wg-policy-ctl EOF_WG_POLICY_CTL

>> "%INSTALL_SCRIPT%" echo     echo "Writing systemd units to /etc/systemd/system/..."
>> "%INSTALL_SCRIPT%" echo.

call :AppendFile wg-policy.service /etc/systemd/system/wg-policy.service EOF_WG_POLICY_SERVICE
call :AppendFile wg-policy-health.service /etc/systemd/system/wg-policy-health.service EOF_WG_POLICY_HEALTH_SERVICE
call :AppendFile wg-policy-health.timer /etc/systemd/system/wg-policy-health.timer EOF_WG_POLICY_HEALTH_TIMER

>> "%INSTALL_SCRIPT%" echo     chmod +x /usr/local/bin/wg-*.sh /usr/local/bin/wg-policy-ctl
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Reloading systemd daemon..."
>> "%INSTALL_SCRIPT%" echo     systemctl daemon-reload
>> "%INSTALL_SCRIPT%" echo.    
>> "%INSTALL_SCRIPT%" echo     echo "Enabling and starting services..."
>> "%INSTALL_SCRIPT%" echo     systemctl enable --now wg-policy.service
>> "%INSTALL_SCRIPT%" echo     systemctl enable --now wg-policy-health.timer
>> "%INSTALL_SCRIPT%" echo.    
>> "%INSTALL_SCRIPT%" echo     echo "Installation complete!"
>> "%INSTALL_SCRIPT%" echo     echo "You can check status with: wg-policy-ctl status"
>> "%INSTALL_SCRIPT%" echo }
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo uninstall_policy^(^) {
>> "%INSTALL_SCRIPT%" echo     echo "Uninstalling WireGuard Policy Firewall..."
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Stopping and disabling services..."
>> "%INSTALL_SCRIPT%" echo     systemctl disable --now wg-policy.service wg-policy-health.timer wg-policy-health.service 2^>/dev/null ^|^| true
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Running cleanup script..."
>> "%INSTALL_SCRIPT%" echo     if [ -x /usr/local/bin/wg-policy-cleanup.sh ]; then
>> "%INSTALL_SCRIPT%" echo         /usr/local/bin/wg-policy-cleanup.sh ^|^| true
>> "%INSTALL_SCRIPT%" echo     fi
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Removing systemd units..."
>> "%INSTALL_SCRIPT%" echo     rm -f /etc/systemd/system/wg-policy.service
>> "%INSTALL_SCRIPT%" echo     rm -f /etc/systemd/system/wg-policy-health.service
>> "%INSTALL_SCRIPT%" echo     rm -f /etc/systemd/system/wg-policy-health.timer
>> "%INSTALL_SCRIPT%" echo     systemctl daemon-reload
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Removing scripts from /usr/local/bin/..."
>> "%INSTALL_SCRIPT%" echo     rm -f /usr/local/bin/wg-policy-lib.sh
>> "%INSTALL_SCRIPT%" echo     rm -f /usr/local/bin/wg-sync-policy.sh
>> "%INSTALL_SCRIPT%" echo     rm -f /usr/local/bin/wg-policy-engine.sh
>> "%INSTALL_SCRIPT%" echo     rm -f /usr/local/bin/wg-policy-cleanup.sh
>> "%INSTALL_SCRIPT%" echo     rm -f /usr/local/bin/wg-sync-watch.sh
>> "%INSTALL_SCRIPT%" echo     rm -f /usr/local/bin/wg-policy-ctl
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo     echo "Uninstallation complete!"
>> "%INSTALL_SCRIPT%" echo }
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo case "${1:-}" in
>> "%INSTALL_SCRIPT%" echo     install^)
>> "%INSTALL_SCRIPT%" echo         install_policy
>> "%INSTALL_SCRIPT%" echo         ;;
>> "%INSTALL_SCRIPT%" echo     uninstall^)
>> "%INSTALL_SCRIPT%" echo         uninstall_policy
>> "%INSTALL_SCRIPT%" echo         ;;
>> "%INSTALL_SCRIPT%" echo     *^)
>> "%INSTALL_SCRIPT%" echo         echo "Usage: $0 {install|uninstall}"
>> "%INSTALL_SCRIPT%" echo         exit 1
>> "%INSTALL_SCRIPT%" echo         ;;
>> "%INSTALL_SCRIPT%" echo esac

echo Done! Generated %INSTALL_SCRIPT% successfully.
goto :eof

:AppendFile
set SRC=%1
set TARGET=%2
set EOF_MARKER=%3

>> "%INSTALL_SCRIPT%" echo     cat ^<^< '%EOF_MARKER%' ^> %TARGET%
type "%SRC%" >> "%INSTALL_SCRIPT%"
>> "%INSTALL_SCRIPT%" echo.
>> "%INSTALL_SCRIPT%" echo %EOF_MARKER%
>> "%INSTALL_SCRIPT%" echo.
goto :eof
