#!/usr/bin/sh
#
# Prepares the directories of the installed layout (bootstrap/layout.php: configuration, state, caches and logs
# apart from the code) with their ownership and modes, the runtime directory of the Octane socket, and the .env
# from the template of the package when there is none. Run as root by the packages after they install the code;
# safe to run again. Prints the path of the .env.
#
# Usage: suricatavel-setup

set -eu

SERVICE_USER=suricatavel

script=$(readlink -f "$0" 2>/dev/null || echo "$0")
CODE=$(cd "$(dirname "$script")/../.." && pwd -P)

if [ "$(id -u)" -ne 0 ]; then
    echo 'Error: suricatavel-setup runs as root.' >&2
    exit 1
fi

if [ ! -f "$CODE/bootstrap/layout.php" ]; then
    echo "Error: $CODE/bootstrap/layout.php is missing: this is not a packaged installation." >&2
    exit 1
fi

layout() {
    php -r '$layout = require $argv[1]; echo $layout[$argv[2]];' "$CODE/bootstrap/layout.php" "$1"
}

CONFIG=$(layout config)
STATE=$(layout state)
CACHE=$(layout cache)
LOGS=$(layout logs)

# Configuration: the service user reads it; only its .env is its own (key:generate writes it).
install -d -o root -g "$SERVICE_USER" -m 0750 "$CONFIG"

# State (also the home of the service user, with its SSH and Ansible files), caches and logs: the service
# user's, readable by nobody else.
for directory in "$STATE" "$STATE/app" "$STATE/app/public" "$STATE/ansible" "$STATE/backups" "$STATE/certs" \
    "$STATE/filament_exports" "$STATE/framework" "$STATE/framework/cache" "$STATE/framework/sessions" \
    "$STATE/suricata" "$STATE/temp" "$CACHE" "$CACHE/views" "$LOGS"; do
    install -d -o "$SERVICE_USER" -g "$SERVICE_USER" -m 0750 "$directory"
done
# SSH keys, vault password and CA key passphrase (SecretKeyStore), and the SSH and Ansible files of the user.
for directory in "$STATE/ssh_keys" "$STATE/secrets" "$STATE/.ssh" "$STATE/.ansible"; do
    install -d -o "$SERVICE_USER" -g "$SERVICE_USER" -m 0700 "$directory"
done
chown -R "$SERVICE_USER:$SERVICE_USER" "$STATE" "$CACHE" "$LOGS"
for directory in "$STATE" "$CACHE" "$LOGS"; do
    find "$directory" -type d -perm -2000 -exec chmod g-s {} +
    find "$directory" -type f -exec chmod u+rw,g-wx,o-rwx {} +
done
for directory in "$STATE/ssh_keys" "$STATE/secrets" "$STATE/.ssh"; do
    find "$directory" -type f -exec chmod 0600 {} +
done

# Runtime directory of the Octane socket, recreated on every boot; the web server reaches the socket through the
# suricatavel group.
mkdir -p /etc/tmpfiles.d
printf 'd /run/suricatavel 0775 %s %s -\n' "$SERVICE_USER" "$SERVICE_USER" > /etc/tmpfiles.d/suricatavel.conf
install -d -o "$SERVICE_USER" -g "$SERVICE_USER" -m 0775 /run/suricatavel
for web_user in www-data nginx apache; do
    if id "$web_user" >/dev/null 2>&1; then
        usermod -a -G "$SERVICE_USER" "$web_user"
    fi
done

# Runtime caches (config, routes, events) would freeze the .env, and the service user cannot remove them (the code
# is root's): the application runs without them. The package's own manifests stay.
rm -f "$CODE/bootstrap/cache/config.php" "$CODE/bootstrap/cache/routes-v7.php" "$CODE/bootstrap/cache/events.php"

if [ ! -f "$CONFIG/.env" ] && [ -f "$CODE/.env.prod" ]; then
    cp "$CODE/.env.prod" "$CONFIG/.env"
    echo "INFO: Created $CONFIG/.env from .env.prod: set APP_URL and REVERB_HOST to your real domain" \
        "(or run $CODE/support/scripts/suricatavel_install.sh to configure everything)." >&2
fi
if [ -f "$CONFIG/.env" ]; then
    chown "$SERVICE_USER:$SERVICE_USER" "$CONFIG/.env"
    chmod 0600 "$CONFIG/.env"
fi

echo "$CONFIG/.env"
