#!/bin/sh
# http://www.berklix.com/~jhs/bin/.sh/bell
# Linked from bell & bell_init

# Bugs:
#	It fails to start an xbiff if a $flag exists, 
#	so after reboot always rm ~/tmp/.bell*
# 	Paths of subscripts are rooted, as bell gets called after ports makes,
#	where I have previously run the csh command: source `which unsetenv.csh`

# JJLATER maybe /usr/ports/mail/hbiff/ /usr/ports/mail/xlbiff


# Use: append after a make command or other long running command,
# to tell human to come look at the errors, fix & restart make again.
# eg:	cd /usr/ports/category ; make BERKLIX_CLIENT=YES ; bell

hostname=`hostname -s`

jhome=$HOME
#	Caution HOME gets unset by unsetenv.csh
jhome=/home/jhs

path=$jhome/bin/.sh/
#  I was wondering if I might only selectively prepend path,
#  but when bell is called from csh it always has a path.
#	dira=`dirname $0`
#	dirb=`dirname $0`
#	base=`basename $0`
#	echo "- 0 $0 - dira $dira - dirb $dirb - base $base -"
#	if [ -n $dira ]; then
#		echo With prefix path
#	else
#		echo No path
#	fi

flag=$jhome/tmp/.bell.$hostname.xbiff
pid=~/tmp/.bell.$hostname.pid
	# The dot prefix is so the file is not so easily deleted
	# accidentaly by rm ~/tmp/*
	# $hostname appended in case some host may have an NFS
	# shared ~ & thus also shared ~/tmp.
# dbg=echo
dbg=true
if [ "`basename $0`" = bell ]; then	#{{
	if [ -e $flag ]; then	#{{
		$dbg "OK will append to existing $flag"
		date -u +%Y-%m-%dT%H:%M:%SZ >> $flag
	else	#}{
		$dbg "Calling $path/bell_init to create $flag"
		if ! $path/bell_init ; then      #{{
			echo "$path/bell_init Failed "
			exit 1
			echo "Exit failed"
		else    #}{
			echo "$path/bell_init Succeeded "
			$path/bell
			exit $?
		fi      #}}
	fi	#}}
else	#}{
	$dbg "OK will initialise an xbiff window."
	namel="`echo $1|dd conv=lcase`" 1>/dev/null 2>&1

	number=`/site/usr/local/bin/host2number $namel`
		# As per xtermv & consolev:
		#   height=41 # Keep this same as 
		#	~/bin/.csh/consolev & 
		#	~/bin/.sh/xtermv
		#   pixels=`echo "${number} ${height} * p" | dc`

	goodstuff=140	# Measured, pretty much correct,
			# tune when host=blak is up.
	width=30	# Just a rough guess for now.
			# tune when host=wind is up,
			# to leave 1 slot for host=test.
	pixels=`echo "${number} ${width} * ${goodstuff} + p" | dc`

	if [ -e $flag ]; then	#{{
		echo "Warning: pre existant $flag"
		ls -l $flag
		$dbg "Removing old $flag"
		rm -f $flag	# Avoid log growing ever bigger
				# One day I may want to swap this for
				#	cp /dev/null $flag
				# if I do some fancy sym links between
				# hosts, running a compile ending in a make
				# on one host, & an xbiff running on another.
	fi	#}}
	touch $flag	# Creates if necessary
	$dbg "RESULT $? XX"
	if ! touch $flag ; then	#{{
		echo "Failed to touch $flag"
		# Maybe unwritable, modes or wrong owner.
		#	HERE
		exit 1	# Else it will loop.
		echo "Exit failed"
	else	#}{
		$dbg "Succeeded to touch $flag"
	fi	#}}

	$dbg "Ring a bell."  
	# echo -n "" # There is a literal Control G in quotes in this line.
	printf "\\a"	# Cleaner, less chance of edit damage.
	# xbiff also rings a bell, one hears them close adjacent,
	# so may be useful as knowing the cause, ie which task/machine ringing
	# Other reason for a seperate bell is to get at least one bell, as
	#  during upgrades xbiff fails to ring a bell, with error:
	#  "Shared object "libXext.so.6" not found, required by "xbiff"

	$dbg "Starting xbiff" 
	xbiff -file $flag -update 1     \
		-bg `/site/usr/local/bin/host2colour $hostname` \
		-geometry 48x48+$pixels-0       \
 -xrm 'xbiff*emptyPixmap:       /usr/local/include/X11/bitmaps/mailempty' \
 -xrm 'xbiff*fullPixmap:        /usr/local/include/X11/bitmaps/mailfull' \
 -xrm 'xbiff*fullPixmapMask:	/usr/local/include/X11/bitmaps/mailfullmsk' \
 -xrm 'xbiff*emptyPixmapMask:	/usr/local/include/X11/bitmaps/mailemptymsk' \
		&
		# On base line, to right of fvwm goodstuff 
		# (which is in lower left corner), stepped across, 
		# partly overlaying as not enough space.  
		#   -geometry 48x48+$pixels-0 
		# On base line. Just to left of exmh xbiff: 
		#   -geometry 48x48+865-0 
		# On base line, Just to right off fvwm goodstuff 
		#   -geometry 48x48+142-0 
		# On left vertical, As per xtermv & consolev: 
		#   -geometry 48x48+0+$pixels
	$dbg "xbiff PID is $!"
	echo $! > $pid

fi	#}}
exit 0

# List Of Icons:
# The icons (looking like USA aluminium long mail boxes that could take a
# newspaper) called at run time by xbiff I haven't found.

# /usr/ports/x11/xbiff/work/xbiff-1.0.2/bitmaps/mail-down       Empty Square
# /usr/ports/x11/xbiff/work/xbiff-1.0.2/bitmaps/mail-up         3 Envelopes

# /usr/local/include/X11/bitmaps/mailempty                      2 Desk tray.
# /usr/local/include/X11/bitmaps/mailfull                       In Top Desk tray

# /usr/local/lib/exmh-2.7.2/bitmaps/getmail.xbm	    Box in door (no 2nd icon)

# JJLATER hack up a set of make icons,
# but I''ve forgotten name of x bitmap editor.
