#!/bin/bash



sudo useradd --system --shell /usr/sbin/nologin --home /var/lib/caddy caddy
sudo mkdir -p /etc/caddy /var/lib/caddy
sudo chown -R caddy:caddy /etc/caddy /var/lib/caddy
sudo chmod 755 /etc/caddy /var/lib/caddy


# Define colors for echo
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
NC="\033[0m" # No color

# Log files
ERROR_LOG="error.log"
INSTALL_LOG="install.log"

# Function to log errors
log_error() {
  local message="$1"
  echo -e "${RED}[ERROR] ${message}${NC}"
  echo "$(date '+%Y-%m-%d %H:%M:%S') ${message}" >> "$ERROR_LOG"
}

# Function to log status
log_status() {
  local message="$1"
  echo -e "${GREEN}[STATUS] ${message}${NC}"
}

# Start the installation
log_status "Starting Caddy installation..."

# Update system packages
log_status "Updating system packages..."
if ! sudo apt update; then
  log_error "Failed to update package lists."
  exit 1
fi

# Install dependencies
log_status "Installing required dependencies..."
if ! sudo apt install -y wget tar; then
  log_error "Failed to install required dependencies."
  exit 1
fi

# Download Caddy binary
CADDY_URL="https://github.com/caddyserver/caddy/releases/download/v2.8.4/caddy_2.8.4_linux_amd64.tar.gz"
log_status "Downloading Caddy binary..."
if ! wget -O caddy.tar.gz "$CADDY_URL"; then
  log_error "Failed to download Caddy binary."
  exit 1
fi

# Extract the tarball
log_status "Extracting Caddy tarball..."
if ! tar -xzf caddy.tar.gz; then
  log_error "Failed to extract Caddy tarball."
  exit 1
fi

# Install Caddy
log_status "Installing Caddy..."
if ! sudo install -m 755 caddy /usr/local/bin/; then
  log_error "Failed to install Caddy."
  exit 1
fi

# Verify installation
log_status "Verifying Caddy installation..."
if ! caddy version &>/dev/null; then
  log_error "Caddy installation failed."
  exit 1
fi

# sudo mkdir -p /etc/caddy && echo "# This is an empty Caddyfile. Add your site configurations here." | sudo tee /etc/caddy/Caddyfile > /dev/null && sudo chmod 644 /etc/caddy/Caddyfile


# Set up systemd service for Caddy
log_status "Setting up Caddy as a systemd service..."
CADDY_SERVICE="/etc/systemd/system/caddy.service"
if ! sudo tee "$CADDY_SERVICE" > /dev/null <<EOL
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/local/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/local/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512

[Install]
WantedBy=multi-user.target
EOL
then
  log_error "Failed to create Caddy systemd service file."
  exit 1
fi

# Reload systemd, enable, and start the Caddy service
log_status "Enabling and starting Caddy service..."
if ! sudo systemctl daemon-reload; then
  log_error "Failed to reload systemd daemon."
  exit 1
fi

if ! sudo systemctl enable caddy; then
  log_error "Failed to enable Caddy service."
  exit 1
fi

if ! sudo systemctl start caddy; then
  log_error "Failed to start Caddy service."
  exit 1
fi

log_status "Caddy service is running!"
log_status "Cleaning up temporary files..."
rm -f caddy.tar.gz caddy

service caddy status

# Finish installation
log_status "Caddy v2.8.4 installed and running as a service successfully!"
echo "$(date '+%Y-%m-%d %H:%M:%S') Caddy OK" >> "$INSTALL_LOG"
