#!/bin/bash
#==========================================================================
#  P  A  R  A  L  O  G  G  E  R     (C) 1999-2002 Geir Torstein Kristiansen
#========================================================================== 
VERSION="1.0.1"
PCONFPATH="/etc/paralogger"
CONFIGFILE="${HOME}/.paraloggerrc"

#   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.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

default () {
#---------------------------------------------------------------------------
# Defaults -  Don't edit this directly here, use the command line options :)
#---------------------------------------------------------------------------
DBG="0"
DBG2="0"
GEN="0"
DTOP="0"
HEIGHT="4"
WIDTH="80"
PLACEMENT="--br"
VOFFSET="0"
HOFFSET="0"
MODE="--vcas"
FONT="6x10"
FONTFX="none"
COLOR="white"
SHADE="0"
CMOD="256"
CMODR="256"
CMODG="256"
CMODB="256"
TINT="0xffffff" 
COLORTAIL="0"
WAIT="0"
STOPLIST="1"
#--------------------------------------------------------------------------
}

VARS="VERSION GEN DTOP HEIGHT WIDTH WINH WINV PLACEMENT VOFFSET HOFFSET MODE FONT FONTFX COLOR SHADE CMOD CMODR CMODG CMODB TINT COLORTAIL WAIT STOPLIST LOGS"

show_usage () {
cat << EOF
Usage: `basename $0` [OPTIONS] [FILES]

Shell script to "tail" the system log(s) in borderless transparent Eterm(s).

 -h, -help		Show this help
 --version		Show version and exit
 -v			Verbose mode (level 1)
 -vv			Verbose mode (level 2)
 -vvv			Verbose mode (level 3)
 -c [FILE]		Configuration file (default is ~/.paraloggerrc)
 -load			Load saved configuration file
 -save			Save specified options to configuration file
 -g			Generate a stand alone shell script
 -D [DESKTOP]		Virtual desktop start the Eterm(s) on (default is 0)
 -H [LINES]		Height of the Eterm (default is 4)
 -W [CHARACTERS]	Width of the Eterm (default is 80)
 -xx (one of)		Base placement 

			  Your screen
			 -------------
			| -tl -tc -tr |
			| -ml -mc -mr |
			| -bl -bc -br |
			 -------------

 -vo [PIXELS]		Vertical offset of the Eterm (default is 0)
 -ho [PIXELS]		Horizontal offset of the Eterm (default is 0)
 -single		No cascading (tail all logs in one Eterm)
 -vcas			Vertical cascade mode (default)
 -hcas			Horizontal cascade mode
 -F, -font [FONT]	What font to use
 -font-fx		Eterm 0.9.1 only (see the Eterm 0.9.1 ChangeLog)
 -f [COLOR]		Eterm text color (colorname or hex)
 -shade [PERCENTAGE]	Darkens the transparent background (not Eterm 0.9.0)
 -cmod [VALUE]		Overall color modifier (only Eterm 0.9.x)
 -cmod-red [VALUE] 	Red color modifier (only Eterm 0.9.x)
 -cmod-green [VALUE]	Green color modifier (only Eterm 0.9.x)
 -cmod-blue [VALUE]	blue color modifier (only Eterm 0.9.x)
 -tint  [VALUE]         Tinting of the transparent background (not Eterm 0.9.0)
  (0.8.x must use 0x000000 hex format, 0.9.1 also understands colornames)
 -color			Use colortail instead of regular tail
 -nowait		Run the Eterm in the background (releases the prompt)
 -nostop		Do not use discard files based on certain criteria
EOF
exit 1
}

out () {
echo -e $1 >&1
}

err () {
if [ "$1" = "-n" ]; then
	echo $2 >&2
else
	echo $1 >&2
	exit 1
fi
}


verbose () {
local op="0"
# verbose [level] ["message"]

if [ "$1" = "1" ]; then
	if [ "$DBG" = "1" ]; then
		op="1"
	fi
fi

if [ "$1" = "2" ]; then
	if [ "$DBG2" = "1" ]; then
		op="1"
	fi
fi

if [ "$1" = "3" ]; then
	if [ "$DBG3" = "1" ]; then
		op="1"
	fi
fi

if [ "$op" = "1" ]; then
	echo "$2" >&2
fi
}

cparser () {
local v l
if [ -n "$1" ]; then
GETOPT=`getopt -n $0 -s sh -a -o hvc:gD:H:W:F:f: -l help,version,load,save,vv,vvv,tl,tc,tr,ml,mc,mr,bl,bc,br,vo:,ho:,single,vcas,hcas,font:,font-fx:,shade:,cmod:,cmod-red:,cmod-green:,cmod-blue:,tint:,color,nowait,nostop -- "$@"`

	eval set -- "$GETOPT"
	while :; do
		case "$1" in
			-h|--help)
				show_usage; shift ;;
			--version)
                                out "Paralogger $VERSION"; exit 0; shift ;;
			-v)
                                DBG="1"; shift ;;
			--vv)
				DBG="1"; DBG2="1"; shift ;;
			--vvv)
				DBG="1"; DBG2="1"; DBG3="1" ; shift ;;
			-c)
				CONFIG="$2"; shift 2 ;;
			--load)
				loadconf ; shift ;;
			--save)
				SAVECONF="1" ; shift ;;
			-g)
				GEN="1"; shift ;;
			-D)
				isitint $2 || err "-D requires an integer"
				DTOP="$2"; shift 2 ;;
			-H)
				isitint $2 || err "-H requires an integer"
				HEIGHT="$2"; shift 2 ;;
                        -W)
				isitint $2 || err "-W requires an integer"
				WIDTH="$2"; shift 2 ;;
			--tl|--tc|--tr|--ml|--mc|--mr|--bl|--bc|--br)
				PLACEMENT="$1"; shift ;;
			--vo)
				isitint $2 || err "-vo requires an integer"
				VOFFSET="$2"; shift 2 ;;
			--ho)
				isitint $2 || err "-ho requires an integer"
				HOFFSET="$2"; shift 2 ;;
			--single|--vcas|--hcas)
				MODE="$1"; shift ;;
			-F|--font)
				FONT="$2"; shift 2 ;;
			--font-fx)
				FONTFX="$2"; shift 2 ;;
			-f)
				COLOR="$2"; shift 2 ;;
			--shade)
				isitint $2 || err "-shade requires an integer"
				SHADE="$2"; shift 2 ;;
			--cmod)
				CMOD="$2"; shift 2 ;;
			--cmod-red)
				CMODR="$2"; shift 2 ;;
			--cmod-green)
				CMODG="$2"; shift 2 ;;
			--cmod-blue)
				CMODB="$2"; shift 2 ;;
			--tint)
				TINT="$2"; shift 2 ;;
			--color)
				COLORTAIL="1"; shift ;;
			--nowait)
				WAIT="1"; shift ;;
			--nostop)
				STOPLIST="0"; shift ;;
			--) shift; break ;;
			*) err "Aborting! internal getopt error!" ;;
		esac
	done
	# $* now only contains stuff that weren't recognized as a switch.
	# That means the log files to tail.
	for l in $*; do
		if [ -n "$LOGS" ]; then
			LOGS="$LOGS $l"
		else
			LOGS="$l" # remove whitespace
		fi
	done
else
	show_usage
fi
}

loadconf () {
local file i value

if [ -n "$CONFIG" ]; then
	if [ -f "$CONFIG" -a -r "$CONFIG" ]; then
		file="$CONFIG"
	else
		err "Aborting! unable to read configuration file"
	fi
else
	if [ -f "$CONFIGFILE" -a -r "$CONFIGFILE" ]; then
		file="$CONFIGFILE"
	elif [ -f "${PCONFPATH}/paralogger.conf" -a -r "${PCONFPATH}/paralogger.conf" ]; then
		file="${PCONFPATH}/paralogger.conf"
	else
		err -n "Aborting! unable to read configuration file"
		err "Are you sure you did a \"make install\" ?"
	fi
fi

verbose 1 "loadconf () using: $file"

for i in $VARS; do
	export $i
	value=`grep -v "#" $file | grep -w $i | cut -f2 -d=`
	if [ -z "$value" ]; then
		err "Aborting! unable to load value for \"$i\" from configuration file"
	fi
	verbose 2 "loadconf () parse: $i=$value"
	case $i in
		HEIGHT|WIDTH|FONT)
			export OLD${i}="$value" ;;
	esac
	export $i="$value"
done
}

saveconf () {
local yesno file i value 

if [ "$SAVECONF" = "1" ]; then

	if [ -n "$CONFIG" ]; then
		file="$CONFIG"
	else
		file="$CONFIGFILE"
	fi

	if [ -f "$file" ]; then
		while :; do
			echo -n "\"$file\" allready exists, overwrite [y|n] ? "
			read yesno
			case $yesno in
				n|N)
					err "Aborting! not writing to \"$file\"" ;;
				y|Y)
					break ;;
			esac
		done
	else
		touch $file
	fi

	if [ ! -w "$file" ]; then
		err "Aborting! cannot write configuration file"
	fi

	verbose 1 "saveconf () using: $file"

	echo "# Automatically generated by Paralogger $VERSION" > $file

	for i in $VARS; do
		export $i
		value=`export -p | grep -w $i | cut -f2 -d\"`
		verbose 2 "saveconf () wrote: $i=$value"
		echo "$i=$value" >> $file
	done
fi
}

chkprog () {
local i EEK="0"
for i in $@; do
	if [ -x "`which $i 2>/dev/null`" ]; then
		verbose 3 "chkprog () program \"$i\" is executable"
	else
		verbose 3 "chkprog () program \"$i\" is not in path"
		EEK="1"
	fi
done

if [ "$EEK" = "1" ]; then
	return 1
else
	return 0
fi
}

isitint () {
local i EEK="0"
for i in $@; do
	if [ "$i" = "0" ]; then
		verbose 3 "isitint () $i is an integer"
	else
		expr "$i" + 0 >/dev/null 2>&1 &&
		verbose 3 "isitint () $i is an integer" || 
	   {
		verbose 3 "isitint () $i is not an integer"
		EEK="1"
	   }
	fi
done

if [ "$EEK" = "1" ]; then
	return 1
else
	return 0
fi
}

amisane () {
local i chk

if [ "$COLORTAIL" = "1" ]; then
	if [ -r "$PCONFPATH/colortail.map" -a -r "$PCONFPATH/generic.colortail" ]; then
		verbose 2 "amisane () found configuration files"
	else
		err -n "Aborting! cannot find the configuration files." 
		err "Are you sure you did \"make install\" ?"
	fi
fi

chk="Eterm xdpyinfo xwininfo xlsfonts rev"

if [ "$COLORTAIL" = "0" ]; then
	chk="$chk tail"
else
	chk="$chk colortail"
fi

for i in $chk; do
	chkprog $i || err "Aborting! \"$i\" not in path"
done 

if [ -z $DISPLAY ]; then
	err "No \$DISPLAY variable is set, are you sure you are running X?"
else
	verbose 2 "amisane () \$DISPLAY variable is set"
fi
}

termver () {
local ETERMVER
ETERMVER=`Eterm --version | head -1 | awk '{ print $2 }'`
if [ -z "$ETERMVER" ]; then
	err "Unable to determine Eterm version, please check your Eterm installation."
else
	verbose 1 "termver () found: Eterm $ETERMVER"  
fi

case $ETERMVER in
	0.8.*)
		ETERM="Eterm -D $DTOP --shade $SHADE --tint $TINT --no-cursor --scrollbar=off --menubar=off -O -W -x" ;;
	0.9)
		ETERM="Eterm -D $DTOP --cmod $CMOD --cmod-red $CMODR --cmod-green $CMODG --cmod-blue $CMODB --no-cursor --scrollbar=off -O -x" ;;
	0.9.1)
		ETERM="Eterm -D $DTOP --shade $SHADE --cmod $CMOD --cmod-red $CMODR --cmod-green $CMODG --cmod-blue $CMODB --tint $TINT --no-cursor --scrollbar=off --buttonbar=off --font-fx $FONTFX -O -x -q" ;;
 	*)
		ETERM="Eterm -D $DTOP --shade $SHADE --cmod $CMOD --cmod-red $CMODR --cmod-green $CMODG --cmod-blue $CMODB --tint $TINT --no-cursor --scrollbar=off --buttonbar=off -O -x"

		err -n "Warning! using unsupported Eterm version: $ETERMVER." ;;
esac

verbose 1 "termver () using: $ETERM"
export ETERM
}

fntxist () {
local i found
for i in `xlsfonts`; do
	if [ "$i" = "$FONT" ]; then
		found="1"
		break
	fi
done
	
if [ "$found" = "1" ]; then
	verbose 1 "fntxist () found \"$FONT\" font"
else
	out "Warning! unable to find \"$FONT\" font, reverting to \"fixed\""
	FONT="fixed"
fi
}

logxist () {
local i n logfile discard oklogs

for i in $LOGS; do
	if [ ! -e "$i" ]; then
		err -n "\"$i\" does not exist."
		n=1
	else
		if [ -f "$i" ]; then
			if [ "$STOPLIST" != "0" ]; then
				discard="0"
				logfile=`basename $i`
	
				case $logfile in
						lastlog*) stop="1" ;;
						sendmail.st) stop="1" ;;
						wtmp*) stop="1" ;;
						utmp*) stop="1" ;; 
						dmesg) stop="1" ;;
						boot.log*) stop="1" ;;
						*) stop="0" ;;
				esac

				if [ "$stop" = "1" ]; then
					discard="1"
					verbose 1 "logxist () discarding $i is in stop list"
				fi

				afterdot=`echo $logfile | rev | cut -f1 -d"."`
				isitint $afterdot && {
				discard="1"
				verbose 1 "logxist () discarding $i is probably not written to anymore"
				} 
				
			fi

			if [ "$discard" != "1" ]; then
				if [ -n "$oklogs" ]; then
					oklogs="$oklogs $i"
				else
					oklogs="$i"
				fi
			fi
		else
			if [ -d "$i" ]; then
				verbose 1 "logxist () discarding $i is a directory"
			else
				verbose 1 "logxist () discarding $i is not a normal file"
			fi
		fi
	fi
done

if [  "$n" = 1 ]; then 
	exit 1
fi

LOGS="$oklogs"

if [ -z "$LOGS" ]; then
	err "Aborting! no logs to tail."
fi
}

haccess () {
local i n
if [ `whoami` = root ]; then 
		verbose 1 "haccess () running as root"
	plcment	
	
else
	for i in $LOGS; do
		if [ ! -r "$i" ]; then
			verbose 2 "haccess () no access to $i" 
			n=1 ;
		fi
	done

		if [  "$n" = 1 ]; then
			verbose 1 "haccess () no access -> authenticating"
			authenticate

		else
			verbose 2 "haccess () have access as `whoami`"
			plcment	
		fi
fi
}

getreso () {
HRES=`xdpyinfo 2>/dev/null | awk '/dimension/{ print $2 }' | cut -f1 -dx`
VRES=`xdpyinfo 2>/dev/null | awk '/dimension/{ print $2 }' | cut -f2 -dx`

isitint $HRES $VRES || err "Aborting! xdpyinfo returning unexpected data."
verbose 1 "getreso () screen resolution is ${HRES}x${VRES}" 

export HRES VRES
}

cntlogs () {
local a
for i in $LOGS; do 
	a=$(($a+1)) 
done

	NMBRLOGS="$a"
	verbose 2 "cntlogs () number of logs is $a"
}

reverse () {
local i t a l tac
verbose 2 "reverse () reversing logs: $LOGS"

chkprog tac && tac="yes"

if [ "$tac" = "yes" ]; then 
	verbose 2 "reverse () using: tac"
	LOGS=`echo $LOGS | tac`
else
	verbose 2 "reverse () using: builtin"
	a="$NMBRLOGS"
		for i in $LOGS; do
			t=0
			for i in $LOGS; do
				t=$(($t+1))
					if [ $t = $a ];then
						if [ -n "$l" ]; then
							l="$l $i"
						else
							l="$i" # remove whitespace
						fi 
					fi
			done
			a=$(($a-1))
	done
	LOGS=$l
fi
verbose 2 "reverse () logs are now: $LOGS"
}

loctcfg () {
if [ -z "$1" ]; then
	err "loctcfg () called without an argument"
fi
COLORTAILCONF=`grep $1 ${PCONFPATH}/colortail.map | cut -f1`

	if [ -z $COLORTAILCONF ]; then
		COLORTAILCONF="generic.colortail"
	fi
verbose 2 "loctcfg () log \"$1\" mapped to \"${PCONFPATH}/${COLORTAILCONF}\""
echo "${PCONFPATH}/${COLORTAILCONF}"
}

winsize () {
local run c cal failone windowid
if [ "$OLDHEIGHT" != "$HEIGHT" ]; then
	run="1"
fi

if [ "$OLDWIDTH" != "$WIDTH" ]; then
        run="1"
fi

if [ "$OLDFONT" != "$FONT" ]; then
	run="1"
fi 

if [ "$run" != "1" ]; then
	verbose 2 "winsize () not calibrating"
fi

if [ "$run" = "1" ]; then
	unset WINH WINV
	c="0"
	SIZE="${WIDTH}x${HEIGHT}"

	chkprog paracal && cal="paracal cal" || cal="sleep 1h"

	$ETERM -g $SIZE -n paracal-$$ -T paracal-$$ -F $FONT -e $cal &

	TMPPID="$!"
	verbose 1 "winsize () calibrating with geometry $SIZE"

	until [ -n "$WINH" -a -n "$WINV" ]; do
		WINH=`xwininfo -name paracal-$$ 2>/dev/null | awk '/Width/{ print $2 }'`
		WINV=`xwininfo -name paracal-$$ 2>/dev/null | awk '/Height/{ print $2 }'`
		sleep 1

		ETERMALIVE=`ps $TMPPID 2>/dev/null | wc -l | awk '{ print $1 }'`
			if [ "$ETERMALIVE" != 2 ]; then
				err "Aborting! please check your Eterm installation."
			fi

		c=$(($c + 1))
			if [ "$c" = 5 ]; then
				failone="1"
				verbose 2 "winsize () method 1 - failed, trying alternative method"
				break
			fi

		verbose 2 "winsize () method 1 - waiting for xwininfo data $c"
	done

	if [ "$failone" = "1" ]; then
		c="0"
		windowid=`xlsclients -la | grep -w -B 2 paracal-$$ | head -n 1 | cut -f2 -d" " | cut -f1 -d":"`
		until [ -n "$WINH" -a -n "$WINV" ]; do
			WINH=`xwininfo -id $windowid | awk '/Width/{ print $2 }'`
			WINV=`xwininfo -id $windowid | awk '/Height/{ print $2 }'`
			sleep 1
			c=$(($c + 1))
			if [ "$c" = "10" ]; then
				kill $TMPPID 2>/dev/null
				err "Aborting! unable to get any values from xwininfo"
			fi
			verbose 2 "winsize () method 2 - waiting for xwininfo data $c"
		done
	fi

	
		kill $TMPPID 2>/dev/null || {
			verbose 2 "winsize () an error occured while killing $TMPPID"
		}
fi

verbose 1 "winsize () size of Eterm is ${WINH}x${WINV}"
isitint $WINH $WINV || err "Aborting! xwininfo returning unexpected data."
export WINH WINV
}

chktail () {
local TAILVER
if [ "$COLORTAIL" = "0" ]; then
	OS=`uname`
	case $OS in     
		Linux)          
			TAILVER=`tail --version | head -1 | awk '{ print $4 }' | cut -f1 -d.`
				if [ "$TAILVER" = "2" ]; then
					TAIL="tail -n $HEIGHT -q --follow=name" # Textutils 2.x
				else
					TAIL="tail -n $HEIGHT -qf" # Textutils 1.x
				fi
			verbose 1 "chktail () found: `tail --version | head -n1`" ;;
		*BSD)
			TAIL="tail -n $HEIGHT -F" ;;
		*)
			TAIL="tail -f"
        esac

	verbose 1 "chktail () using: $TAIL"
	export TAIL
else
	verbose 1 "chktail () found: `colortail --version | head -n1`"
fi
}


trapids () {
if [ $WAIT = 0 ]; then
	verbose 2 "trapids () trapping and waiting for pids: $PIDS"
	trap "kill $PIDS 2>/dev/null; exit 1" 1 2 3 15
	wait $PIDS
fi
}

runterm () {
local s TAILTMP COLORTMP COLORT
verbose 1 "runterm () $i has geometry $GEOM"


if [ "$COLORTAIL" = "1" ]; then

	if [ "$MODE" = "--single" ]; then
		for s in $LOGS; do
			COLORTMP="`loctcfg $s`"
			TAILTMP=" $s"

			if [ -z "$TAILCMD" ]; then
				TAILCMD="$TAILTMP"
			else
				TAILCMD="$TAILCMD $TAILTMP"
			fi
 
			if [ -z "$COLORT" ]; then
				COLORT="-k $COLORTMP"
			else
				COLORT="$COLORT,$COLORTMP"
			fi
		done
		TAILCMD="colortail -n $HEIGHT -q -f $COLORT $TAILCMD"
	else
		TAILCMD="colortail -n $HEIGHT -q -f -k `loctcfg $i`"
	fi
else
	if [ "$MODE" != "--single" ]; then
		TAILCMD="$TAIL"
	else
		TAILCMD="$TAIL $LOGS"
	fi
fi

if [ "$GEN" = "1" ]; then
	if [ "$MODE" != "--single" ]; then
		out "$ETERM -F $FONT -f $COLOR -e $TAILCMD $i &"
	else
		out "$ETERM -f $FONT -f $COLOR -e $TAILCMD &"
	fi
else
	if [ "$MODE" != "--single" ]; then
		$ETERM -g $GEOM -F $FONT -f $COLOR -n "Paralogger $i" -t "Paralogger $i" -e $TAILCMD $i &
	else
		$ETERM -g $GEOM -F $FONT -f $COLOR -n "Paralogger $i" -t "Paralogger $i" -e $TAILCMD &		
	fi

	CNT=$(($CNT + 1)) 
	PIDS="$! $PIDS" 

	verbose 2 "runterm () pid of Eterm is $!"

	if [ $MODE = "--single" -o $CNT = $NMBRLOGS ]; then
		trapids
	fi
fi
}

plcment () {
case $PLACEMENT in
	--tl) # topleft
	VPOS=$((0+$VOFFSET))
	HPOS=$((0+$HOFFSET))
	;;
	--tc) # topcenter
	VPOS=$((0+$VOFFSET))
	HPOS=$((($HRES/2)-($WINH/2)+$HOFFSET))
	;;
	--tr) # topright
	VPOS=$((0+$VOFFSET))
	HPOS=$(($HRES-$WINH-$HOFFSET))
	;;

	--bl) # bottomleft
	VPOS=$(($VRES-$WINV-$VOFFSET))
	HPOS=$((0+$HOFFSET))
	;;
	--bc) # bottomcenter
	VPOS=$(($VRES-$WINV-$VOFFSET))
	HPOS=$((($HRES/2)-($WINH/2)+$HOFFSET))
	;;
	--br) # bottomright
	VPOS=$(($VRES-$WINV-$VOFFSET))
	HPOS=$(($HRES-$WINH-$HOFFSET))
	;;

	--ml) # middleleft
	VPOS=$((($VRES/2)-($WINV/2)+$VOFFSET))
	HPOS=$((0+$HOFFSET))
	;;
	--mc) # middlecenter
	VPOS=$((($VRES/2)-($WINV/2)+$VOFFSET))
	HPOS=$((($HRES/2)-($WINH/2)+$HOFFSET))
	;;
	--mr) # middleright
	VPOS=$((($VRES/2)-($WINV/2)+$VOFFSET))
	HPOS=$(($HRES-$WINH-$HOFFSET))
	;;
	
	*)
	err "Aborting! invalid placement \"$PLACEMENT\"."
esac

GEOM=${WIDTH}x${HEIGHT}+${HPOS}+${VPOS}

verbose 1 "plcment () initial placement is \"$GEOM\""
verbose 1 "plcment () base placement is \"$PLACEMENT\""

export ORIGHPOS="$HPOS" ORIGVPOS="$VPOS"
cascade
}

cascade () {
local panic i

verbose 1 "cascade () cascade mode is $MODE"
cntlogs

if [ $MODE = "--single" ]; then
	runterm
else
  case $PLACEMENT in
  		--b?|--?r)
  			reverse
  		;;

  		*)
  			# Do nothing
  esac

panic="0"
  for i in $LOGS; do
	GEOM=${WIDTH}x${HEIGHT}+${HPOS}+${VPOS}
	runterm
	if [ "$GEN" != "1" ]; then
		sleep 1
	fi
	case $MODE in
		--vcas)
		  case $PLACEMENT in
			--t?|--m?)
			VPOS=$(($VPOS+$WINV))
			if [ $(( $VPOS > (($VRES-$WINV)) )) = 1 ]; then
				VPOS=$ORIGVPOS
				HPOS=$(($HPOS+$WINH))
			fi

			if [ $(( $HPOS > (($HRES-$WINH)) )) = 1 ]; then
				panic="1"
			fi
			;;

			--b?)
			VPOS=$(($VPOS-$WINV))
			if [ $(( 0 > $VPOS )) = 1 ]; then
				VPOS=$ORIGVPOS
				HPOS=$(($HPOS+$WINH))
			fi

			if [ $(( $HPOS > (($HRES-$WINH)) )) = 1 ]; then
				panic="1"
			fi
		  esac
		;;
		
		--hcas)
		  case $PLACEMENT in
			--?l|--?c)
			HPOS=$(($HPOS+$WINH))
			if [ $(( $HPOS > (($HRES-$WINH)) )) = 1 ]; then
				HPOS=$ORIGHPOS
				VPOS=$(($VPOS+$WINV))
			fi
		
			if [ $(( $VPOS > (($VRES-$WINH)) )) = 1 ]; then
				panic="1"
			fi
			;;

			--?r)
			HPOS=$(($HPOS-$WINH))
			if [ $(( 0 > $HPOS )) = 1 ]; then
				HPOS=$ORIGHPOS
				VPOS=$(($VPOS+$WINV))
			fi

			if [ $(( VPOS > (($VRES-$WINV)) )) = 1 ]; then
				panic="1"
			fi
		  esac
	esac
	
	if [ $panic = 1 ]; then
	  GEOM=${WIDTH}x${HEIGHT}+${HPOS}+${VPOS}
	 err -n "Aborting! no more space left on screen in $MODE $PLACEMENT"
	 # err -n "Bug here? Testing: Eterm -g $GEOM" 
	  break
	fi 
  done

  if [ $panic = 1 ]; then
  trapids
  fi
fi
}

authenticate () {
local r

r="0"
while [ `whoami` != root ]; do 

 if [ "$DBG" != "1" -a "$DBG2" != "1" -a "$DBG3" != "1" ]; then
	clear
 fi

 if [ $r = 3 ]; then
	break
 fi

 out "\n$(uname -n) ($(uname -srm))"
 out "\nTo monitor these selected system logs:\n"

 for i in $LOGS; do
	out "$i"
 done

 out "\nPlease enter your system root password.\n"
 export -f verbose err out chkprog cntlogs reverse trapids runterm plcment cascade loctcfg
 export LOGS GEOM VERSION ETERM TAIL PCONFPATH DBG DBG2 DBG3 GEN DTOP HEIGHT WIDTH VOFFSET HOFFSET PLACEMENT MODE FONT COLOR SHADE CMOD CMODR CMODG CMODB TINT COLORTAIL WAIT

 su -s /bin/sh -c plcment && break
 r=$(($r + 1))
done
}

sysinfo () {
verbose 1 "sysinfo () Paralogger $VERSION"
verbose 2 "sysinfo () `uname -rsm` on `uname -n`"
verbose 2 "sysinfo () `getopt --version`"
verbose 2 "sysinfo () $SHELL $BASH_VERSION"
}

#--------------------------------------------------------------------------
# M a i n 
#--------------------------------------------------------------------------

default
cparser "$@"
sysinfo
amisane
termver
chktail
logxist
fntxist
getreso
winsize
saveconf
haccess

# EOF
