# sign.sh - add sysadmin "signature" to history
#
# NOTE: we need to declare PROMPT_COMMAND as follows:
#
# ~# tail -n 4 .bash_profile 
#
#  export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo `date +"%Y-%m-%d %H:%M:%S"` $SYSADMIN "$(history 1)" >> ~/.real_bash_history'
#  readonly PROMPT_COMMAND
#
# ---
# 2011-09-27 - Ruy Rocha <ruy.rocha@hostgator.com.br>

# sign only root access
if [ "$(id -u)" -eq 0 ]; then

  # Only assign SYSADMIN if not readonly and not set:
  if ! declare -p SYSADMIN 2>/dev/null | grep -q '^declare -r'; then
    if [ -z "${SYSADMIN}" ]; then
      export SYSADMIN="$(printenv SSH_CONNECTION | awk '{ print $1 }')"
    fi
  fi

  # allow multi-line history and always append to file (instead of write on exit)
  shopt -s histappend
  shopt -q -s cmdhist

  readonly HISTFILE
  readonly SYSADMIN

fi
