/*
 * PageOnCall
 *
 * Copyright  1993 by Christopher A. Wichura (caw@miroc.chi.il.us)
 * All rights reserved.
 *
 * This script will read the pager:schedule file to try and determine
 * who is on call.  It will then call SpoolPage to send the page text
 * to the person or persons indicated.
 *
 * If URGENT is the first word on the command line, PageOnCall will operate
 * in urgent mode.  This means it will continue looking through multiple
 * schedules until it can satisfy the request.  Thus, it is possible
 * have a 'normal duty' schedule and an 'emergency duty' schedule in
 * the same schedule file.  When the URGENT switch is used, SpoolPage
 * will also be started in URGENT mode.
 *
 * The text of the message to be sent will be read from stdin if it is not
 * provided on the command line.
 */

scheduleFile = 'PAGER:schedule'

/*
 * No general user modifiable code is found below this point.
 * Make changes here at your own risk.
 */

/*
 * First we want to figure out what the date and time is so we know
 * where to look in the schedule file.
 */

weekDay = date('Weekday')
select
	when upper(left(weekDay, 3)) = 'SUN' then weekDay = 0
	when upper(left(weekDay, 3)) = 'MON' then weekDay = 1
	when upper(left(weekDay, 3)) = 'TUE' then weekDay = 2
	when upper(left(weekDay, 3)) = 'WED' then weekDay = 3
	when upper(left(weekDay, 3)) = 'THU' then weekDay = 4
	when upper(left(weekDay, 3)) = 'FRI' then weekDay = 5
	when upper(left(weekDay, 3)) = 'SAT' then weekDay = 6
end

halfHour = time('Minutes') % 30

/*
 * Look at the command line we got and see if we are in urgent mode
 * or if we have the page text specified, etc.
 */

parse arg pageText

if upper(word(pageText, 1)) = 'URGENT' then do
	urgentMode = TRUE
	parse var pageText . pageText
end
else
	urgentMode = FALSE

pageText = strip(pageText)

/*
 * Now open the schedule file and start looking for a match.
 */

if open(fh, scheduleFile, 'R') then do
	want = weekDay
	exitVal = 0

	do until eof(fh)
		line = readln(fh)

		if left(line, length(want)) = want then do
			if want = weekDay then do
				who = substr(line, halfHour + 2, 1)

				if (who ~= '' & who ~= '-') then
					want = who'='
				else if urgentMode = FALSE then
					leave
			end
			else do
				exitVal = doPage(substr(line, 3), urgentMode, pageText)
				want = ''
				leave
			end
		end
	end

	call close(fh)

	if want = weekDay then do
		say 'No one scheduled at this time.  Message not sent.'
		exitVal = 1
	end
	else if want ~= '' then do
		say 'Couldn''t find '''want''' target list identifier.  Message not sent.'
		exitVal = 2
	end
end
else do
	say 'Couldn''t open' scheduleFile'.'
	exitVal = 5
end

exit exitVal

/*
 * The doPage procedure takes a list of comma separated names and
 * builds the command line to call SpoolPage with.
 */

doPage: procedure
	theCommand = 'spoolpage'

	targetList = arg(1)

	if targetList = '' then do
		say 'Empty target list found.  Message not sent.'
		return 3
	end

	do while targetList ~= ''
		index = pos(',', targetList)

		if index > 0 then do
			target = left(targetList, index - 1)
			targetList = substr(targetList, index + 1)
		end
		else do
			target = targetList
			targetList = ''
		end

		theCommand = theCommand '"'target'"'
	end

	if arg(2) = TRUE then
		theCommand = theCommand 'URGENT'

	if arg(3) ~= '' then
		theCommand = theCommand 'MESSAGE' arg(3)

	address command theCommand

	if rc ~= 0 then
		return 4
	else
		return 0
