#!/bin/sh -

nlogins=7
altfile=/var/adm/wtmpx.prev

# worked - report cumulative login time
# Steve Kinzler, steve@kinzler.com, Oct 93/May 95
# https://kinzler.com/me/home.html#unix

cons=; file=; bad=

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-[0-9]*)	nlogins=`echo "$1" | sed 's/.//'`;;
		-c)		cons=t;;
		-f)		file="-f $2"; shift;;
		-F)		file="-f $altfile";;

		--)		shift; break;;
		-h)		bad=t; break;;
		-*)		bad=t; echo "$0: unknown option ($1)" 1>&2;;
		*)		break;;
		esac
		shift;;
	esac
done

case "$#,$bad" in
[01],)	;;
*)	cat << EOF 1>&2
usage: $0 [ -0-9... ] [ -c ] [ -f file ] [ -F ] [ regexp ]
	-#	consider the last # number of logins (default $nlogins)
	-c	include console logins
	-f	read last logins from the given file (default as with last(1))
	-F	read last logins from the alternate file, $altfile
The total time is accumulated from lines matching a given regexp,
default today's weekday abbreviation.
EOF
	exit 1;;
esac

date=`date`
echo "$date"

tmpA=/tmp/wkdA$$
tmpB=/tmp/wkdB$$
trap "rm -f $tmpA $tmpB; exit" 0 1 13 15
trap continue 2

last -"$nlogins" $file ${USER-$LOGNAME} |
case "$cons" in
?*)	cat;;
*)	grep -v console;;
esac > $tmpA
cat $tmpA
echo

case $# in
0)	set $date .;;
esac

grep -i "$1" < $tmpA > $tmpB

(set `echo "$date" | sed 's/.*\([0-9][0-9]\):\([0-9][0-9]\):.*/\1 \2/'`

 sed -n 's/.*\([0-9][0-9]\):\([0-9][0-9]\)  *still.*/\1 \2/p' $tmpB |
 awk '{ print ('"$1"'-$1+24)%24, '"$2"'-$2 }'

 sed -n 's/.*(\([0-9][0-9]\):\([0-9][0-9]\))/\1 \2/p' $tmpB) |

awk ' { hr += $1; min += $2 }
  END { printf "worked %d:%02d on '"$1"'\n", int(hr+min/60), (min+60)%60 }'
