#!/bin/sh
# Run the server-apps first-boot deploys and report progress to Plymouth.
# Invoked from /etc/firsttime.d by run-parts.

DIR=/etc/firsttime.d/server-apps.d
# Seconds per 1% while smoothly advancing the bar during one deploy.
CREEP_DELAY=2

[ -d "$DIR" ] || exit 0

mkdir -p /root/.install-log
exec >>/root/.install-log/office-server-deploy.log 2>&1

# Honor the system locale so messages get translated; English otherwise.
for f in /etc/locale.conf /etc/sysconfig/i18n; do
    # shellcheck source=/dev/null
    [ -r "$f" ] && . "$f"
done
[ -n "$LANG" ] && export LANG

# Translate an English msgid; fall back to the msgid itself if gettext or
# the message catalog is missing (gettext returns the msgid in that case).
gt() {
    if gettext -V >/dev/null 2>&1; then
        gettext -d installed-db-office-server -- "$1"
    else
        printf '%s' "$1"
    fi
}

# Overall heading and the "don't power off" warning. Kept here (gettext-
# translated) instead of in the Plymouth theme, where two-step would render
# them as static, untranslatable strings.
HEADER=$(gt "Setting up server applications")
WARN=$(gt "Do not turn off the computer")

plymouth_alive() {
    plymouth --ping 2>/dev/null
}

# Show the boot-splash caption: heading, current step + percent, warning.
# All text is in the single Plymouth message slot (multiline via the label
# plugin), so we compose the three lines ourselves.
show() {
    # $1 = step label, $2 = percent
    plymouth display-message --text="$HEADER
$1 - $2%
$WARN" 2>/dev/null
}

# Smoothly raise the bar from $1 to $2-1 and update the caption "$3 - NN%"
# until killed. One deploy runs for tens of seconds, so without this the bar
# would sit still and look frozen. We drive the caption ourselves (the
# theme's percent label is off) because that label and display-message share
# the same Plymouth message slot and would overwrite each other.
creep() {
    cur=$1
    while [ "$cur" -lt "$2" ]; do
        plymouth system-update --progress="$cur" 2>/dev/null
        show "$3" "$cur"
        cur=$((cur + 1))
        sleep "$CREEP_DELAY"
    done
}

# Collect executable steps in glob order (the same order run-parts uses).
scripts=
for f in "$DIR"/*; do
    [ -f "$f" ] && [ -x "$f" ] && scripts="$scripts $f"
done

total=0
for f in $scripts; do
    total=$((total + 1))
done

[ "$total" -gt 0 ] || exit 0

plymouth_alive && plymouth change-mode --updates

template=$(gt "Setting up %s (%d of %d)")
done_count=0
status=0
for script in $scripts; do
    name=$(basename "$script" | sed -e 's/^[0-9]*-//' -e 's/^office-server-//')
    # shellcheck disable=SC2059
    label=$(printf "$template" "$name" "$((done_count + 1))" "$total")
    base=$((done_count * 100 / total))
    next=$(((done_count + 1) * 100 / total))
    creeper_pid=
    if plymouth_alive; then
        show "$label" "$base"
        creep "$base" "$next" "$label" &
        creeper_pid=$!
    fi
    "$script" || status=1
    [ -n "$creeper_pid" ] && kill "$creeper_pid" 2>/dev/null
    done_count=$((done_count + 1))
    if plymouth_alive; then
        plymouth system-update --progress="$next"
        show "$label" "$next"
    fi
done

if plymouth_alive; then
    plymouth system-update --progress=100
    if [ "$status" -eq 0 ]; then
        plymouth display-message --text="$(gt "Server applications configured")"
    else
        plymouth display-message --text="$(gt "Server applications configuration failed")"
    fi
fi
exit "$status"
