devi@70: #!/bin/sh
devi@70: 
devi@70: 
devi@76: # Environment variables set by the script:
devi@76: #
devi@76: #       L3_SESSION_ID   - uniq id of the LiLaLo-session
devi@76: #       L3_PARENT_TTY   - name of tty on which script is running
devi@76: #       PS1             - intercative shell prompt in which LiLaLo hides
devi@76: #                         various information about the command
devi@76: #       L3_TAMPERED_EDITORS - list of editors which are tampered with functions
devi@76: 
devi@76: 
devi@76: # Functions with the names starting _l3_ are internal.
devi@76: # Such functions are unset before this rc script exits
devi@76: 
devi@76: _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'
devi@76: 
devi@70: _l3_start()
devi@70: {
devi@80:     echo $- | grep -q i || return 0
devi@79:     if _l3_is_running_here
devi@76:     then
devi@76:         _l3_env
devi@76:         _l3_init_prompt
devi@76:          l3_fix_prompt
devi@76:         _l3_tamper_editors
devi@76:         _l3_tamper_commands
devi@76:         _l3_unset_internal
devi@79:     else
devi@79:         _l3_start_session
devi@79:         _l3_run_script
devi@76:     fi
devi@76: }
devi@76: 
devi@76: # ===================== STAGE 1 ============================
devi@76: 
devi@79: _l3_is_running_here()
devi@76: {
devi@79: 	ps waux | awk '{print $2" "$11 }' | grep -q ^$PPID" "script 
devi@79: 	return $?
devi@79: 
devi@79: # Check if ^^^^ run on Linux
devi@79: # and del  vvvv this if it does
devi@79: 
devi@76:     export L3_TTY=`/usr/bin/tty` 
devi@79:     uname -a | grep -qi bsd && bsd=yes
devi@70:     proc_on_the_term=`w | grep "${L3_TTY##/dev/}" | awk '{print $8;}'`
devi@70:     # freeBSD: 
devi@70:     [ -n "$bsd" ] && \
devi@70:     proc_on_the_term=`w | grep "${L3_TTY##/dev/tty}" | awk '{print $6;}'`
devi@70: 
devi@79:     [ -n "$proc_on_the_term" ] && echo $proc_on_the_term | grep -q script
devi@70: }
devi@70: 
devi@70: _l3_start_session()
devi@70: {
devi@79:     export L3_SESSION_ID=${RANDOM}${RANDOM}${RANDOM}${RANDOM}-`date +%s`
devi@71:     export L3_HOME=~/.lilalo/
devi@70:     mkdir -p $L3_HOME
devi@70: 
devi@79:     tty=`/usr/bin/tty`
devi@70:     uname -a | grep -qi bsd && bsd=yes
devi@70:     parent=`cat /proc/$PPID/cmdline 2> /dev/null`
devi@79:     [ -z "$parent" ] && parent="`ps waux | awk '{if ($2 == '$PPID') print $11; }'`"
devi@70:     system=`uname -rs`
devi@79:     login_from=`who | grep "${tty##/dev/}" | awk '{print $6;}' | tr -d '()'`
devi@70:     #[ -n "$bsd" ] && login_from="" #FIXME!
devi@70:     start_time=`date +%s`
devi@70:     hostname=`hostname -f 2> /dev/null`
devi@70:     [ -n "$bsd" ] && hostname=`hostname`
devi@70: 
devi@70:     cat <<INFO > $L3_HOME/$L3_SESSION_ID.info
devi@70: <session>
devi@70:     <local_session_id>$L3_SESSION_ID</local_session_id>
devi@70:     <hostname>$hostname</hostname>
devi@70:     <user>$USER</user>
devi@70:     <uid>$UID</uid>
devi@70:     <login_from>$login_from</login_from>
devi@79:     <tty>$tty</tty>
devi@70:     <system>$system</system>
devi@70:     <parent>$parent</parent>
devi@70:     <ppid>$PPID</ppid>
devi@70:     <pid>$$</pid>
devi@70:     <start_time>$start_time</start_time>
devi@76:     <lang>$LANG</lang>
devi@70: </session>
devi@70: INFO
devi@70: 
devi@79:     unset parent system login_from start_time hostname tty
devi@70: }
devi@70: 
devi@70: _l3_run_script()
devi@70: {
devi@70:     uname -a | grep -qi bsd && bsd=yes
devi@70:     flush="-f"                          #linux
devi@70:     [ -n "$bsd" ] && flush="-t 0"       #freebsd
devi@76:     export L3_PARENT_TTY=`/usr/bin/tty`
devi@70:     exec script $flush -q $L3_HOME/${L3_SESSION_ID}.script
devi@70: }
devi@70: 
devi@76: # ===================== STAGE 2 ============================
devi@76: 
devi@76: _l3_env()
devi@76: {
devi@76:     trap l3_close_session 2
devi@76:     trap l3_close_session EXIT
devi@76:     true
devi@76: }
devi@76: 
devi@76: l3_close_session()
devi@70: {
devi@71:     ( 
devi@71:         echo '<history>'
devi@71:         history | sed 's/&/\&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
devi@71:         echo '</history>'
devi@71:     ) >> $L3_HOME/$L3_SESSION_ID.info
devi@70: }
devi@70: 
devi@76: _l3_init_prompt()
devi@76: {
devi@76:     PS1='[\u@\h:\W]\$ '
devi@77:     [ $UID = 0 ] \
devi@77:         && PS1='\[\033[0;31m\]'$PS1'\[\033[0m\]' \
devi@77:         || PS1='\[\033[0;32m\]'$PS1'\[\033[0m\]' \
devi@76:     export PS1
devi@76: }
devi@76: 
devi@76: l3_fix_prompt()
devi@76: {
devi@97:     export PS1='\[$(l3_save_last_line)v2#\!#$?#$UID#$$#$(/bin/date +%s)#$PWD#\033[1024D\033[K\]'$PS1
devi@76: }
devi@76: 
devi@76: _l3_tamper_editors()
devi@76: {
devi@76:     for editor_file in $_l3_editors_to_tamper
devi@76:     do
devi@76:         [ -x $editor_file ] || continue
devi@76:         editor_name=${editor_file##*/}
devi@76:         eval "
devi@76:         $editor_name() { 
devi@76:             if [ -d \"\$1\" ] 
devi@76:             then
devi@76:                 $editor_file \"\$1\"
devi@76:                 return \$?
devi@76:             else
devi@76:                 TIME=\"\`date +%s\`\"
devi@76:                 DIR=\"\"
devi@76:                 [ \"\${1#/}\" = \"\$1\" ] && DIR=\"\$PWD/\"
devi@80:                 DIFFNAME=\"\${L3_SESSION_ID}_\${TIME}\`echo \$DIR\$1| sed s@_@__@ | sed 's@/@_@g'\`.diff\"
devi@76:                 old_file=\"/tmp/l3-saved-\$\$.\$RANDOM.\$RANDOM\"
devi@76:                 /bin/cp -- \"\$1\" \"\$old_file\" 2> /dev/null
devi@76:                 $editor_file \"\$@\" || ERR=\$?
devi@80:                 [ -e \"\$old_file\" ] && diff \"\$old_file\" \"\$1\" > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
devi@80:                 if [ "$?" == 2 ] 
devi@80:                 then 
devi@80:                     diff /dev/null \"\$1\"  > \"\$L3_HOME/\$DIFFNAME\" 2> /dev/null
devi@80:                 fi
devi@76:                 /bin/rm \"\$old_file\" 2> /dev/null
devi@76:                 return \$ERR
devi@76:             fi
devi@76:         }
devi@76:         "
devi@76:         L3_TAMPERED_EDITORS="$L3_TAMPERED_EDITORS $editor_name"
devi@76:     done
devi@76:     [ -n "$L3_TAMPERED_EDITORS" ] && export L3_TAMPERED_EDITORS
devi@76: }
devi@76: 
devi@76: _l3_tamper_commands()
devi@76: {
devi@76:     tty()
devi@76:     {
devi@76:         [ -n "$L3_PARENT_TTY" ] && echo $L3_PARENT_TTY || /usr/bin/tty
devi@76:     }
devi@76: }
devi@76: 
devi@76: _l3_unset_internal()
devi@76: {
devi@76:     unset `set | grep '^_l3_.*()' | sed 's/()//'`
devi@76:     unset `set | grep '^_l3_.*=' | sed 's/=.*//'`
devi@76: }
devi@78: 
devi@78: 
devi@78: l3shot()
devi@78: {
devi@78:     if [ -x "`which xwd`" ]
devi@78:     then
devi@78:         _l3_home=${L3_HOME:-~/.lilalo}
devi@78:         shot_name="${L3_SESSION_ID}_`date +%s`".xwd
devi@78:         echo -n Choose window to be shoot ... >&2
devi@78:         [ -d ${_l3_home} ] || mkdir -p ${_l3_home}
devi@78:         xwd -out "$_l3_home/$shot_name" \
devi@78:         && echo  Ok\
devi@78:         && echo Shot was successful. \
devi@78:         && echo Screenshot is written to ${_l3_home}/${shot_name} \
devi@78:         && echo Screenshot will appears in the lablog.
devi@78:     else
devi@78:         {
devi@78:             echo
devi@78:             echo "Can't make screenshot :("
devi@78:             echo 
devi@78:             echo I must use program xwd to make screenshot, 
devi@78:             echo but it seems not to be installed
devi@78:             echo Try to find the program in the \"xbase-clients\" package
devi@78:             echo
devi@78:         }   >&2 
devi@78:     fi
devi@78: }
devi@97: 
devi@97: # Append lines from "$@" files to the end of the shell history
devi@97: hist_append () 
devi@97: { 
devi@97:     eval $(cat "$@" | sed 's/"/\\\\"/g' | while read line; do echo history -s \"$line\"\;; done); 
devi@97: }
devi@97: 
devi@97: l3_save_last_line ()
devi@97: {
devi@97:     (
devi@97:     echo '<cline>'
devi@97:     history 1 | sed 's/&/&amp;/; s/</\&lt;/g; s/>/\&gt;/g'
devi@97:     echo '</cline>'
devi@97:     ) >> $L3_HOME/$L3_SESSION_ID.info
devi@97: }
devi@97: 
devi@97: