#!/bin/sh
base_dir=$(pwd)

echo "Updating service scripts..."
/root/.cargo/bin/podman-openrc ./services /etc/init.d/

# Update services (awall policies, scripts)
for service in "./services"/*/; do
  [ -d "$service" ] || continue
  cd "$service" || continue

  # Run install.sh if installing for the first time (if $1 is "install")
  if [ "$1" = "install" ] && [ -f "install.sh" ]; then
    read -n 1 -s -r -p "Press any key to install $service..."
    source ./install.sh
  fi

  # Run update.sh if present
  if [ -f "update.sh" ]; then
    source ./update.sh "$1"
  fi

  # Symlink all caddy configs
  for caddyfile in *.caddy; do
    [ -e "$caddyfile" ] || continue
    CADDY_NAME="${caddyfile%.caddy}"
    cp -f "./$caddyfile" "/var/containers/caddy/$CADDY_NAME"
  done

  cd "$base_dir"
done