#!/bin/sh

### Bourne shell script to print out an excuse from the BOFH excuse file.
### Suitable for login messages, email signatures, etc. Knock yourself out.
###
###   usage: bofh.sh [ excuse file ]
###
### If an excuse file isn't specified on the command line, it looked for
### "excuses.txt" in the current directory.  But look, I really think you
### can figure this out for yourself.
###
### The original BOFH Excuse Server homepage:
###  . http://www.cs.wisc.edu/~ballard/bofh/
###
### Where this file came from:
###  . http://yoyo.org/~pgl/bofh/
###
###
### - Tue Jun 25 20:43:06 BST 2002
###   Peter Lowe, pgl@instinct.org
###

# default excuse file to look for
defaultexcusefile='./excuses.txt'

# check for the excuse file
if [ $1 ]
then
	if [ ! -f $1 ]
	then
		# duh
		echo "Can't find $1!"
		exit 1;
	else
		excusefile=$1
	fi
elif [ -f $defaultexcusefile ]
then
	excusefile=$defaultexcusefile
else
	# is this _really_ necessary?
	me=$(basename $0)
	cat <<-EOUSAGE
	Usage: $me [ excuse file ]

	If an excuse file is not specified on the command line, it is assumed
	to be $defaultexcusefile.
	EOUSAGE

	exit 1
fi

# get the number of excuses as a maximum value to use for the random number
numexcuses=$(wc -l $excusefile)
numexcuses=${numexcuses%% $excusefile}

# I suppose this may be a bit over the top
[ $numexcuses ] || {
	cat <<-EOERR
	Erm. Seems something went wrong. Does the file "$excusefile" _really_ have
	0 lines?
	EOERR
	exit 1
	}

# terrible, but it'll do (famous last words)
randline=$(($(date +%M%S) * $$ % $numexcuses))

# print out the excuse
head -n $randline "$excusefile" | tail -1