#!/bin/bash

# Debug variant of deploy_lt.sh
# Builds with debug symbols, deploys, enables core dumps, and restarts services.
# After a crash, run on the server:
#   coredumpctl list
#   coredumpctl gdb simon.service
#   (gdb) bt full

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_BINARY_DEBUG="$SCRIPT_DIR/didactyl_static_x86_64_debug"
SOURCE_BINARY_FALLBACK="$SCRIPT_DIR/didactyl_static_x86_64"
SOURCE_BINARY=""
REMOTE_HOST="ubuntu@laantungir.net"
REMOTE_TARGETS=(
    "/home/simon/didactyl"
    "/home/didactyl/didactyl"
)
REMOTE_SERVICES=(
    "simon.service"
    "didactyl.service"
)

if ! command -v rsync >/dev/null 2>&1; then
    echo "ERROR: rsync is not installed or not in PATH"
    exit 1
fi

if ! command -v ssh >/dev/null 2>&1; then
    echo "ERROR: ssh is not installed or not in PATH"
    exit 1
fi

echo "=========================================="
echo "  DEBUG BUILD + DEPLOY"
echo "=========================================="
echo ""

echo "Building DEBUG static binary (symbols preserved)..."
"$SCRIPT_DIR/build_static.sh" --debug

if [ -f "$SOURCE_BINARY_DEBUG" ]; then
    SOURCE_BINARY="$SOURCE_BINARY_DEBUG"
elif [ -f "$SOURCE_BINARY_FALLBACK" ]; then
    SOURCE_BINARY="$SOURCE_BINARY_FALLBACK"
else
    echo "ERROR: Build completed but no binary found (expected $SOURCE_BINARY_DEBUG or $SOURCE_BINARY_FALLBACK)"
    exit 1
fi

echo ""
echo "Binary info:"
file "$SOURCE_BINARY"
ls -lh "$SOURCE_BINARY"
echo ""

REMOTE_STAGE="/tmp/didactyl_static_x86_64"

echo "Uploading debug binary to $REMOTE_HOST:$REMOTE_STAGE"
rsync -avz --progress "$SOURCE_BINARY" "$REMOTE_HOST:$REMOTE_STAGE"

for target in "${REMOTE_TARGETS[@]}"; do
    echo "Installing staged binary to $target via sudo"
    ssh "$REMOTE_HOST" "sudo install -m 0755 '$REMOTE_STAGE' '$target'"
done

echo ""
echo "Enabling core dumps on remote..."
ssh "$REMOTE_HOST" "
    # Ensure systemd stores core dumps via coredumpctl
    sudo mkdir -p /etc/systemd/coredump.conf.d
    echo '[Coredump]
Storage=external
ProcessSizeMax=512M
ExternalSizeMax=512M' | sudo tee /etc/systemd/coredump.conf.d/debug.conf > /dev/null
    sudo systemctl daemon-reload

    # Remove any ulimit restrictions for the services
    for svc in ${REMOTE_SERVICES[*]}; do
        OVERRIDE_DIR=\"/etc/systemd/system/\${svc}.d\"
        sudo mkdir -p \"\$OVERRIDE_DIR\"
        echo '[Service]
LimitCORE=infinity' | sudo tee \"\$OVERRIDE_DIR/coredump.conf\" > /dev/null
    done
    sudo systemctl daemon-reload
"

echo "Restarting services on $REMOTE_HOST: ${REMOTE_SERVICES[*]}"
ssh "$REMOTE_HOST" "sudo systemctl restart ${REMOTE_SERVICES[*]}"

echo "Cleaning up remote staging file $REMOTE_STAGE"
ssh "$REMOTE_HOST" "rm -f '$REMOTE_STAGE'"

echo ""
echo "=========================================="
echo "  DEBUG DEPLOYMENT COMPLETE"
echo "=========================================="
echo ""
echo "After reproducing the crash, run on the server:"
echo "  ssh $REMOTE_HOST"
echo "  sudo coredumpctl list"
echo "  sudo coredumpctl gdb simon.service"
echo "  (gdb) bt full"
echo "  (gdb) info threads"
echo "  (gdb) thread apply all bt"
echo ""
echo "Then paste the backtrace here."
