#!/bin/bash
set -e

# Adastra Storage Installation Script for Ubuntu 24.04
# This script builds and installs the Adastra Storage appliance

INSTALL_DIR="/opt/adastra-storage"
SERVICE_USER="adastra"
SERVICE_GROUP="adastra"
BUILD_DIR="$(pwd)"
PACKAGE_DIR="$BUILD_DIR/packaging"

echo "=========================================="
echo "Adastra Storage Installation Script"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "Please run as root (use sudo)"
    exit 1
fi

# Check Ubuntu version
if [ ! -f /etc/os-release ]; then
    echo "Error: Cannot determine OS version"
    exit 1
fi

. /etc/os-release
if [ "$ID" != "ubuntu" ] || [ "$VERSION_ID" != "24.04" ]; then
    echo "Warning: This installer is designed for Ubuntu 24.04"
    echo "Detected: $ID $VERSION_ID"
    read -p "Continue anyway? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

echo "Step 1: Installing system dependencies..."
apt-get update
apt-get install -y \
    golang-go \
    zfsutils-linux \
    smartmontools \
    nfs-kernel-server \
    samba \
    targetcli-fb \
    build-essential \
    git \
    curl \
    wget

# Install MinIO (if not already installed)
if ! command -v minio &> /dev/null; then
    echo "Installing MinIO..."
    wget -q https://dl.min.io/server/minio/release/linux-amd64/minio -O /usr/local/bin/minio
    chmod +x /usr/local/bin/minio
fi

echo ""
echo "Step 2: Building Adastra Storage application..."

# Build the application
cd "$BUILD_DIR"
if [ ! -f go.mod ]; then
    echo "Error: go.mod not found. Are you in the project root?"
    exit 1
fi

# Build binary
echo "Building binary..."
go build -o "$BUILD_DIR/appliance" ./cmd/appliance

if [ ! -f "$BUILD_DIR/appliance" ]; then
    echo "Error: Build failed"
    exit 1
fi

echo ""
echo "Step 3: Creating installation directory structure..."

# Create directories
mkdir -p "$INSTALL_DIR/bin"
mkdir -p "$INSTALL_DIR/data"
mkdir -p "$INSTALL_DIR/templates"
mkdir -p "$INSTALL_DIR/migrations"
mkdir -p "$INSTALL_DIR/logs"
mkdir -p /etc/systemd/system

# Create service user if it doesn't exist
if ! id "$SERVICE_USER" &>/dev/null; then
    echo "Creating service user: $SERVICE_USER"
    useradd -r -s /bin/false -d "$INSTALL_DIR" "$SERVICE_USER"
fi

echo ""
echo "Step 4: Installing application files..."

# Copy binary
cp "$BUILD_DIR/appliance" "$INSTALL_DIR/bin/adastra-storage"
chmod +x "$INSTALL_DIR/bin/adastra-storage"

# Copy templates
cp -r "$BUILD_DIR/internal/templates"/* "$INSTALL_DIR/templates/"

# Copy migrations
cp -r "$BUILD_DIR/migrations"/* "$INSTALL_DIR/migrations/"

# Copy uninstaller
cp "$PACKAGE_DIR/uninstall.sh" "$INSTALL_DIR/uninstall.sh"
chmod +x "$INSTALL_DIR/uninstall.sh"

# Set ownership
chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"

echo ""
echo "Step 5: Installing systemd service..."

# Install systemd service
cp "$PACKAGE_DIR/adastra-storage.service" /etc/systemd/system/
systemctl daemon-reload

# Add service user to necessary groups
usermod -aG disk "$SERVICE_USER" || true

echo ""
echo "Step 6: Configuring service..."

# Create environment file (if needed)
if [ ! -f "$INSTALL_DIR/.env" ]; then
    cat > "$INSTALL_DIR/.env" <<EOF
# Adastra Storage Configuration
INSTALL_DIR=$INSTALL_DIR
DATA_DIR=$INSTALL_DIR/data
LOG_DIR=$INSTALL_DIR/logs
PORT=8080
EOF
    chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR/.env"
    chmod 600 "$INSTALL_DIR/.env"
fi

echo ""
echo "=========================================="
echo "Installation Complete!"
echo "=========================================="
echo ""
echo "Installation directory: $INSTALL_DIR"
echo "Data directory: $INSTALL_DIR/data"
echo "Service user: $SERVICE_USER"
echo ""
echo "To start the service:"
echo "  systemctl start adastra-storage"
echo ""
echo "To enable on boot:"
echo "  systemctl enable adastra-storage"
echo ""
echo "To check status:"
echo "  systemctl status adastra-storage"
echo ""
echo "To view logs:"
echo "  journalctl -u adastra-storage -f"
echo ""
echo "Default admin credentials:"
echo "  Username: admin"
echo "  Password: admin"
echo ""
echo "⚠️  IMPORTANT: Change the default password after first login!"
echo ""
echo "Access the web interface at: http://localhost:8080"
echo ""

