#!/bin/sh
# (c) Igor Chubin, igor@chub.in, 2004-2006
# Environment variables set by the script:
#
# L3_SESSION_ID - uniq id of the LiLaLo-session
# L3_PARENT_TTY - name of tty on which script is running
# PS1 - intercative shell prompt in which LiLaLo hides
# various information about the command
# L3_TAMPERED_EDITORS - list of editors which are tampered with functions
# Functions with the names starting _l3_ are internal.
# Such functions are unset before this rc script exits
_l3_editors_to_tamper='/bin/vi /usr/bin/vi /usr/bin/vim /bin/ee /usr/bin/ee /usr/bin/pico /usr/bin/nano /usr/local/bin/vim'
_l3_start()
{
echo $- | grep -q i || return 0
if _l3_is_running_here
then
_l3_env
_l3_init_prompt
l3_fix_prompt
_l3_tamper_editors
_l3_tamper_commands
_l3_unset_internal
else
_l3_start_session
_l3_run_script
fi
}
# ===================== STAGE 1 ============================
_l3_is_running_here()
{
ps waux | awk '{print $2" "$11 }' | grep -q ^$PPID" "script
return $?
# Check if ^^^^ run on Linux
# and del vvvv this if it does
export L3_TTY=`/usr/bin/tty`
uname -a | egrep -qi '(bsd|darwin)' && bsd=yes
proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
# freeBSD:
[ -n "$bsd" ] && \
proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
[ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -q script
}
_l3_start_session()
{
export L3_SESSION_ID=${RANDOM}${RANDOM}${RANDOM}${RANDOM}-`date +%s`
export L3_HOME=~/.lilalo/
mkdir -p $L3_HOME
tty=`/usr/bin/tty`
uname -a | egrep -qi '(bsd|darwin)' && bsd=yes
parent=`cat /proc/$PPID/cmdline 2> /dev/null`
[ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`"
system=`uname -rs`
login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
#[ -n "$bsd" ] && login_from="" #FIXME!
start_time=`date +%s`
hostname=`hostname -f 2> /dev/null`
[ -n "$bsd" ] && hostname=`hostname`
cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
<session>
<local_session_id>$L3_SESSION_ID</local_session_id>
<hostname>$hostname</hostname>
<user>$USER</user>
<uid>$UID</uid>
<login_from>$login_from</login_from>
<tty>$tty</tty>
<system>$system</system>
<parent>$parent</parent>
<ppid>$PPID</ppid>
<pid>$$</pid>
<start_time>$start_time</start_time>
<lang>$LANG</lang>
</session>
INFO
unset parent system login_from start_time hostname tty
}
_l3_run_script()
{
uname -a | egrep -qi '(bsd|darwin)' && bsd=yes
flush="-f" #linux
[ -n "$bsd" ] && flush="-t 0" #freebsd
export L3_PARENT_TTY=`/usr/bin/tty`
if [ -n "$bsd" ]
then
exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script ${0#-}
else
exec script $flush -c ${0#-} -q $L3_HOME/${L3_SESSION_ID}.script
fi
}
# ===================== STAGE 2 ============================
_l3_env()
{
trap l3_close_session 2
trap l3_close_session EXIT
true
}
l3_close_session()
{
(
echo '<history>'
history | sed 's/&/\&/; s/</\</g; s/>/\>/g'
echo '</history>'
) >> $L3_HOME/$L3_SESSION_ID.info
}
_l3_init_prompt()
{
PS1='[\u@\h:\W]\$ '
[ $UID = 0 ] \
&& PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
|| PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
export PS1
}
l3_fix_prompt()
{