#!/bin/bash
# Launcher config editor (github.com/swissfel/launcher_editor), run from its
# checkout in /sf/bernina/applications in the Bernina Python env.
#
#   launcher-editor           asks whether to edit the Bernina launcher
#   launcher-editor --live    view the deployed Bernina launcher (read-only)
#   launcher-editor ARGS...   plain editor, ARGS passed on (see --help)
#
# Editing the Bernina launcher works on your own clone in ~/launcher_config.
# Preview (F5) shows it in the real launcher; Publish commits and pushes it,
# and the push deploys it to /sf/bernina/config/launcher.
#
# On a shared account (gac-*) it first asks for your own user name and runs
# the editor as you via `become` (password in a terminal), so changes are
# published under your name. --live and --help work as the shared account.
# Update the app: git -C /sf/bernina/applications/launcher_editor pull

APP=/sf/bernina/applications/launcher_editor
LIVE=/sf/bernina/config/launcher
REPO=git@gitea.psi.ch:autodeploy_config/sf_bernina_launcher.git
WORK="$HOME/launcher_config"
ROOT=S_Bernina.json
# same as the consoles' launcher (/usr/bin/epics_launcher_wrapper)
PREVIEW="epics_launcher --position 80 -120"
NOTE="Publishing pushes to gitea, which deploys to $LIVE within about a minute. A launcher that is already open shows the change after it is restarted."

if [[ $USER == gac-* && $1 != --live && $1 != -h && $1 != --help ]]; then
    me=$(zenity --entry --width=460 --title="Launcher editor" --text="You are logged in as $USER, a shared account.

Launcher changes are published under your own name, so the editor runs as you on this console. You will be asked for your password in a terminal.

Your PSI user name:" 2>/dev/null) || exit 0
    me=${me//[[:space:]]/}
    if [ -z "$me" ] || [[ $me == gac-* ]] || ! id -- "$me" >/dev/null 2>&1; then
        zenity --error --no-markup --width=420 --title="Launcher editor" \
            --text="Not a valid personal user name: ${me:-(empty)}" 2>/dev/null
        exit 1
    fi
    inner="/sf/bernina/bin/launcher-editor${*:+ $(printf '%q ' "$@")}"
    if [ -t 0 ] && [ -t 1 ]; then
        exec become "$me" -c "$inner"
    fi
    exec gnome-terminal --title="Launcher editor as $me" --wait -- bash -c '
        echo "Launcher editor: log in as $1 (your password)"
        become "$1" -c "$2" || { echo; read -rp "Login or editor failed. Press Enter to close." _; }' \
        _ "$me" "$inner"
fi

source /sf/bernina/bin/bpy-env.sh >/dev/null

editor() {
    PYTHONPATH="$APP${PYTHONPATH:+:$PYTHONPATH}" exec python -B -m launcher_editor "$@"
}

[ "$1" = "--live" ] && editor --read-only "$LIVE/$ROOT"
[ $# -gt 0 ] && editor "$@"

zenity --question --width=440 --title="Launcher editor" \
    --ok-label="Edit Bernina launcher" --cancel-label="Plain editor" \
    --text="Edit the Bernina launcher?

Your own copy in ~/launcher_config is updated from gitea and opened. Preview (F5) shows your changes in the real launcher; Publish makes them live on all consoles." \
    2>/dev/null || editor

# The gitea SSH key lives on ~/network_drives. In a session without it
# (e.g. after `become`) mount it now, while we have a terminal for kinit.
if [ ! -d "$HOME/network_drives/home" ] && [ -t 0 ]; then
    echo "Mounting ~/network_drives (needed for your gitea SSH key)..."
    setup-network-drives || true
fi

export GIT_TERMINAL_PROMPT=0 GIT_SSH_COMMAND="ssh -o BatchMode=yes"
if [ ! -d "$WORK/.git" ]; then
    if ! out=$(git clone -q "$REPO" "$WORK" 2>&1); then
        zenity --error --no-markup --width=520 --title="Launcher editor" --text="Could not clone $REPO into $WORK:

$out

The SSH key for gitea is on ~/network_drives. If that isn't mounted, run setup-network-drives and try again." 2>/dev/null
        exit 1
    fi
elif ! out=$(git -C "$WORK" pull -q --ff-only 2>&1); then
    zenity --warning --no-markup --width=520 --title="Launcher editor" --text="Could not update ~/launcher_config from gitea (unpublished commits, local changes in the way, or no connection):

$out

Opening your copy as it is. Git > Update from Remote retries later." 2>/dev/null
fi

editor --preview-command "$PREVIEW" --publish-note "$NOTE" "$WORK/$ROOT"
