#! /bin/csh -f
#Resource scheduling system- see README.
#pvelleca Rev C 10/26/94 

#SET UP THE PROPER ENVIRONMENT
source ./.schedrc

#THE NEXT (2) VARIABLES REPRESENT THE NUMBER OF RESERVABLE TIME SLOTS PER
#DAY, WHICH IS THE NUMBER OF LINES IN THE FILE '$template'. 
set slots_per_day = `wc -l < $template`
#CREATE A VARIABLE ARRAY WHOSE NUMBER OF ELEMENTS IS THE NUMBER OF SLOTS
#PER DAY.
set time_slots = ( `awk '{print NR}' $template` )

#CURSOR AND DISPLAY CONTROL CODES ARE USED IN THIS SCRIPT FOR
#CONTROLLING THE LOOK OF THE MENUS.  IF THESE ARE NOT WANTED,
#THEN SET TERM (IN .schedrc) TO SOMETHING OTHER THAN "VT100",
#OR COMMENT/DELETE THEM:
# Esc[nA -> "^[[nA" MOVE CURSOR UP n LINES.
# Esc[2K -> "^[[2K" ERASE CURRENT LINE.
# Esc[1m -> "^[[1m" BOLD ON.
# Esc[22m ->"^[[22m" BOLD OFF.
if ( $TERM == "vt100" ) then
	set up1 =	"[1A"
	set up2 =	"[2A"
	set up3 =	"[3A"
	set erase =	"[2K"
	set boldON =	"[1m"
	set boldOFF =	"[22m"
else
	set up1 =	""
	set up2 =	""
	set up3 =	""
	set erase =	""
	set boldON =	""
	set boldOFF =	""
endif

goto INITIALIZE
AFTER_INITIALIZE:

MAIN_MENU:
clear
echo "Status:  PS[${psstate}]  PR[${printer}]  RPT[${reporttype}]" 
echo " "
echo "MAIN MENU"
echo "==== ===="
echo " "
echo "1	Add ${restype}"     
echo "2	Delete ${restype}"
echo "3	List ${restype}"
echo " "
echo "4	Reserve ${restype}"
echo "5	Delete Reservation"
echo " "
echo "6	Printing"
echo " "
echo "0	Exit"
echo ""
echo -n "CHOICE?  "
set option = $< 
set option = `expr "$option" : '\(.\)'`

switch ( $option )
case "":
case 0:
case m:
case q:
case x:
case e:
  exit 0
  breaksw


#ADD RESOURCE TO THE LIST OF RESOURCES, CONTAINED IN THE FILE '$resoucelist'.
case 1:
  echo -n "Name of ${restype}(S) to add: "
  set resources = $<
  if (`expr "${resources}" : '.*'` == 0 ) then
    goto MAIN_MENU
  endif
  foreach resource ( ${resources} )
    if ( `fgrep -xc ${resource} ${resourcelist}` == 0 ) then
      expr "${resource}" : '\(...............\)' \| "${resource}">>${resourcelist}
    else
      echo "${restype} ${resource} already exists."
    endif
  end
  sort ${resourcelist} > ${resourcelist}.tmp
  mv ${resourcelist}.tmp ${resourcelist}
  echo "Add complete..."
  $<
  breaksw



#DELETE Resource FROM resourcelist. 
case 2:
  echo -n "Name of ${restype}(S) to delete: "
  set resources = $<
  if (`expr "${resources}" : '.*'` == 0 ) then
    goto MAIN_MENU
  endif
  if ( $resources == 'ALL' ) then
    echo -n "You want to delete ALL ${restype}S and their reservations? "
    set response = $<
    if ( `expr "$response" : '\(.\)'` =~ [yY] ) then
      rm -rf ${resv}/* >& /dev/null
      cp /dev/null ${resourcelist} >& /dev/null 
    endif
  else
     foreach resource ( ${resources} )
       if ( `fgrep -xc ${resource} ${resourcelist}` == 0 ) then
         echo "${restype} ${resource} does not exist."
       else
         #REMOVE resource FROM resourcelist FILE.
         fgrep -xv ${resource} ${resourcelist} > ${resourcelist}.tmp
         mv ${resourcelist}.tmp ${resourcelist}
         #REMOVE ANY RESERVATIONS FOR THAT RESOURCE.
         find $resv -name ${resource} -print|xargs rm >& /dev/null
         echo "${restype} ${resource} and reservations deleted."
       endif
     end
  endif
  echo -n "Delete complete..."
  $<
  breaksw



#SHOW  RESOURCE LIST TO SCREEN.
case 3:
  clear
  echo CURRENT ${restype} LIST
  echo " "
  cat ${resourcelist}|sort -b|pr -3n4t|more
  echo " "
  echo -n "${restype}S listed..."
  $<
  breaksw



#RESERVE A RESOURCE FOR A PERIOD OF TIME.
case 4:
  echo -n "$erase"
  GET_RESV_DATE:
  echo -n "$up1"
  echo -n "$erase" 
  set rd
  set trd
  echo -n "DATE of reservation?  "
  set mrd = $<
  if ( `expr "$mrd" : '.*'` == 0 || $mrd == 'm' ) then
    goto MAIN_MENU
  endif
  #CHECK FOR RANGE OF DATES.
  if ( `expr "$mrd" : '.*-'` > 0 ) then
    set rd = ($rd `expr "$mrd" : '\([0-9/]*\)-'`)
    set rd = ($rd `expr "$mrd" : '.*-\([0-9/]*\)'`)
    if ( $rd[1] == $rd[2] ) then
      set rd = $mrd
    endif
  else
    set rd = $mrd
  endif
  set trd = ($rd)

  #VERIFY EACH ENDPOINT IN RANGE OF DATES IS VALID
  set i = 1
  while ( $i <= $#trd )
    set trd[$i] = `echo $rd[$i]|sed s,\^0,,g|sed s,/0,/,g|cut -f1-2 -d'/'`
    if ( `fgrep -xc $trd[$i] $calendar` == 0 ) then
      goto GET_RESV_DATE
    endif
    @ i++
  end  
  set rd = ($trd)

  #GET EACH DAY IN THE RANGE OF DAYS AND PLACE IN AN ARRAY.
  if ( $#rd > 1 ) then
    set sday = `fgrep -xn $rd[1] $calendar|cut -f1 -d':'`
    set eday = `fgrep -xn $rd[2] $calendar|cut -f1 -d':'`
    @ numdays = 1 + $eday - $sday
    set listofdays = (`tail +$sday $calendar|head -$numdays`)
  else
    set sday = $rd
    set eday = $rd
    set numdays = 1
    set listofdays = $rd
  endif

  echo -n "$erase"
  GET_RESV_NAME:
  echo -n  "$up1"
  echo -n  "$erase"
  echo -n "NAME of reserver for ${boldON}$mrd${boldOFF}?  "
  set name = $<
  if ( $name == 'm' ) then
    goto MAIN_MENU
  endif
  #MAKE NAME 15 CHARACTERS OR LESS.
  set name = `expr "$name" : '\(...............\)' \| "$name"`
  if ( $name == 0 ) then
    goto GET_RESV_DATE
  endif

  echo -n "$erase"
  GET_RESV_HOST:
  echo -n "$up1"
  echo -n "$erase"
  echo -n "${restype} for ${boldON}$name${boldOFF} on ${boldON}$mrd${boldOFF}? "
  set resource = $<
  if ( ${resource} == 'm' ) then
    goto MAIN_MENU
  endif
  if ( `expr "${resource}" : '.*'` == 0 ) then
    echo -n "$erase"
    goto GET_RESV_NAME
  endif
  if ( `fgrep -xc ${resource} ${resourcelist}` == 0 ) then
    echo "No such ${restype}:  ${boldON}${resource}${boldOFF}."
    echo -n "$up1"
    goto GET_RESV_HOST 
  endif

  echo -n "$erase"
  GET_RESV_TIME:
  echo -n "$up1"
  echo -n "$erase"
  echo -n "TIME on ${boldON}${resource}${boldOFF} for ${boldON}$name${boldOFF}"
  echo -n " on ${boldON}$mrd${boldOFF}?  "
  set rtime = $<
  if ( $rtime == 'm' ) then
    goto MAIN_MENU
  endif
  set rtimelen = `expr "$rtime" : '.*'`
  if ( $rtimelen == 0 ) then
    goto GET_RESV_HOST 
  endif
  set rtimelen = `expr "$rtime" : '^[0-2][0-9][0-9][0-9]-[0-2][0-9][0-9][0-9]$'`
  if ( $rtimelen == 0 ) then
    goto GET_RESV_TIME 
  endif

  #GET START TIME
  set stime = `expr "$rtime" : '\(....\)-....'`
  if ( `grep -c "${stime}""-" $template` != 1 ) then
    goto GET_RESV_TIME
  endif

  #GET END TIME
  set etime = `expr "$rtime" : '....-\(....\)'`
  if ( `grep -c "\-$etime" $template` != 1 ) then
    goto GET_RESV_TIME
  endif

  #ENSURE END TIME IS AFTER START TIME.
  if ( $etime <= $stime ) then
    goto GET_RESV_TIME
  endif

  #FOR EACH DAY IN LIST OF DAYS, ATTEMPT RESERVATION
  set lod = ($listofdays)
  set resv_good = 'TRUE'
  echo -n "Dates:  "
  while ( $#lod != 0 && $resv_good == 'TRUE' )
    #CREATE DAY DIRECTORY IF NOT ALREADY.
    set day = `echo $lod[1]|sed si/i.ig`
    if (! -e ${resv}/${day} ) then
      mkdir  ${resv}/${day}
    endif
    #ALIAS RESOURCE FILE TO SOMETHING SHORT AND EASY 
    set hfile = ${resv}/${day}/${resource}
    #SET UP A TIME-SLOT TEMPLATE FOR THAT RESOURCE ON THAT DAY IF NOT ALREADY.
    if (! -e ${hfile} ) then
      cp ${template} ${hfile}
    endif

    #READ DISK FILE INTO MEMORY FOR FASTER RESPONSE
    set l = ($time_slots)
    foreach ln ( $time_slots )
      #NOTE DOUBLE QUOTES NEEDED TO INSURE ELEMENT IS ONE WORD.
      #I.E. SIZE OF l[n] IS 1 ($#l[1] = 1).
      #GET LINE NUMBER 'ln' FROM FILE.
      set l[$ln] = "`tail +$ln $hfile|line`" 
    end 

    #GET STARTING TIME SLOT POSITION IN FILE.
    set startslot = `grep "${stime}-" $numtemp|cut -f1`
  
    #GET END TIME SLOT POSITION IN FILE.
    set endslot = `grep "\-$etime" $numtemp|cut -f1`

    #CHECK EACH TIME FOR EXISTING RESERVATION, IF NOT, RESERVE IT
    echo -n $day
    set ln = $startslot
    while ( $ln <= $endslot && $resv_good == 'TRUE' ) 
      #NOTE PARENTHESIS REQUIRED TO CREATE AN ARRAY IN SLOT.
      set slot = ($l[$ln])
      #IF SECOND COLUMN OF FILE IS NOT RESERVED (IS EMPTY)
      if ( $#slot == 1 ) then
        #SLOT IS NOT TAKEN, MAKE RESERVATION
        set l[$ln] = "${l[$ln]} ${name}"
      else 
        #SLOT IS TAKEN, CANT RESERVE BLOCK OF TIMES.
        #DONT WRITE MEMORY BACK TO DISK FILE-KEEP FILE AS IS.
        set resv_good = 'FALSE'
        echo " "
        echo -n "RESERVE CANCELLED: ${boldON}${resource}${boldOFF} taken at "
        echo "${boldON}$slot[1]${boldOFF} by ${boldON}$slot[2]${boldOFF}."
      endif
      @ ln++
    end  
    shift lod

    #FINISHED WITH PART OF RESERVATION, HOLD RESV TEMP UNTIL SURE ALL ARE VALID.
    if ( $resv_good == 'TRUE' ) then
      if ( $#lod == 0 ) then
        echo -n "."
      else
        echo -n ", "
      endif
      #WRITE SCHEDULE FROM MEMORY FILE BACK TO TEMPORARY DISK FILE.
      set ln = 1
      foreach ln ( $time_slots ) 
        echo $l[$ln] >> ${hfile}.tmp
      end
    endif

  end

  #FINISHED WITH RESERVATION
  if ( $resv_good == 'TRUE' ) then
    foreach day ( $listofdays )
      set day = `echo $day|sed si/i.ig`
      #ALIAS RESOURCE FILE TO SOMETHING SHORT AND EASY
      set hfile = ${resv}/${day}/${resource}
      #WRITE ALL SCHEDULES FROM MEMORY FILE BACK TO DISK FILE.
      mv ${hfile}.tmp $hfile
    end
    echo " "
    echo "Reservation complete."
  endif

  echo " ";echo " "
  goto GET_RESV_TIME
  breaksw



#DELETE RESERVATION
case 5:
  GET_DEL_DATE:
  echo -n "$up1"
  echo -n "$erase"
  echo -n "DATE to delete reservation?  "
  set rd = $<
  if ( $rd == 'm' ) then
    goto MAIN_MENU
  endif
  if ( $rd == 'ALL' ) then
    echo -n "Are you sure you want to delete ALL current reservations? "
    set response = $<
    if ( `expr "$response" : '\(.\)'` =~ [yY] ) then
      rm -rf ${resv}/* >& /dev/null 
      goto MAIN_MENU
    endif
  endif
  set rd = `echo $rd|sed s/\^0//g|sed s,/0,/,g|cut -f1-2 -d'/'`
  if ( `expr "$rd" : '.*'` == 0 ) then
    goto MAIN_MENU
  endif
  if ( `fgrep -xc $rd $calendar` == 0 ) then
    goto GET_DEL_DATE
  endif
  set day = `echo $rd|sed s,/,.,g`
  if (! -e ${resv}/${day} ) then
    echo "No reservations made for that day."
    echo -n "$up1"
    goto GET_DEL_DATE
  endif

  echo -n "$erase"
  GET_DEL_HOST:
  echo -n "$up1"
  echo -n "$erase"
  echo -n "${restype} reservation was for on ${boldON}$rd${boldOFF}?  "
  set resource = $<
  if ( ${resource} == 'm' ) then
    goto MAIN_MENU
  endif
  if ( `expr "${resource}" : '.*'` == 0 ) then
    echo -n "$erase"
    goto GET_DEL_DATE 
  endif
  if ( `fgrep -xc ${resource} ${resourcelist}` == 0 ) then
    echo "No such ${restype}: ${boldON}${resource}${boldOFF}."
    echo -n "$up1"
    goto GET_DEL_HOST 
  endif
  if (! -e ${resv}/${day}/${resource} ) then
    echo -n "No reservations for ${boldON}${resource}${boldOFF} "
    echo "on ${boldON}$day${boldOFF}."
    echo -n "$up1"
    goto GET_DEL_HOST 
  endif

  echo -n "$erase"
  GET_DEL_TIME:
  echo -n "$up1"
  echo -n "$erase"
  echo -n "TIME to delete for ${boldON}${resource}${boldOFF} "
  echo -n "on ${boldON}$rd${boldOFF}?  "
  set rtime = $<
  if ( $rtime == 'm' ) then
    goto MAIN_MENU
  endif
  set rtimelen = `expr "$rtime" : '.*'`
  if ( $rtimelen == 0 ) then
    echo -n "$erase"
    goto GET_DEL_HOST
  endif
  set rtimelen = `expr "$rtime" : '^[0-2][0-9][0-9][0-9]-[0-2][0-9][0-9][0-9]$'`
  if ( $rtimelen == 0 ) then
    echo -n "$erase"
    goto GET_DEL_TIME
  endif

  #SET START TIME
  set stime = `expr "$rtime" : '\(....\)-....'`
  if ( `grep -c "${stime}-" $template` != 1 ) then
    goto GET_DEL_TIME
  endif

  #SET END TIME
  set etime = `expr "$rtime" : '....-\(....\)'`
  if ( `grep -c "\-$etime" $template` != 1 ) then
    goto GET_DEL_TIME
  endif

  #ENSURE END TIME IS AFTER START TIME.
  if ( $etime <= $stime ) then
    goto GET_RESV_TIME
  endif

  #RENAME RESOURCE FILE TO SOMETHING SHORT AND EASY TO TYPE
  set hfile = ${resv}/${day}/${resource}

  #READ DISK FILE INTO MEMORY FOR FASTER RESPONSE
  set l = ($time_slots)
  set ln = 1
  while ( $ln <= $slots_per_day)
    #NOTE DOUBLE QUOTES NEEDED TO INSURE ELEMENT IS ONE WORD.
    set l[$ln] = "`tail +$ln $hfile|line`"
    @ ln++
  end

  #GET START TIME SLOT POSTIION.
  set startslot = `nl $hfile|grep "${stime}-"|awk '{print $1}'`

  #GET END TIME SLOT POSTIION.
  set endslot = `nl $hfile|grep "\-$etime"|awk '{print $1}'`

  #DELETE RESERVEE FROM EACH TIME SLOT
  set ln = $startslot
  while ( $ln <= $endslot )
    #NOTE PARENTHESIS TO CREATE AN ARRAY IN SLOT.
    set slot = ($l[$ln])
    #SET TIME SLOT TO ONLY CONTAIN TIME, NO RESERVEE.
    set l[$ln] = "$slot[1]"
    @ ln++
  end

  #FINISHED WITH RESERVATION DELETION
  #WRITE SCHEDULE FROM MEMORY FILE BACK TO DISK FILE.
  rm -f $hfile
  set ln = 1
  while ( $ln <= $slots_per_day )
    echo $l[$ln] >> $hfile
    @ ln++
  end
  echo "Reservation deletion complete."

  echo " ";echo " "
  goto GET_DEL_TIME
  breaksw

  
 
#PRINT REPORTS
case 6:
  PRINT_MENU:
  clear
  echo "Status:  PS[${psstate}]  PR[${printer}]  RPT[${reporttype}]" 
  echo " "
  echo "PRINTING MENU"
  echo "======== ===="
  echo " "
  echo "  ONE DAY REPORT"
  echo "  1	All ${restype}S"
  echo "  2	One ${restype}"
  echo " "
  echo "  ONE WEEK REPORT"
  echo "  3	All ${restype}S" 
  echo "  4	One ${restype}"
  echo " "
  echo "  ALL RESERVATIONS"
  echo "  5     All ${restype}S"
  echo "  6	One ${restype}"
  echo " "
  echo "  7     Set Printing Defaults"
  echo " "
  echo "  0	Exit Printing Menu"
  echo "  q     Quit Program"
  echo ""
  echo -n "CHOICE?  "
  set option = $<
  set option = `expr "$option" : '\(.\)'`
  echo " "
  echo -n "$up1"

  switch ( $option )
  case "":
  case 0:
  case m:
    goto MAIN_MENU
    breaksw
  case q:
  case e:
  case x:
    exit 0
    breaksw

  #PRINT SCHEDULE FOR ALL RESOURCE
  case 1:
  case 3:
    echo -n "$erase"
    GET_PSDATE:
    echo -n "$up1"
    echo -n "$erase"
    echo -n "DATE to print? "
    set rd = $<
    set rd = `echo $rd|sed s,\^0,,g|sed s,/0,/,g|cut -f1-2 -d'/'`
    if ( `expr "$rd" : '.*'` == 0 ) then
      echo -n "$erase"
      goto PRINT_MENU
    endif
    if ( $rd =~ [tT] || $rd == 'today' ) then
      set rd = $today
    endif
    if ( `fgrep -xc $rd $calendar` == 0 ) then
      echo -n "$up1"
      goto GET_PSDATE
    endif

    #GET LIST OF DAYS TO PRINT
    if ($option == '3' ) then
      set RETURN = 'RETURN_GET_LIST_PS'
      goto GET_LIST
      RETURN_GET_LIST_PS:
      set header = "SCHEDULE FOR WEEK OF ${listofdays[1]}-${listofdays[7]} "
      set header = "$header  ALL ${restype}S."
      set fn = "week_of_${listofdays[1]}_all_${restype}s"
    else
      set listofdays = `echo $rd|sed "s,/,\.,g"`
      set header = "SCHEDULE FOR DAY OF $listofdays FOR ALL ${restype}S."
      set fn = "day_of_${listofdays}_all_${restype}s"
    endif

    #GET LIST OF RESOURCES.
    alias listhosts 'ls $resv/$day'

    #CREATE REPORT
    set RETURN = "RETURN_CR_REP_PSDATE"
    goto CREATE_REPORT
    RETURN_CR_REP_PSDATE:


    #PRINT REPORT
    set RETURN = "RETURN_PRINT_PSDATE"
    goto PRINT_FILE
    RETURN_PRINT_PSDATE: 
    breaksw
 

  #PRINT FOR ONE RESOURCE
  case 2:
  case 4:
    echo -n "$erase"
    GET_PSOHHOST:
    echo -n "$up1"
    echo -n "$erase"
    echo -n "${restype} to print? "
    set resource = $<
    if ( `expr "${resource}" : '.*'` == 0 ) then
      goto PRINT_MENU
    endif
    if ( `fgrep -xc ${resource} ${resourcelist}` == 0 ) then
      echo "No such ${restype}."
      echo -n "$up1"
      goto GET_PSOHHOST
    endif

    echo -n "$erase"
    GET_PSOHDATE:
    echo -n "$up1"
    echo -n "$erase"
    echo -n "DATE to print ${boldON}${resource}${boldOFF}? "
    set rd = $<
    if ( $rd == 'm' ) then
      goto PRINT_MENU
    endif
    set rd = `echo $rd|sed s,\^0,,g|sed s,/0,/,g|cut -f1-2 -d'/'`
    if ( `expr "$rd" : '.*'` == 0 ) then
      goto GET_PSOHHOST 
    endif
    if ( $rd =~ [tT] || $rd == 'today' ) then
      set rd = $today
    endif
    if ( `fgrep -xc $rd $calendar` == 0 ) then
      goto GET_PSOHDATE 
    endif
    
    if ($option == '4' ) then
      #GET DAYS OF THE WEEK THAT CONTAIN THE REQUESTED DATE.
      set RETURN = 'RETURN_GET_LIST_PSOH'
      goto GET_LIST
      RETURN_GET_LIST_PSOH:
      set header = "SCHEDULE WEEK OF ${listofdays[1]}-${listofdays[7]} "
      set header = "$header FOR ${resource}."
      set fn = "week_of_${listofdays[1]}_${resource}"
    else
      set listofdays = `echo $rd|sed "s,/,\.,g"`
      set header = "DATE OF $listofdays SCHEDULE FOR ${restype} ${resource}."
      set fn = "day_of_${listofdays}_${resource}"
    endif

    #GET LIST OF RESOURCES
    alias listhosts 'echo ${resource}'

    #CREATE REPORT
    set RETURN = "RETURN_CR_REP_PSOHDATE"
    goto CREATE_REPORT
    RETURN_CR_REP_PSOHDATE:

    #PRINT REPORT
    set RETURN = "RETURN_PRINT_PSOHDATE"
    goto PRINT_FILE
    RETURN_PRINT_PSOHDATE:
    goto GET_PSOHDATE
    breaksw


  #PRINT ALL CURRENTLY SCHEDULED
  case 5:
    #SORT DAYS IN CALENDAR (NOT NUMERIC) ORDER.
    #E.G. 3/11 COMES AFTER 3/2, NOT BEFORE.
    set listofdays
    cd $resv >& /dev/null
    foreach day ( 1 2 3 4 5 6 7 8 9 10 11 12 )
      set ld = ( `find . -name ${day}.\* -print|cut -f2 -d'/'` )
      set listofdays = ( $listofdays `echo $ld|sort -n -t. +1` )
    end
    cd .. >& /dev/null
    
    #SET DEFAULT FILENAME
    set fn = "all_${restype}_`date +%m.%d.%y`"
    set header = "SCHEDULE AS OF `date +%m/%d/%y` FOR ALL ${restype}S."
 
    #SET LIST OF RESOURCE
    alias listhosts 'ls ${resv}/$day'

    #CREATE REPORT
    set RETURN = "RETURN_CR_REP_ALL_SCHEDULED"
    goto CREATE_REPORT
    RETURN_CR_REP_ALL_SCHEDULED:

    #PRINT REPORT
    set RETURN = "RETURN_PRINT_ALL_SCHEDULED"
    goto PRINT_FILE
    RETURN_PRINT_ALL_SCHEDULED: 
    goto PRINT_MENU
    breaksw


  #PRINT ALL CURRENTLY SCHEDULED FOR ONE RESOURCE
  case 6:
    echo -n "$erase"
    GET_AOHOST:
    echo -n "$up1"
    echo -n "$erase"
    echo -n "${restype} to print? "
    set resource = $<
    if ( `expr "${resource}" : '.*'` == 0 ) then
      echo -n "$erase"
      goto PRINT_MENU
    endif
    if ( `fgrep -xc ${resource} ${resourcelist}` == 0 ) then
      echo "No such ${restype}."
      echo -n "$up1"
      goto GET_AOHOST
    endif
    #SORT DAYS IN CALENDAR (NOT NUMERIC) ORDER.
    set listofdays 
    cd $resv >& /dev/null
    foreach day ( 1 2 3 4 5 6 7 8 9 10 11 12 )
      set ld = ( `find . -name ${day}.\* -print|cut -f2 -d'/'` )
      set listofdays = ( $listofdays `echo $ld|sort -n -t. +1` )
    end
    cd .. >& /dev/null
    set fn = "${resource}_as_of_`date +%m.%d.%y`"
    set header = "SCHEDULE AS OF `date +%m/%d/%y` FOR ${resource}." 

    #GET LIST OF RESOURCE
    alias listhosts 'echo ${resource}'

    #CREATE REPORT
    set RETURN = "RETURN_CR_REP_ALL_SCHEDULED_OH"
    goto CREATE_REPORT
    RETURN_CR_REP_ALL_SCHEDULED_OH:

    #PRINT REPORT
    set RETURN = "RETURN_PRINT_ALL_SCHEDULED_OH"
    goto PRINT_FILE
    RETURN_PRINT_ALL_SCHEDULED_OH:
    goto PRINT_MENU
    breaksw



  #SET PRINTING DEFAULTS MENU
  case 7:
    DEFAULT_MENU:
    clear
    echo "PRINTING DEFAULTS MENU"
    echo "======== ======== ===="
    echo " "
    echo "  1  Postscript printing is:    $psstate"
    echo "  2  Default printer is:        $printer"
    echo "  3  Default report format is:  $reporttype"
    echo " "
    echo " "
    echo "  0  Exit Printing Defaults Menu"
    echo "  q  Quit Program"
    echo ""
    echo -n "CHOICE?  "
    set option = $<
    set option = `expr "$option" : '\(.\)'`
    echo " "
    echo -n "$up1"

    switch ( $option )
    case "":
    case 0:
    case m:
      goto PRINT_MENU
      breaksw
    case x:
    case q:
    case e:
      exit
      breaksw

    #TOGGLE POSTSCRIPT SETTING FOR PRINTING.
    case 1:
      if ( $psstate == 'ON' ) then
        set psstate = 'OFF'
        alias printcmd 'lpr -Jschedule -P$printer filetoprint'
      else
        set psstate = 'ON'
        alias printcmd 'a2ps filetoprint|lpr -Jschedule -P$printer'
      endif
      echo $psstate > $ps
      goto DEFAULT_MENU
      breaksw


    #CHANGE DEFAULT PRINTER
    case 2:
      SET_DEFAULT_PRINTER:
      set printer = `cat $printerfile`
      echo -n "Change default printer [${printer}]:  "
      set newprinter = $<
      if ( `expr "$newprinter" : '.*'` == 0 ) then
        set newprinter = $printer
      endif
      if ( `echo $newprinter|tr 'a-z' 'A-Z'` == 'NONE' ) then
        echo "NONE" > $printerfile
        set printer = "NONE"
        goto DEFAULT_MENU
      endif
      if ( `lpstat -p|grep -c $newprinter` == 0 ) then
        echo "No such printer: ${newprinter}."
        goto SET_DEFAULT_PRINTER
      endif
      echo $newprinter > $printerfile
      set printer = $newprinter
      goto DEFAULT_MENU
      breaksw


    #TOGGLE REPORT FORMAT
    case 3:
      if ( $reporttype == 'MATRIX' ) then
        set reporttype = 'FLAT'
      else
        set reporttype = 'MATRIX'
      endif
      echo $reporttype > $reportfile
      goto DEFAULT_MENU
      breaksw

    default:
      goto DEFAULT_MENU
      breaksw
    endsw
    goto DEFAULT_MENU


  default:
    goto PRINT_MENU
    breaksw
  endsw
  goto PRINT_MENU



default:
  goto MAIN_MENU
  breaksw
endsw
goto MAIN_MENU
exit 0



CREATE_REPORT:
#THIS SUBROUTINE REQUIRES THREE VARIABLES:
#header, listofdays, listhosts, WHICH ARE DEFINED BY THE CALLING 
#ROUTINE.
echo $header > filetoprint
if ( $reporttype == 'FLAT' ) then
  #CREATE FLAT REPORT
  foreach day ( $listofdays )
    echo "DATE ${day}  ==========================" >> filetoprint
    if ( ! -e ${resv}/$day ) then
      echo "Nothing scheduled." >> filetoprint
    else
      foreach resource ( `listhosts` )
        if ( ! -e ${resv}/${day}/${resource} ) then
          echo "Nothing scheduled." >> filetoprint
        else if ( `wc -c<${resv}/${day}/${resource}` > `wc -c<$template` ) then
          echo "${restype} ${resource}  ==========" >> filetoprint
          awk 'NF>1 {printf("%9s\t%-15.15s\t%-15.15s\n",$1,host,$2)}' \
          host=${resource} ${resv}/${day}/${resource} >> filetoprint
          echo "  " >> filetoprint
        else
          echo "Nothing scheduled." >> filetoprint
        endif
      end
    endif
    echo " " >> filetoprint
  end
else
  #CREATE MATRIX REPORT
  foreach day ( $listofdays )
    echo "DATE ${day}  ==========================" >> filetoprint
    if ( ! -e ${resv}/$day ) then
      echo "Nothing scheduled." >> filetoprint
    else
      #CREATE HEADER OF TIME SLOTS FOR EACH DAY ON SEPERATE PAGE.
      awk 'BEGIN {printf "RESOURCE  ||"} \
      {printf "%-6.4s|",$1 } \
      END {print ""}' $template >> filetoprint

      #MATRIX FORMAT:  resourcename field=  10 wide.
      #                time slot field=  6 wide, left justified in 5.
      awk 'BEGIN {printf "==========||"} \
      {printf "%s|",equal } \
      END {print ""}' equal="======" $template >> filetoprint

      foreach resource ( `listhosts` )
        #CREATE ROW FOR HOST UNDER HEADER.
        if ( ! -e ${resv}/${day}/${resource} ) then
          echo "Nothing scheduled." >> filetoprint
        else if ( `wc -c<${resv}/${day}/${resource}` > `wc -c<$template` ) then
          awk 'NR == 1 {printf "%-10.9s||",h} \
          {printf("%-6.5s|",$2)} \
          END {print ""}' h=${resource} $resv/$day/${resource} >> filetoprint

          awk 'NR==1 {printf "__________||"} \
          {printf("%s|",underline)} \
          END {print ""}' underline="______" $template >> filetoprint
        else
          echo "Nothing scheduled." >> filetoprint
        endif
      end
    endif 
    shift listofdays
    if ( $#listofdays > 0 && $reporttype == "MATRIX" ) then
      #FORM FEED, THIS NEXT CHAR IS ACTUALLY octal 014
      echo "" >> filetoprint 
    else
      echo " ";echo " " >> filetoprint
    endif
  end
  echo " " >> filetoprint
endif
echo "END OF SCHEDULE." >> filetoprint
goto $RETURN



PRINT_FILE:
#PRINT THE REPORT TO SCREEN, FILE, PRINTER OR FILE & PRINTER.
echo -n "Output to ${boldON}s${boldOFF}creen, ${boldON}p${boldOFF}rinter, "
echo -n "${boldON}f${boldOFF}ile or ${boldON}b${boldOFF}oth?  "
set output = $<
if ( `expr "$output" : '.*'` == 0 ) then
  goto $RETURN
endif
set output = `expr "$output" : '\(.\)'|tr 'A-Z' 'a-z'`

if ( $output == 'f' || $output == 'b' ) then
  echo " "
  echo -n "Filename ["${fn}"]:  "
  set outfile = $<
  if ( $outfile == 'm' ) then
    goto PRINT_MENU
  endif
  if ( `expr "$outfile" : '.*'` == 0 ) then
    set outfile = $fn
  endif
  set outfile = ${printdir}"/"${outfile}
  rm -f $outfile
endif

if ( $output == 'p' || $output == 'b' ) then
  if ( $printer == 'NONE' ) then
    echo -n "$erase"
    GET_TEMP_PRINTER:
    echo -n "$up1"
    echo -n "$erase"
    echo -n "Printer:  "    
    set printer = $<
    if ( `expr "$printer" : '.*'` == 0 ) then
      echo -n "$erase"
      echo -n "$up1"
      goto PRINT_FILE
    endif
    if ( $printer == 'm' ) then
      goto PRINT_MENU
    endif
    if ( `lpstat -p|grep -c $printer` == 0 ) then
      echo "No such printer: ${printer}."
      echo -n "$up1"
      goto GET_TEMP_PRINTER
    endif
  endif
endif

switch ( $output )
case p:
  printcmd
  rm -f filetoprint   
  breaksw
case s:
  clear
  more filetoprint          
  rm -f filetoprint
  echo " "
  echo "Print complete..."
  $<
  echo " "
  echo " "
  breaksw
case f:
  mv filetoprint $outfile
  breaksw
case b:
  printcmd
  mv filetoprint $outfile
  breaksw
default:
  goto PRINT_FILE
  breaksw
endsw

goto $RETURN
 



INITIALIZE:
#CLEAN UP CALENDAR, AND SET UP OPERATION VARIABLES.
echo "Please wait, cleaning up calendar..."
if ( `cat $cleandate` != $today ) then
  #REMOVE PREVIOUS DAY SCHEDULES FOR CURRENT MONTH.
  #WILL REMOVE OLD SCHEDULES FROM THE FIRST OF THE MONTH UP TO
  #TODAY, FOR THE CURRENT MONTH.
  set day = `echo $today|cut -f2 -d'/'`
  set todaypos = `nl $calendar|grep $today|line`
  @ spos = ( $todaypos[1] - $day ) + 1
  @ numdel = $day - 1
  set daystodelete = ( `tail +$spos $calendar|head -$numdel|sed "s,/,\.,g"` )
  cd $resv >& /dev/null
  rm -rf $daystodelete >& /dev/null
  cd $dir >& /dev/null
  echo $today > $cleandate
endif

#CREATE NUMBERED TEMPLATE FILE FOR FASTER RESPONSE.
nl $template > $numtemp

#REMOVE 'DAYS' WITH NO RESERVATIONS FOR FASTER REPORT RESPONSE.
#FIRST REMOVE ALL RESOURCE FILES WITH NO RESVERATIONS IN THEM.
set tempsize = `wc -c < $template`
foreach resource ( `cd $resv;find . -type f -print` )
  if ( `wc -c < $resv/${resource}` <= $tempsize ) then
    rm -f $resv/${resource} >& /dev/null
  endif
end
#THEN REMOVE ALL 'DAY' DIRECTORIES THAT ARE EMPTY.
foreach day ( `cd ${resv};ls` )
  if ( `du -s $resv/$day|cut -f1` <= 1 ) then
    rm -rf $resv/$day >& /dev/null
  endif
end

#SET DEFAULT POSTSCRIPT STATE
if ( ! -e $ps || -z $ps ) then
  if ( -e a2ps ) then
     echo "ON" > $ps
  else
     echo "OFF" > $ps
  endif
endif
set psstate = `cat $ps`
if ( $psstate == 'OFF' ) then
 alias printcmd 'lpr -JSCHEDULE -P$printer filetoprint'
else
  alias printcmd 'a2ps filetoprint|lpr -JSCHEDULE -P$printer'
endif

#SET RESOURCE TYPE
if ( -e $resourcefile && `wc -c < $resourcefile` > 1 ) then
  set restype = `cat $resourcefile|tr 'a-z' 'A-Z'`
else
  set restype = "HOST"
  echo $restype > $resourcefile
endif

#SET DEFAULT PRINTER
if ( -e $printerfile && `wc -c < $printerfile` > 1 ) then
  set printer = `cat $printerfile`
else
  set printer = "NONE"
  echo $printer > $printerfile
endif

#SET DEFAULT REPORT FORMAT
if ( -e $reportfile && ! -z $reportfile ) then
  set reporttype = `cat $reportfile`
else
  set reporttype = "MATRIX"
  echo $reporttype > $reportfile
endif

#ENSURE CALENDAR IS OK.
set calsize = `wc -l < ${calendar}`
set calcreateyear = `cat $calcreatefile`
if ( ${calsize} < 365 || ${calsize} > 366 || $calcreateyear != $year ) then
   echo "Creating calendar ..."
   cp /dev/null ${calendar}
   foreach month ( 1 2 3 4 5 6 7 8 9 10 11 12 )
     foreach day ( `cal $month $year|egrep '^ *[0-9]'` )
        echo "$month/$day" >> ${calendar}
     end
   end
   echo "${year}" > $calcreatefile
endif
goto AFTER_INITIALIZE




GET_LIST:
#FOR A GIVEN DAY, RETURN THE LIST OF DAYS THAT STARTS WITH SUNDAY,
#ENDS WITH THE FOLLOWING SATURDAY, AND CONTAINS THAT GIVEN DAY.
#THE LIST OF DAYS IS THEREFORE ALWAYS LENGTH = 7.
set nextyear = $year
set m = `echo $rd|cut -f1 -d'/'`
set d = `echo $rd|cut -f2 -d'/'`
set listofdays = `cal $m $year|egrep '^ *[0-9]'|grep $d|line`
if ( $#listofdays < 7 ) then
  if ( $listofdays[1] == 1 ) then
    set nmtype = 'PREV'
    @ nextm = $m - 1
    if ( $nextm == 0 ) then
      set nextm = 12
      @ nextyear = $year - 1
    endif
  else 
    set nmtype = 'NEXT'
    @ nextm = $m + 1
    if ( $nextm == 13 ) then
      set nextm = 1
      @ nextyear = $year + 1
    endif
  endif
  set cp = 0
  foreach dy ( $listofdays )
    @ cp++
    set listofdays[$cp] = ${m}"."$listofdays[$cp]
  end
  if ( $nmtype == 'PREV' ) then
    set moredays = `cal $nextm $nextyear|egrep '^ *[0-9]'|tail -1`
    set cp = 0
    foreach dy ( $moredays )
      @ cp++
      set moredays[$cp] = ${nextm}"."$moredays[$cp]
    end
    set listofdays = `echo ${moredays[*]} ${listofdays[*]}`
  else
    set moredays = `cal $nextm $nextyear|egrep '^ *[0-9]'|line`
    set cp = 0
    foreach dy ( $moredays )
      @ cp++
      set moredays[$cp] = ${nextm}"."$moredays[$cp]
    end
    set listofdays = `echo ${listofdays[*]} ${moredays[*]}`
  endif
else 
  set cp = 0
  foreach dy ( $listofdays )
    @ cp++
    set listofdays[$cp] = ${m}"."$listofdays[$cp]
  end
endif
goto $RETURN
 

