#!/bin/bash

set -e

case "$1" in
    configure)
        echo "Configuring wild-cloud-central..."

        # Create wildcloud user if it doesn't exist
        if ! id wildcloud >/dev/null 2>&1; then
            useradd --system --home-dir /var/lib/wild-central --create-home --shell /bin/false wildcloud
        fi

        # Detect architecture
        ARCH=$(dpkg --print-architecture)
        if [ "$ARCH" = "amd64" ]; then
            TALOS_ARCH="amd64"
            KUBECTL_ARCH="amd64"
        elif [ "$ARCH" = "arm64" ]; then
            TALOS_ARCH="arm64"
            KUBECTL_ARCH="arm64"
        else
            echo "Warning: Unsupported architecture $ARCH, skipping tool installations"
            TALOS_ARCH=""
            KUBECTL_ARCH=""
        fi

        # Install yq if not present
        if ! command -v yq >/dev/null 2>&1 || ! yq --version | grep -q "mikefarah"; then
            echo "Installing yq (mikefarah/yq)..."
            YQ_VERSION="v4.50.1"
            if [ -n "$ARCH" ]; then
                wget -q "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_${ARCH}" -O /usr/local/bin/yq
                chmod +x /usr/local/bin/yq
                echo "Installed yq $(yq --version)"
            fi
        else
            echo "yq already installed: $(yq --version)"
        fi

        # Install talosctl if not present
        if ! command -v talosctl >/dev/null 2>&1; then
            echo "Installing talosctl..."
            TALOS_VERSION="v1.11.5"
            if [ -n "$TALOS_ARCH" ]; then
                wget -q "https://github.com/siderolabs/talos/releases/download/${TALOS_VERSION}/talosctl-linux-${TALOS_ARCH}" -O /usr/local/bin/talosctl
                chmod +x /usr/local/bin/talosctl
                echo "Installed talosctl $(talosctl version --client --short)"
            fi
        else
            echo "talosctl already installed: $(talosctl version --client --short 2>/dev/null || talosctl version --short)"
        fi

        # Install kubectl if not present
        if ! command -v kubectl >/dev/null 2>&1; then
            echo "Installing kubectl..."
            KUBECTL_VERSION="v1.34.1"
            if [ -n "$KUBECTL_ARCH" ]; then
                wget -q "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/${KUBECTL_ARCH}/kubectl" -O /usr/local/bin/kubectl
                chmod +x /usr/local/bin/kubectl
                echo "Installed kubectl $(kubectl version --client --short 2>/dev/null || kubectl version --client)"
            fi
        else
            echo "kubectl already installed: $(kubectl version --client --short 2>/dev/null || kubectl version --client)"
        fi

        # Create required directories
        mkdir -p /var/lib/wild-central
        mkdir -p /var/lib/wild-central/instances
        mkdir -p /var/log/wild-central
        mkdir -p /var/www/html/talos
        mkdir -p /var/ftpd

        # Set ownership of wildcloud-managed files and directories
        chown -R wildcloud:wildcloud /var/lib/wild-central
        chown wildcloud:wildcloud /var/log/wild-central
        
        # Force ownership of talos directory (critical for PXE assets)
        echo "Setting ownership of /var/www/html/talos/ to wildcloud"
        chown -R wildcloud:wildcloud /var/www/html/talos/
        
        echo "Setting ownership of /var/ftpd/ to wildcloud"
        chown -R wildcloud:wildcloud /var/ftpd

        # Set up dnsmasq directories and permissions
        mkdir -p /etc/dnsmasq.d
        mkdir -p /etc/dnsmasq.d/wild-cloud-instances

        # Give wildcloud group write access to dnsmasq.d directory for creating temp files
        chgrp wildcloud /etc/dnsmasq.d
        chmod 775 /etc/dnsmasq.d

        # Create or fix ownership of wild-cloud.conf
        if [ ! -f /etc/dnsmasq.d/wild-cloud.conf ]; then
            # Create new file
            touch /etc/dnsmasq.d/wild-cloud.conf
            echo "Created /etc/dnsmasq.d/wild-cloud.conf"
        else
            # File exists - fix ownership if needed
            echo "Found existing /etc/dnsmasq.d/wild-cloud.conf - updating ownership"
        fi

        # Always set correct ownership and permissions
        chown wildcloud:wildcloud /etc/dnsmasq.d/wild-cloud.conf
        chmod 644 /etc/dnsmasq.d/wild-cloud.conf

        # Set ownership and permissions for instance configs directory
        chown wildcloud:wildcloud /etc/dnsmasq.d/wild-cloud-instances
        chmod 755 /etc/dnsmasq.d/wild-cloud-instances
        echo "Created /etc/dnsmasq.d/wild-cloud-instances/ for per-instance DNS configs"

        # Set up systemd-resolved configuration directory and file
        mkdir -p /etc/systemd/resolved.conf.d

        # Create wild-cloud DNS configuration file owned by wildcloud
        if [ ! -f /etc/systemd/resolved.conf.d/wild-cloud.conf ]; then
            touch /etc/systemd/resolved.conf.d/wild-cloud.conf
            echo "Created /etc/systemd/resolved.conf.d/wild-cloud.conf"
        else
            echo "Found existing /etc/systemd/resolved.conf.d/wild-cloud.conf"
        fi
        chown wildcloud:wildcloud /etc/systemd/resolved.conf.d/wild-cloud.conf
        chmod 644 /etc/systemd/resolved.conf.d/wild-cloud.conf

        # Ensure /etc/resolv.conf is a symlink to systemd-resolved stub (best practice)
        # Skip in Docker/container environments where resolv.conf might be bind-mounted
        if [ ! -L /etc/resolv.conf ] || [ "$(readlink /etc/resolv.conf)" != "/run/systemd/resolve/stub-resolv.conf" ]; then
            echo "Configuring /etc/resolv.conf to use systemd-resolved stub mode"
            # Backup existing resolv.conf if it's a regular file
            if [ -f /etc/resolv.conf ] && [ ! -L /etc/resolv.conf ]; then
                cp /etc/resolv.conf /etc/resolv.conf.wild-backup 2>/dev/null || echo "Note: Could not backup /etc/resolv.conf (may be read-only)"
            fi
            # Create symlink to systemd-resolved stub (skip if file is busy/read-only)
            if rm -f /etc/resolv.conf 2>/dev/null; then
                ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
                echo "Set /etc/resolv.conf to use systemd-resolved stub mode"
            else
                echo "Note: /etc/resolv.conf is read-only or busy (container environment?), skipping symlink setup"
            fi
        fi

        # Install polkit rules for service management
        mkdir -p /etc/polkit-1/rules.d
        cat > /etc/polkit-1/rules.d/50-wild-cloud-central.rules << 'POLKIT_EOF'
/* Allow wildcloud user to manage dnsmasq and systemd-resolved services */
polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.systemd1.manage-units" &&
        (action.lookup("unit") == "dnsmasq.service" ||
         action.lookup("unit") == "systemd-resolved.service") &&
        subject.user == "wildcloud") {
        return polkit.Result.YES;
    }
});
POLKIT_EOF
        chmod 644 /etc/polkit-1/rules.d/50-wild-cloud-central.rules
        echo "Installed polkit rules for service management"

        # Enable nginx site (disable default site, enable wild-central)
        if [ -f /etc/nginx/sites-enabled/default ]; then
            rm -f /etc/nginx/sites-enabled/default
            echo "Disabled default nginx site"
        fi
        if [ ! -L /etc/nginx/sites-enabled/wild-central ]; then
            ln -sf /etc/nginx/sites-available/wild-central /etc/nginx/sites-enabled/wild-central
            echo "Enabled wild-central nginx site"
        fi

        # Reload nginx to pick up the new site
        if systemctl is-active --quiet nginx; then
            nginx -t && systemctl reload nginx
            echo "Reloaded nginx configuration"
        fi

        # Enable and start the service (skip in container environments)
        if [ -d /run/systemd/system ]; then
            systemctl daemon-reload
            systemctl enable wild-cloud-central.service
            echo "wild-cloud-central configured successfully"
            echo "Start the service with: sudo systemctl start wild-cloud-central"
            echo "View logs with: sudo journalctl -u wild-cloud-central -f"
        else
            echo "Note: systemd not detected (container environment?), skipping service enable"
            echo "wild-cloud-central installed successfully"
        fi
        ;;

    abort-upgrade|abort-remove|abort-deconfigure)
        ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac

exit 0