#!/bin/sh

# cereal: command line interface to cereal screen serial loggers
#
# The cereal scripts were written by
# Jameson Graef Rollins <jrollins@finestructure.net>
# and
# Daniel Kahn Gillmor <dkg@fifthhorseman.net>.
#
# They are Copyright 2007, and are all released under the GPL, version 3
# or later.

##################################################
SHAREDIR=${SHAREDIR:-"/usr/share/cereal"}
export SHAREDIR
. "$SHAREDIR/common"
##################################################

usage() {
cat <<EOF
Usage: cereal <subcommand> [options] [args]
Cereal client program.

subcommands:
  attach (a) SESSION                      attach to session
  follow (f) [options] SESSION            follow session log
    -c (--cat)                              cat log instead of follow
    -p (--path)                             output just log file path
  list (l) [options] [SESSION]...         list session(s)
    -n (--names)                            list just session names
  help (h,?)                              this help

EOF
}

attach() {
    SESSION="$1"

    if [ -z "$SESSION" ] ; then
	failure "Not enough input arguments.
Type 'cereal help' for more info."
    elif ! is_session "$SESSION" ; then
	failure "Session '$SESSION' not found.
Type 'cereal list' to list sessions."
    elif ! can_attach "$SESSION" "$USER" ; then
	failure "You do not have permission to attach to session '$SESSION'."
    fi

    is_running "$SESSION"
    case $? in
	0|2) # also attempt to start if session is unknown (return code 2)
	    log_write "$SESSION" "user '$USER' attaching to session from `tty`..."
	    screen -x -S "cereal:$SESSION" || failure "Could not reattach screen."
	    log_write "$SESSION" "user '$USER' on `tty` detached from session."
	    ;;
	1)
	    failure "Session '$SESSION' not running."
	    ;;
    esac
}

log_follow() {
    tail -n 100 -F "$LOG"
}

log_cat() {
    cat "$LOG"
}

log_pwd() {
    echo "$LOG"
}

follow() {
    local SESSION
    local TEMP
    local OUTPUT
    OUTPUT="log_follow"

    TEMP=$(getopt -o cp --long cat,path -n "cereal follow" -- "$@")
    if [ $? -ne 0 ] ; then
	failure "Invalid options."
    fi

    # Note the quotes around `$TEMP': they are essential!
    eval set -- "$TEMP"

    while true ; do
        case "$1" in
	    -c|--cat) OUTPUT="log_cat" ; shift 1 ;;
	    -p|--path) OUTPUT="log_pwd" ; shift 1 ;;
	    --) shift ;;
	    *)
		SESSION="$1"
		break
		;;
        esac
    done

    if [ -z "$SESSION" ] ; then
	failure "Not enough input arguments.
Type 'cereal help' for more info."
    elif ! is_session "$SESSION" ; then
	failure "Session '$SESSION' not found.
Type 'cereal list' to list sessions."
    elif ! can_follow "$SESSION" "$USER" ; then
	failure "You do not have permission to follow session '$SESSION'."
    fi

    LOG="$SESSIONDIR/$SESSION/log/main/current"
    if [ "$OUTPUT" = 'log_follow' -o "$OUTPUT" = 'log_cat' ] ; then
	echo "############################################################"
	echo "### cereal log: $SESSION"
	printf "### "
	list "$SESSION"
	echo "### log file: $LOG"
	echo "############################################################"
    fi
    $OUTPUT
}

###############################################################
### MAIN

handle_command() {

    COMMAND="$1"

    [ "$COMMAND" ] || failure "Type 'cereal help' for usage."
    shift

    case $COMMAND in
	'attach'|'a')
	    attach "$@"
	    ;;
	'follow'|'f')
	    follow "$@"
	    ;;
	'list'|'l')
	    list "$@" || failure "There are no sessions." 1
	    ;;
	'help'|'h'|'?')
	    usage
	    ;;
	*)
	    failure "Unknown command: '$COMMAND'
Type '$CMD help' for usage."
	    ;;
    esac

    exit "$ERR"
}

handle_command "$@"
