#!/bin/sh
# postinst for multiportal. Must be idempotent and must never execute PHP
# (a config/loader problem must not wedge dpkg). Services are enabled here but
# NOT started, and NOT restarted on upgrade: mpctl runs migrations, reloads
# php-fpm and starts services, so new code never runs against an un-migrated
# schema. /etc/multiportal/.env is authored by mpctl, never by this package -
# its absence keeps every unit's ConditionPathExists false, so an unconfigured
# box stays inert even across reboots.
set -e

APP_DIR=/usr/share/multiportal
STATE_DIR=/var/lib/multiportal
LOG_DIR=/var/log/multiportal
CONF_DIR=/etc/multiportal
APP_USER=multiportal
APP_GROUP=multiportal

case "$1" in
    configure)
        umask 077

        # --- service account (mpctl's contract: user/group `multiportal`) ----
        if ! getent group "$APP_GROUP" >/dev/null; then
            addgroup --system --quiet "$APP_GROUP"
        fi
        if ! getent passwd "$APP_USER" >/dev/null; then
            adduser --system --quiet --ingroup "$APP_GROUP" \
                --home "$STATE_DIR" --no-create-home \
                --shell /usr/sbin/nologin "$APP_USER"
        fi

        # --- configuration directory (mpctl writes .env into it) ------------
        install -d -o root -g root -m 0755 "$CONF_DIR"

        # --- mutable state (service-owned; backups tightened to 0700) -------
        install -d -o "$APP_USER" -g "$APP_GROUP" -m 0750 "$STATE_DIR"
        for d in runtime runtime/cache assets iso uploads; do
            install -d -o "$APP_USER" -g "$APP_GROUP" -m 0750 "$STATE_DIR/$d"
        done
        install -d -o "$APP_USER" -g "$APP_GROUP" -m 0700 "$STATE_DIR/backup"

        # --- logs ------------------------------------------------------------
        install -d -o "$APP_USER" -g adm -m 0750 "$LOG_DIR"

        # --- web-served dirs: symlink the payload into /var/lib --------------
        # These are stripped from the payload, so the links are created (not
        # shipped) and only when the path is absent or already our symlink.
        for link in assets iso uploads; do
            linkpath="$APP_DIR/backend/web/$link"
            if [ -L "$linkpath" ] || [ ! -e "$linkpath" ]; then
                ln -sfn "$STATE_DIR/$link" "$linkpath"
            fi
        done

        # --- systemd: enable, never start ------------------------------------
        if [ -d /run/systemd/system ]; then
            systemctl daemon-reload || true
        fi
        if command -v deb-systemd-helper >/dev/null 2>&1; then
            for unit in multiportal-websocket.service 'multiportal-queue@1.service' multiportal-cron.timer; do
                deb-systemd-helper unmask "$unit" >/dev/null 2>&1 || true
                deb-systemd-helper enable "$unit" >/dev/null 2>&1 || true
            done
        fi

        # NOTE: php-fpm is deliberately NOT reloaded here. Reloading would put
        # new bytecode behind the web tier before migrations have run; mpctl
        # reloads php8.2-fpm after `yii migrate` succeeds.

        # --- ioncube loader check (warn only, never fail configure) ----------
        if command -v php8.2 >/dev/null 2>&1; then
            if ! php8.2 -m 2>/dev/null | grep -qi ioncube; then
                echo "multiportal: WARNING - ionCube loader not enabled for PHP 8.2; the application will not run until it is installed." >&2
            fi
        fi

        echo "multiportal: installed. mpctl performs the following (or run manually):"
        echo "  1. Author $CONF_DIR/.env (root:$APP_GROUP 0640)"
        echo "  2. Run migrations:  sudo -u $APP_USER php8.2 $APP_DIR/yii migrate --interactive=0"
        echo "  3. Reload PHP:      systemctl try-reload-or-restart php8.2-fpm"
        echo "  4. Start services:  systemctl start multiportal-websocket multiportal-queue@1 multiportal-cron.timer"
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        ;;
esac

exit 0
