#!/usr/bin/env bash
# totaljs-conky [system|services|compact|clock|<your profile>|hidden|restore|menu]: shows a profile of the widget on the wallpaper, or hides it. "restore" starts the one that the
# person chose last (the system one the first time), once the session is ready: it is what the session runs.
set -euo pipefail

# the menu of the desktop asks for its entries (an Openbox pipe menu): every profile that the person has, with a mark on the one that is shown
if [ "${1:-}" = menu ]; then
    CURRENT="$(cat "${HOME}/.config/totaljs/conky-profile" 2>/dev/null || echo system)"
    echo '<openbox_pipe_menu>'
    NAMES="system services compact clock"
    for file in "${HOME}"/.config/conky/*.conf; do
        [ -f "${file}" ] || continue
        name="$(basename "${file}" .conf)"
        case "${name}" in *[!a-z0-9_-]*) continue ;; esac
        case " ${NAMES} " in *" ${name} "*) ;; *) NAMES="${NAMES} ${name}" ;; esac
    done
    for name in ${NAMES}; do
        [ -f "${HOME}/.config/conky/${name}.conf" ] || [ -f "/etc/skel/.config/conky/${name}.conf" ] || continue
        label="$(printf '%s' "${name:0:1}" | tr 'a-z' 'A-Z')${name:1}"
        [ "${name}" = "${CURRENT}" ] && label="● ${label}"
        printf '  <item label="%s"><action name="Execute"><command>totaljs-conky %s</command></action></item>\n' "${label}" "${name}"
    done
    echo '  <separator />'
    label=Hide; [ "${CURRENT}" = hidden ] && label="● Hide"
    printf '  <item label="%s"><action name="Execute"><command>totaljs-conky hidden</command></action></item>\n' "${label}"
    echo '</openbox_pipe_menu>'
    exit 0
fi

PROFILE="${1:-system}"
STATE_DIR="${HOME}/.config/totaljs"
STATE="${STATE_DIR}/conky-profile"
mkdir -p "${STATE_DIR}"

if [ "${PROFILE}" = restore ]; then
    PROFILE="$(cat "${STATE}" 2>/dev/null || echo system)"
    # Openbox starts the autostart before the root window is ready: Conky exits at once when it is not
    for _ in $(seq 1 20); do
        pgrep -u "$(id -u)" -x openbox >/dev/null 2>&1 && xset q >/dev/null 2>&1 && break
        sleep 1
    done
fi

if [ "${PROFILE}" = hidden ]; then
    echo hidden > "${STATE}"
    pkill -u "$(id -u)" -x conky 2>/dev/null || true
    exit 0
fi
# a profile is any file ~/.config/conky/<name>.conf: the name has only letters, digits, - and _
case "${PROFILE}" in ""|*[!a-z0-9_-]*) PROFILE=system ;; esac
[ -f "${HOME}/.config/conky/${PROFILE}.conf" ] || [ -f "/etc/skel/.config/conky/${PROFILE}.conf" ] || PROFILE=system
echo "${PROFILE}" > "${STATE}"

CONFIG="${HOME}/.config/conky/${PROFILE}.conf"
[ -f "${CONFIG}" ] || CONFIG="/etc/skel/.config/conky/${PROFILE}.conf"

pkill -u "$(id -u)" -x conky 2>/dev/null || true
nohup conky -c "${CONFIG}" >/dev/null 2>&1 &
