#!/usr/bin/env bash
set -euo pipefail
#set -x

# Detect Ubuntu codename (jammy, noble, etc)
CODENAME=$(lsb_release -sc)
CODENAME="jammy"

echo "Detected Ubuntu codename: $CODENAME"

# Map codenames to wkhtmltopdf packages
case "$CODENAME" in
    jammy)
        WK_DEB="wkhtmltox_0.12.6.1-2.jammy_amd64.deb"
        ;;
    noble)
        WK_DEB="wkhtmltox_0.12.6.1-1.noble_amd64.deb"
        ;;
    *)
        echo "ERROR: This script does not support Ubuntu codename '$CODENAME'"
        echo "Supported: jammy (22.04), noble (24.04)"
        exit 1
        ;;
esac

# Download wkhtmltopdf package if needed
if [[ ! -f "$WK_DEB" ]]; then
    echo "Downloading wkhtmltopdf for Ubuntu $CODENAME..."
    # Try primary URL, if that fails try fallback, otherwise fail with message
    if ! wget -O "$WK_DEB" "https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/${WK_DEB}"; then
        echo "Primary download failed, trying fallback..."
        if ! wget -O "$WK_DEB" "https://github.com/wkhtmltopdf/packaging/releases/latest/download/${WK_DEB}"; then
            echo "ERROR: failed to download $WK_DEB" >&2
            rm -f "$WK_DEB"
            exit 1
        fi
    fi
fi

if [[ ! -s "$WK_DEB" ]]; then
    echo "ERROR: downloaded $WK_DEB is empty" >&2
    rm -f "$WK_DEB"
    exit 1
fi
echo "Installing $WK_DEB..."
sudo apt install -y "./$WK_DEB"

# Install PHP from PPA (works on 22.04 and 24.04)
if ! grep -q "ondrej/php" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
    echo "Adding PHP PPA..."
    sudo apt update -y
    sudo add-apt-repository -y ppa:ondrej/php
fi

echo "Updating apt package lists..."
sudo apt update -y

echo "Installing PHP 8.3 and extensions..."
sudo apt install -y \
    php8.3 \
    php8.3-cli \
    php8.3-common \
    php8.3-fpm \
    php8.3-mysql \
    php8.3-zip \
    php8.3-gd \
    php8.3-mbstring \
    php8.3-curl \
    php8.3-xml \
    php8.3-bcmath

# Fix up php.ini
PHPINI="/etc/php/8.3/cli/php.ini"

# -----------------------------
# Determine TIMEZONE value
# -----------------------------
if [[ -f /etc/timezone ]]; then
    TZVAL="$(cat /etc/timezone)"
else
    TZVAL="America/Chicago"
fi

# Escape for sed (slashes and &)
TZ_ESCAPED=$(printf '%s' "$TZVAL" | sed 's/[\/&]/\\&/g')

# -----------------------------
# Update include_path
# Only if it matches the default:
#     ;include_path = ".:/usr/share/php"
# -----------------------------
sudo sed -i \
  -e 's#^\s*;\?\s*include_path\s*=\s*"\.:/usr/share/php"#include_path = "/php:/php/classes"#' \
  "$PHPINI"

# -----------------------------
# Update date.timezone
# Matches either:
#   ;date.timezone =
#   date.timezone =
#   date.timezone = "something"
# -----------------------------
sudo sed -i \
  -e "s#^\s*;\?\s*date.timezone\s*=.*#date.timezone = \"$TZ_ESCAPED\"#" \
  "$PHPINI"

# -----------------------------
# Update memory_limit
# Only if it matches the default:
#     memory_limit = -1
# -----------------------------
sudo sed -i \
  -e 's#^memory_limit\s*=\s*-1#memory_limit = 512M#' \
  "$PHPINI"

echo "Done!"
