#!/bin/bash
# Forced command of the stratum-support user: records the session.
#   session-shell <session id> <end (epoch)>
SID="${1:-session}"
END="${2:-0}"
LOG=/var/log/stratum-support
umask 077
if [[ -n "${SSH_ORIGINAL_COMMAND:-}" ]]; then
    # a command (scp, sftp, ssh host cmd): logged, then run
    printf '%s %s\n' "$(date '+%F %T')" "$SSH_ORIGINAL_COMMAND" >> "$LOG/$SID.commands" 2>/dev/null
    logger -t stratum-support "session $SID command: $SSH_ORIGINAL_COMMAND" 2>/dev/null
    exec /bin/bash -c "$SSH_ORIGINAL_COMMAND"
fi
logger -t stratum-support "session $SID: terminal opened" 2>/dev/null
printf '\n  Stratum remote support - session %s, ends %s\n' "$SID" "$(date -d "@$END" '+%F %H:%M' 2>/dev/null)"
printf '  This terminal is recorded. Root: sudo -i\n\n'
exec script -q -f -a "$LOG/$SID.typescript" -c "/bin/bash -l"
