From zaslavsk@hcp.med.harvard.edu Fri Mar 13 07:35 EST 1998
Received: from FCRFV1.NCIFCRF.GOV (fcrfv1.ncifcrf.gov [129.43.51.4]) by ncisun1-nf0.ncifcrf.gov (8.8.7/8.7.3) with SMTP id HAA00547 for <toms@fcs280s.NCIFCRF.GOV>; Fri, 13 Mar 1998 07:35:02 -0500 (EST)
Received: from hcp.med.harvard.edu by FCRFV1.NCIFCRF.GOV with ESMTP
          for toms@alum.mit.edu; Fri, 13 Mar 1998 7:37:53 -0500
Received: from nightingale.med.harvard.edu (nightingale.med.harvard.edu [134.174.144.99])
	by mail.hcp.med.harvard.edu (8.8.5/8.8.5) with ESMTP id HAA21837
	for <toms@alum.mit.edu>; Fri, 13 Mar 1998 07:35:06 -0500 (EST)
Received: (from zaslavsk@localhost) by nightingale.med.harvard.edu (8.7.3/8.7.3) id HAA14436 for toms@alum.mit.edu; Fri, 13 Mar 1998 07:35:05 -0500 (EST)
Resent-Message-Id: <199803131235.HAA00547@ncisun1-nf0.ncifcrf.gov>
Resent-Date: Fri, 13 Mar 1998 7:36:39 -0500
Resent-From: toms@fcrfv2.ncifcrf.gov
Resent-To: toms@ncisun1-nf0.ncifcrf.gov
Date: Fri, 13 Mar 1998 07:35:05 -0500 (EST)
From: Alan Zaslavsky <zaslavsk@hcp.med.harvard.edu>
Message-Id: <199803131235.HAA14436@nightingale.med.harvard.edu>
To: toms@alum.mit.edu
Subject: atchanges (revised version)
X-Sun-Charset: US-ASCII
Content-Type: text
Content-Length: 10340
Status: O


Tom, since you so kindly posted this program, would you be willing to
replace the current file with the following updated version?  This fixes
a few bugs and improves some features, relative to the version you have --
just a variety of things that came up in the course of using it last year.

	Best regards,
	Alan Zaslavsky



#! /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
#	Original: 23 May 1997
#	This version: 10 December 1997

#	"atchange" was written by Tom Schneider and Jeff Haemer and
#	is available from https://alum.mit.edu/www/toms/atchange.html.
#	"html" option is based on an idea by Jeff Copeland and Jeff Haemer.
#	"latex" option uses Tom Schneider's ideas.

##################### Following lines should be customized if necessary
set PSFLAGS = "-ef"		## System V (e.g. Solaris) version of ps
# set PSFLAGS="-uxww"		## BSD (e.g. SunOS) version
set ATCHANGE = atchange		## name of the atchange command on this system
set ATPATH = ""			## path to atchange if needed (not on default path)
	## the following command pops up a window all across the
	## screen and then lets it die; modify as needed for your system
set POPCMD = 'shelltool  -Wp     0  450 -Ws 1142 1 -WP 68 0 +Wi test -V '
set LATEX = "latex"		## LaTeX command on this system
set WAIT = 2			## waiting time for atchanges to start up
				## before deleting temporary file
###################################################################

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

	## we will create a link to the command file with a "pretty" name
	##  that shows original arguments when we run "ps"; where to put it?
set LINKDIR = /tmp/		## we can always use /tmp directory
if ( -w . ) then
  set LINKDIR = ""		## working directory if writeable, prettier!
  endif

set RM = rm			## to remove the command file
set PRINTONLY = 0		## to execute the command
while ( "$1" =~ -* )
  switch ( $1 )
   case -s			## to save the command file ( -s option)
     set RM = "echo Command file passed to $ATCHANGE is saved in"
     breaksw
   case -p			## to print command file, but do not run
     set PRINTONLY = 1
     breaksw
   default
     echo Unrecognized flag "$1"
   endsw
  shift
  end

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

switch ( $1 )
	################################# help: prints help message
case help:
echo "$CMD : utilities using the $ATCHANGE command"
echo "Usage: $CMD [-s|-p] 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).  Then runs start."
echo "   $CMD start: run $ATCHANGE to start up all files named in the environment"
echo "	variable ATCHANGEFILES."
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 <files,extensions>: start up $ATCHANGE to watch all files "
echo "	that now exist in the current directory with the given extensions"
echo " 	(.c and .f by default if no extensions are specified) or filenames"
echo "	(anything not beginning with '.').  $ATCHANGE will run 'make',"
echo " 	without arguments, whenever one of the files is modified.  "
echo " 	For example:    $CMD make .c"
echo " 	will run 'make' each time any .c file is modified."
echo "   $CMD html <filename>: load filename (a single browsable file, "
echo "	typically HTML) into an already-running netscape browser, "
echo "	then start up $ATCHANGE to watch filename and reload into browser "
echo "	whenever 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 "   -p: print (do not run) command file for make, latex, or backup options."
echo "For other file watching functionality, use the $ATCHANGE command directly."
breaksw

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

	################################# kill: kill running atchange jobs
case kill:
ps $PSFLAGS | grep -v 'grep ' | grep "/$ATCHANGE " | grep `whoami` | sed -e 's/^[^ ]* *//' -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 4
  kill $PROCIDS
  endif
if ( $?ATCHANGEFILES ) then	## now run equivalent of: atchanges start
  foreach file ( $ATCHANGEFILES )
  if ( -f $file ) then
    $ATPATH$ATCHANGE $file &
    echo "Started $file."
    else
    echo "File $file does not exist."
    endif
    end  
  endif
breaksw

	################################# start: start jobs in ATCHANGEFILES
case start:
if ( $?ATCHANGEFILES ) then
  foreach file ( $ATCHANGEFILES )
  if ( -f $file ) then
    $ATPATH$ATCHANGE $file &
    echo "Started $file."
    else
    echo "File $file does not exist."
    endif
    end
  else
  echo "Environment variable ATCHANGEFILES is not defined."
  endif
breaksw

	################################# latex: recompile latex file
case latex:
set CMDFILE = /tmp/$CMD-latex.$$
set ARGS = "$*"
shift
touch $CMDFILE
	## 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."
  rm $CMDFILE
  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 DISPCMD = " "		## No display, do nothing
  else
  set DISPCMD = " $POPCMD "	## Command to pop up a window briefly
  endif
	## set up the command file and run i
ed $CMDFILE << EOF >& /dev/null
1,\$s@.*@&.tex	$LATEX & \< /dev/null | sed -n -e '/Warning/p' -e '/^\\\!/,\$p' -e '/^Output written/p'  ; $DISPCMD \\
@
w
q
EOF
  if ( $PRINTONLY ) then
    cat $CMDFILE
    else
    ln -s $CMDFILE $LINKDIR"* $ARGS"
    $ATPATH$ATCHANGE $LINKDIR"* $ARGS" &
    sleep $WAIT
    rm $LINKDIR"* $ARGS"
    endif
  $RM $CMDFILE
breaksw

	################################# make: list of files and extensions
case make:
set CMDFILE = /tmp/$CMD-make.$$
set ARGS = "$*"
shift
if ( $#argv < 1 ) then 
  set FILENAMES = ".c .f"
  else
  set FILENAMES = "$*"
  endif
touch $CMDFILE
	## create a list of files, expanding extensions
foreach filename ($*)
  if ( "$filename" =~ .* ) then 
    ls *$1 |& grep -v "No match" >> $CMDFILE
    else 
    echo $filename >> $CMDFILE
    endif
  end
	## check that we have some files
if ( ` cat $CMDFILE | wc -l` < 1 ) then
  echo "No files found."
  rm $CMDFILE
  exit 1
  endif
echo `cat $CMDFILE | wc -l` files to be watched.
	## set up the command file and run it
ed $CMDFILE << EOF >& /dev/null
1,\$s/.*/&	make  /
w
q
EOF
  if ( $PRINTONLY ) then
    cat $CMDFILE
    else
    ln -s $CMDFILE $LINKDIR"* $ARGS"
    $ATPATH$ATCHANGE $LINKDIR"* $ARGS" &
    sleep $WAIT
    rm $LINKDIR"* $ARGS"
    endif
  $RM $CMDFILE
breaksw

	################################# backup: numbered backups of a file
case backup:
set CMDFILE = /tmp/$CMD-backup.$$
set ARGS = "$*"
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
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
if ( $PRINTONLY ) then
  cat $CMDFILE
  else
  ln -s $CMDFILE $LINKDIR"* $ARGS"
  $ATPATH$ATCHANGE $LINKDIR"* $ARGS" &
  sleep $WAIT
  rm $LINKDIR"* $ARGS"
  endif
$RM $CMDFILE
breaksw

	################################# html: load netscape, reload at change
case html:
shift
if ( $#argv == 0 ) then
  echo "Error: a file argument must be given"
  exit 1
  endif
if ( $#argv > 1 ) then
  echo "Warning: only one filename allowed, extra filenames will be ignored"
  endif
set html = $1
if ( "` echo $html | cut -c 1 `" == "/" ) then
  set htmlpath = "$html"
  else 
  set htmlpath = "$PWD/$html"
  endif
if ( ! -f $htmlpath ) then
  echo File $html does not exist to display
  exit 1
  else
  netscape -noraise -remote "openFile($htmlpath)"
  $ATPATH$ATCHANGE $html "netscape -noraise -remote 'reload'" & 
  endif
breaksw

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

endsw

