#!/bin/bash
# Set up symlinks for Docker configurations to avoid manual copying

set -e  # Exit on error

echo "==================================================================="
echo "Docker Configuration Symlink Setup"
echo "==================================================================="
echo ""

REPO_ROOT="/home/hoborg/homelab"

# Function to create symlink with backup
create_symlink() {
    local service=$1
    local target=$2
    local link=$3

    echo "-------------------------------------------------------------------"
    echo "Setting up: $service"
    echo "-------------------------------------------------------------------"
    echo "Target: $target"
    echo "Link:   $link"
    echo ""

    # Check if target exists
    if [ ! -f "$target" ]; then
        echo "ERROR: Target file does not exist: $target"
        return 1
    fi

    # Create parent directory if it doesn't exist
    local link_dir=$(dirname "$link")
    if [ ! -d "$link_dir" ]; then
        echo "Creating directory: $link_dir"
        mkdir -p "$link_dir"
    fi

    # Backup existing file if it's not already a symlink
    if [ -f "$link" ] && [ ! -L "$link" ]; then
        backup="${link}.backup.$(date +%Y%m%d_%H%M%S)"
        echo "Backing up existing file to: $backup"
        cp "$link" "$backup"
        rm "$link"
    elif [ -L "$link" ]; then
        echo "Removing existing symlink"
        rm "$link"
    fi

    # Create symlink
    ln -s "$target" "$link"

    if [ -L "$link" ]; then
        echo "✓ Symlink created successfully"
    else
        echo "✗ Failed to create symlink"
        return 1
    fi

    echo ""
}

# Gitea docker-compose.yml
create_symlink "Gitea docker-compose" \
    "$REPO_ROOT/config/docker/gitea/docker-compose.yml" \
    "/opt/docker/gitea/docker-compose.yml"

# Jellyfin docker-compose.yml
create_symlink "Jellyfin docker-compose" \
    "$REPO_ROOT/config/docker/jellyfin/docker-compose.yml" \
    "/opt/docker/jellyfin/docker-compose.yml"

# qBittorrent docker-compose.yml
create_symlink "qBittorrent docker-compose" \
    "$REPO_ROOT/config/docker/qbittorrent/docker-compose.yml" \
    "/opt/docker/qbittorrent/docker-compose.yml"

# Portainer docker-compose.yml
create_symlink "Portainer docker-compose" \
    "$REPO_ROOT/config/docker/portainer/docker-compose.yml" \
    "/opt/docker/portainer/docker-compose.yml"

# Copyparty docker-compose.yml
create_symlink "Copyparty docker-compose" \
    "$REPO_ROOT/config/docker/copyparty/docker-compose.yml" \
    "/opt/docker/copyparty/docker-compose.yml"

# Glances docker-compose.yml
create_symlink "Glances docker-compose" \
    "$REPO_ROOT/config/docker/glances/docker-compose.yml" \
    "/opt/docker/glances/docker-compose.yml"

# Syncthing docker-compose.yml
create_symlink "Syncthing docker-compose" \
    "$REPO_ROOT/config/docker/syncthing/docker-compose.yml" \
    "/opt/docker/syncthing/docker-compose.yml"

# Docker daemon config
create_symlink "Docker daemon config" \
    "$REPO_ROOT/config/docker/daemon.json" \
    "/etc/docker/daemon.json"

# Landing page
create_symlink "Landing page" \
    "$REPO_ROOT/config/www/index.html" \
    "/var/www/homelab/index.html"

echo "==================================================================="
echo "Summary"
echo "==================================================================="
echo ""
echo "✓ All symlinks created successfully!"
echo ""
echo "Notes:"
echo "  - Original files backed up with timestamp"
echo "  - Edit files in $REPO_ROOT/config/docker/"
echo "  - Changes take effect immediately (no copying needed)"
echo "  - For daemon.json: restart docker after changes"
echo "  - For docker-compose: restart containers after changes"
echo ""
echo "Verify symlinks:"
echo "  ls -l /opt/docker/gitea/docker-compose.yml"
echo "  ls -l /opt/docker/jellyfin/docker-compose.yml"
echo "  ls -l /opt/docker/qbittorrent/docker-compose.yml"
echo "  ls -l /opt/docker/portainer/docker-compose.yml"
echo "  ls -l /opt/docker/copyparty/docker-compose.yml"
echo "  ls -l /opt/docker/glances/docker-compose.yml"
echo "  ls -l /opt/docker/syncthing/docker-compose.yml"
echo "  ls -l /etc/docker/daemon.json"
echo ""
