#!/bin/bash -x
# Usage:  job.sh -r'resume' -n -d'html directory' file file ...
#
# Script to move job postings from /ls120/*.htm (on a dos 
# floppy) to '~/html/[date]/[posting no.].html', then print out
# two copies of the appropriate resume with the posting number 
# embedded in the top.  The resume is assumed to be in postscript,
# and is run through the ghostscript interpreter as part of the
# printing process.  The default resume is '/home/lane/axhome/
# comp.resume.new.aw.ps'.
#
# First, we define a function that we need later.  This is 
# not a terribly complex function, and we could have inlined
# the code, but we can define functions, so we make the 
# code that comes later neater to read.
#
function edit_res 
{
	file=$1
	defaultres="/home/lane/axhome/comp.resume.new.aw.ps"
	name=$(grep 'Posting No' $file|cut -f6 -d'<'|cut -f2 -d' ')
	title=$(sed -n '/Job Title/p' $file|sed 's/.*: \([^<]*\)<.*/\1/') 
	dept=$(sed -n '/<STRONG>Department/p' $file|sed 's/.*: \([^<]*\)<.*/\1/')
	if grep "$name" html/applied; then
		echo "Already applied for posting $name" >> html/week.$thedate
	else
		echo "$name:$dept:$title" >> html/week.$thedate 
		echo "$name:$dept:$title" >> html/applied
		mv $file $datedir/$name.html
		if [ -z $resume ]; then
			sed "s!T-97-5555-JL!$name!" $defaultres > $HOME/tmp/$name.ps
		elif [ -n $resume ]; then
			sed "s!POSTING_NO!$name!" $resume > $HOME/tmp/$name.ps
		elif [ -n $plain ]; then
			sed "s!POSTING_NO!$name!" $resume|lpr
		elif [ -n $noprint ]; then
			echo "$name would have been printed"
		fi
	fi
}
#
# Next, we process the command line and fetch the options and
# arguements.  After that, we set up any directories that
# we need.
#
# declare -f is a debugging statement that forces bash to list
# out all of the functions that are currently declared.  At
# the moment, this will only be edit_res.  It is possible to 
# declare functions as part of a .bashrc or .bash_profile, and
# have them available as though the function was a command.
#
declare -f
while getopts ":r:d:pn" opt; do
	case $opt in
		r ) resume=$OPTARG ;;
		d ) dir=$OPTARG ;;
		p ) plain='p' ;;
		n ) noprint='n' ;;
		\? ) echo "Processing files in ls120 drive" ;;
	esac
done
#
# The next command just gets the options and their arguements
# out of the way.
#
shift $((OPTIND - 1))
#
# Now we attempt to mount the ls120 drive if it is not mounted
# and if $dir is null.
#
if [ -z $dir ]; then
	if ! ls /ls120; then
	#
	# If ls /ls120 fails, then the ls120 drive is not
	# mounted, and we need to mount it and tell
	# ourselves that we did so.
	#
		if mount /dev/hdc; then
			mountflag=1
		else
			echo "Mount failed, terminating"
			exit 1
		fi
	else
		echo "ls120 drive already mounted"
	fi
fi
thedate=$(date '+%d%b%y')
htmldir="$HOME/html"
datedir="$htmldir/$thedate"
if [ -n $dir ] && [ $dir != '/ls120' ]; then
	mkdir $dir
fi
if [ ! -d $datedir ]; then
	mkdir $datedir
fi
for i in $(ls $dir/*.htm); do
	edit_res $i
done
if [ -n "$*" ]; then
	for i in "$@"; do
		edit_res $i
	done
fi
#
# Clean-up.  First, we umount the /ls120 drive, if we mounted
# it in the first place.
#
if [ -n $mountflag ]; then
#	umount /dev/hdc
	echo "/dev/hdc would be umounted"
fi
# 
# ghostscript is really tempermental, and does not 
# like to be run from within a script.  It very much wants to
# be the shell, and just drops into the background and sulks
# if it is more than one shell script deep.  For this reason,
# we stack up the edited resumes in a temporary directory
# ('$HOME/tmp') and feed them directly to lpr in one
# big lump.  The non-default resumes, we fed straight to
# lpr.
lpr -Pps -#2 $HOME/tmp/*.ps
#rm $HOME/tmp/*.ps