#!/bin/bash

# Script to install Caddy web server

# Check if the script is run with superuser privileges
if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root (using sudo)."
  exit 1
fi

echo "Installing Caddy web server..."

# Add the Caddy apt repository
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

rm /etc/caddy/Caddyfile
cp Caddyfile /etc/caddy

if [ $? -eq 0 ]; then
  echo "Caddy web server installed successfully!"
  echo ""
  echo "You can now configure Caddy by editing its configuration file:"
  echo "/etc/caddy/Caddyfile"
  echo ""
  echo "To start and enable Caddy to run on boot:"
  echo "sudo systemctl start caddy"
  echo "sudo systemctl enable caddy"
else
  echo "Error installing Caddy web server. Please check the output above for details."
  exit 1
fi

exit 0
