#!/bin/sh
#
# lg: log checking/maintainance utility
#
# Copyright (C) 1997 Mark Rigby-Jones
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# If you do not have a copy of the GNU General Public License then
# you can get one by writing to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
#

version="lg v1.4 by Mark Rigby-Jones, 17th Jan 1997"

logfile="/var/admin/log"
tmpfile="/tmp/lg.$$"
filedir="$HOME/.lg"
pager="less -eis"
editor="pico"

if (test $EUID = 0); then
  echo "lg: cannot be run as root - please return to your normal uid!"
  exit 1
fi

if !(test -d $filedir); then
  mkdir $filedir
fi

case "$1" in
  -a|--add)
    if (test $# = 2); then
      logfile="$2"
    fi
    if (test -n "$EDITOR"); then
      editor=$EDITOR
    fi
    if (test -n "$VISUAL"); then
      editor=$VISUAL
    fi
    $editor $tmpfile
    if (test -e $tmpfile); then
      (echo -e "\n"`date`" on "`uname -n` ; cat $tmpfile) | tr -s '\n' >> $logfile
      rm $tmpfile
    fi
    exit 0
    ;;
  -p|--page|-t|--tail)
    if (test $# = 2); then
      logfile="$2"
    fi
    ;;
  -h|--help)
    cat << 'EOT'
Usage: lg [function] [logfile]
Check whether a logfile (/var/admin/log by default) has been changed, and
print a brief message if it has since last read using lg. Other functions
shown below. Note that for security reasons, you should not run lg whilst
root - it will exit with an error message.

-a, --add       append an entry to the logfile - allows editing of a
                 temporary file which is added to the logfile together
                 with time, date and host machine information.
-p, --page      view the entire logfile using a pager
-t, --tail      view the last 24 lines of the logfile
-h, --help      print this information and exit
-V, --version   print version information and exit

If you need more info, then read the man page!
EOT
    exit 0
    ;;
  -V|--version)
    echo $version
    exit 0
    ;;
  *)
    if (test $# = 1); then
      logfile="$1"
    fi
    ;;
esac

if (test -e $logfile); then
  echo $logfile > $tmpfile
  chkfile=$filedir/`tr / . < $tmpfile`
  rm $tmpfile
else
  echo "lg: $logfile not found / illegal option"
  echo "try \`lg --help' for more information"
  exit 1
fi

case "$1" in
  -p|-page)
    if (test -n "$PAGER"); then
      pager=$PAGER
    fi
    touch $chkfile
    $pager $logfile
    ;;
  -t|--tail)
    touch $chkfile
    tail -24 $logfile
    ;;
  *)
    if !(test -e $chkfile && test $chkfile -nt $logfile); then
      echo "$logfile has been modified"
    fi
    ;;
esac
