#!/bin/bash
# apt-ostree Daemon Installation Script
# This script installs all daemon components for apt-ostree

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if running as root
if [[ $EUID -ne 0 ]]; then
   print_error "This script must be run as root"
   exit 1
fi

print_status "Installing apt-ostree daemon components..."

# Create necessary directories
print_status "Creating directories..."
mkdir -p /usr/libexec
mkdir -p /etc/apt-ostree
mkdir -p /var/lib/apt-ostree
mkdir -p /var/cache/apt-ostree
mkdir -p /var/log/apt-ostree
mkdir -p /etc/dbus-1/system.d
mkdir -p /usr/share/dbus-1/system-services
mkdir -p /usr/share/polkit-1/actions

# Copy daemon binary
print_status "Installing daemon binary..."
if [[ -f "target/release/apt-ostreed" ]]; then
    cp target/release/apt-ostreed /usr/libexec/
    chmod +x /usr/libexec/apt-ostreed
else
    print_warning "Daemon binary not found, skipping..."
fi

# Copy configuration files
print_status "Installing configuration files..."
cp src/daemon/apt-ostreed.conf /etc/apt-ostree/
chmod 644 /etc/apt-ostree/apt-ostreed.conf

# Copy D-Bus configuration
print_status "Installing D-Bus configuration..."
cp src/daemon/org.aptostree.dev.conf /etc/dbus-1/system.d/
cp src/daemon/org.aptostree.dev.service /usr/share/dbus-1/system-services/
chmod 644 /etc/dbus-1/system.d/org.aptostree.dev.conf
chmod 644 /usr/share/dbus-1/system-services/org.aptostree.dev.service

# Copy Polkit policy
print_status "Installing Polkit policy..."
cp src/daemon/org.aptostree.dev.policy /usr/share/polkit-1/actions/
chmod 644 /usr/share/polkit-1/actions/org.aptostree.dev.policy

# Copy systemd services
print_status "Installing systemd services..."
cp src/daemon/apt-ostreed.service /etc/systemd/system/
cp src/daemon/apt-ostree-bootstatus.service /etc/systemd/system/
cp src/daemon/apt-ostree-countme.service /etc/systemd/system/
cp src/daemon/apt-ostree-countme.timer /etc/systemd/system/
cp src/daemon/apt-ostreed-automatic.service /etc/systemd/system/
cp src/daemon/apt-ostreed-automatic.timer /etc/systemd/system/

chmod 644 /etc/systemd/system/apt-ostreed.service
chmod 644 /etc/systemd/system/apt-ostree-bootstatus.service
chmod 644 /etc/systemd/system/apt-ostree-countme.service
chmod 644 /etc/systemd/system/apt-ostree-countme.timer
chmod 644 /etc/systemd/system/apt-ostreed-automatic.service
chmod 644 /etc/systemd/system/apt-ostreed-automatic.timer

# Set proper ownership
print_status "Setting ownership..."
chown -R root:root /var/lib/apt-ostree
chown -R root:root /var/cache/apt-ostree
chown -R root:root /var/log/apt-ostree

# Initialize OSTree repository if it doesn't exist
if [[ ! -d "/var/lib/apt-ostree/repo/objects" ]]; then
    print_status "Initializing OSTree repository..."
    /usr/bin/ostree init --repo=/var/lib/apt-ostree/repo
fi

# Reload systemd and D-Bus
print_status "Reloading systemd and D-Bus..."
systemctl daemon-reload
systemctl reload dbus

# Enable services
print_status "Enabling services..."
systemctl enable apt-ostreed.service
systemctl enable apt-ostree-bootstatus.service
systemctl enable apt-ostree-countme.timer
systemctl enable apt-ostreed-automatic.timer

# Start the daemon
print_status "Starting apt-ostreed daemon..."
systemctl start apt-ostreed.service

# Verify installation
print_status "Verifying installation..."
if systemctl is-active --quiet apt-ostreed.service; then
    print_status "apt-ostreed daemon is running"
else
    print_error "apt-ostreed daemon failed to start"
    systemctl status apt-ostreed.service
    exit 1
fi

# Test D-Bus connection
print_status "Testing D-Bus connection..."
if gdbus introspect --system --dest org.aptostree.dev --object-path /org/aptostree/dev > /dev/null 2>&1; then
    print_status "D-Bus connection successful"
else
    print_warning "D-Bus connection test failed"
fi

print_status "apt-ostree daemon installation completed successfully!"
print_status "You can now use 'apt-ostree' commands with daemon support"
print_status "Use 'systemctl status apt-ostreed.service' to check daemon status" 