#!/bin/bash
# Server Setup Script for Ubuntu 22.04
# Run as root: bash server-setup.sh

echo "=== Installing Docker and Docker Compose ==="
apt update
apt install -y ca-certificates curl gnupg lsb-release

# Add Docker's official GPG key
mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set up Docker repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

# Start and enable Docker
systemctl start docker
systemctl enable docker

echo "=== Installing Additional Tools ==="
apt install -y git nginx certbot python3-certbot-nginx ufw htop

echo "=== Setting up Firewall ==="
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow 80
ufw allow 443
ufw --force enable

echo "=== Creating Application User ==="
useradd -m -s /bin/bash stiftung
usermod -aG docker stiftung

echo "=== Creating Application Directory ==="
mkdir -p /opt/stiftung
chown stiftung:stiftung /opt/stiftung

echo "=== Setup Complete! ==="
echo "Next steps:"
echo "1. Switch to stiftung user: su - stiftung"
echo "2. Clone your repository to /opt/stiftung"
echo "3. Configure environment variables"
echo "4. Start the application"
