
#! /bin/csh -f -b

#	Utilities using the "atchange" command (Perl script) to watch files
#	and perform certain actions when they are modified.

#	This script was written by Alan Zaslavsky, zaslavsk@hcp.med.harvard.edu
#	This version: 27 May 1997

#	"atchange" was written by Tom Schneider and Jeff Haemer and
#	is available from https://alum.mit.edu/www/toms/atchange.html	

##################### Following lines should be customized if necessary
set ATCHANGE = atchange		## name of the atchange command on this system
set PSFLAGS = "-ef"		## System V version of ps
# set PSFLAGS="-uxww"		## BSD (e.g. SunOS) version
###################################################################

set CMD = $0			## what is this command called?
set CMD = $CMD:t

set RM = rm			## to remove the command file
if ( "x$1" == "x-s" ) then	## to save the command file ( -s option)
  set RM = "echo Command file for atchanges is saved in"
  shift
  endif

if ( "x$1" == "x" ) then	## no option specified
  echo "Usage: $CMD [-s] option [file | extension] ..."
  echo "    For details, type:   $CMD help"
  exit 0
  endif

switch ( $1 )
breaksw
	################################# help: prints help message
case help:
echo "$CMD : utilities using the $ATCHANGE command"
echo "Usage: $CMD [-s] option [file | extension] ..."
echo "   $CMD help: print this message"
echo "   $CMD show: print a list of currently running $ATCHANGE processes, "
echo " 	with PID and arguments."
echo "   $CMD kill: print list as above, then kill all of those processes"
echo " 	(after a brief pause to let you interrupt)."
echo "   $CMD backup <filenames>: start up $ATCHANGE to watch the named files"
echo " 	and create a backup each time it is modified so filename is"
echo " 	copied to filename.0, filename.0 to filename.1, etc.  If the"
echo " 	first argument after 'backup' is a digit 1-9, it is"
echo " 	interpreted as the number of the oldest (highest numbered)"
echo " 	backup that may be created; default value is 5."
echo "   $CMD make <extensions>: start up $ATCHANGE to watch all files that"
echo "	now exist in the current directory with the given extensions"
echo " 	(.c and .f by default if no extensions are specified).  $ATCHANGE"
echo " 	will run 'make', without arguments, whenever one of the files is"
echo " 	modified.  For example:    $CMD .c"
echo " 	will run 'make' each time any .c file is modified."
echo "   $CMD latex <filename>: start up $ATCHANGE to watch filename.tex"
echo " 	where filename is empty (indicating all .tex files) or one or"
echo " 	more basenames (without the .tex extension).  Whenever one of"
echo " 	the files is modified, latex is run on the file and if a "
echo " 	display is open, a window is briefly popped up to force xdvi"
echo " 	to redisplay."
echo "   -s: save the command file for make, latex, or backup options,"
echo "	so it will be left after exit as /tmp/$CMD-make.nnnn, etc."
echo "For other file watching functionality, use the $ATCHANGE command directly."
breaksw

	################################# latex: recompile latex file
case latex:
set CMDFILE = /tmp/$CMD-latex.$$
shift
	## create a list of files without extensions
if ( "x$1" == "x" ) then 
    ls *.tex |& grep -v "No match" | sed -e 's/\.tex$//' > $CMDFILE
  else 
  echo > $CMDFILE
  foreach filename ($*)
    if ( -f $filename.tex ) then
	echo $filename | sed -e 's/\.tex$//' >> $CMDFILE
	else 
	echo Warning: $filename.tex not found.
	endif
    end
  endif
	## check that we have some files
if ( ` cat $CMDFILE | wc -l` < 1 ) then
  echo "No .tex files found."
  exit 1
  endif
echo `cat $CMDFILE | wc -l` .tex files to be watched.
	## determine the value of POPCMD (shelltool call is from Tom Schneider)
if ( x$DISPLAY == x || x$DISPLAY == xNODISPLAY ) then
  set POPCMD = " "		## No display, do nothing
  else
  set POPCMD = 'shelltool  -Wp     0  450 -Ws 1142 1 -WP 68 0 +Wi test -V  '
  endif
	## set up the command file and run it
ed $CMDFILE << EOF >& /dev/null
1,\$s/.*/&.tex	telatex & ; $POPCMD\\
/
w
q
EOF
  $ATCHANGE $CMDFILE &
  $RM $CMDFILE
breaksw

	################################# backup: numbered backups of a file
case backup:
shift
if ( $#argv < 1 ) then
  echo 'No files to back up.'
  exit 0
  endif
set LAST = 5		# highest number to be created (default value)
if ( $1 =~ [1-9] ) then 
  set LAST = $1
  shift
  if ( $#argv < 1 ) then
    echo 'No files to back up.'
    exit 0
    endif
  endif
set CMDFILE = /tmp/$CMD-backup.$$
echo > $CMDFILE
foreach FILE ( $* )
  echo -n $FILE "" >> $CMDFILE
  set NEW = $LAST
  while ( $NEW > 0 )
    @ OLD = $NEW - 1
    echo "	if ( -f $FILE.$OLD ) then" >> $CMDFILE
    echo "	mv $FILE.$OLD $FILE.$NEW" >> $CMDFILE
    echo "	endif" >> $CMDFILE
    @ NEW--
    end			## end while
  echo "	if ( -f $FILE ) then" >> $CMDFILE
  echo "	cp $FILE $FILE.$NEW" >> $CMDFILE
  echo "	endif" >> $CMDFILE
  echo "" >> $CMDFILE
  end				## end foreach
$ATCHANGE $CMDFILE &
sleep 2
$RM $CMDFILE
breaksw

	################################# show: show all running atchange jobs
case show:
ps $PSFLAGS | grep -v 'grep ' | grep "/$ATCHANGE " | \
	grep `whoami` | sed -e 's/^[a-z]* //' -e "s/ .*$ATCHANGE / /"
breaksw

	################################# kill: kill running atchange jobs
case kill:
ps $PSFLAGS | grep -v 'grep ' | grep "/$ATCHANGE " | grep `whoami` | sed -e 's/^[a-z]* //' -e "s/ .*$ATCHANGE / /"
set PROCIDS = `ps $PSFLAGS | grep -v 'grep ' | grep "/$ATCHANGE " | grep $USER | awk '{print $2}' `
if ( "x$PROCIDS" == "x" ) then
  echo No $ATCHANGE processes running
  exit 0
  else
  echo About to kill these processes, you may interrupt.
  sleep 2
  kill $PROCIDS
  endif
breaksw

	################################# default: short usage message
default:
echo "Unrecognized option. For $CMD usage, type"
echo "	$CMD help"

endsw
