#! /bin/csh -f
#
# modify_docs - Script to set the top document path in all documents in a
#               directory hierarchy.
#
# Version 1.3
#
# Copyright (c) 1994 by
#
#    Confluent, Inc.
#    400 Spear St., Suite 207
#    San Francisco, CA 94105
#
# Voice: 415-764-1000
# Fax: 415-764-1008
# Internet: info@confluent.com
#
# Created: 940905 - wpt
# Modified: 940913 - wpt : take into account both palettes and examples.
# Modified: 950109 - jag : move to admin, generalize, handle only 1 directory
# Modified: 950422 - jag : change paths to TOP_DIR if third argument specified

# Set up the command name used to invoke this script.
set cmd = $0
set cmd = $cmd:t

# Print usage if there were too few arguments.
if ($#argv < 2) then
  echo ""
  echo "usage: $cmd <directory> <document> [<undo_path>]"
  echo ""
  echo "where:"
  echo ""
  echo "  <directory> - directory hierarchy in which documents should be modified"
  echo "  <document> - name of documents to be modified"
  echo "  <undo_path> - if specified, replace this path with TOP_DIR"
  echo ""
  echo "Modify the top document path in all files named <document> in the"
  echo "<directory> hierarchy if <undo_path> is not specified."
  echo ""
  echo "For example, typing:"
  echo ""
  echo "   $cmd /usr/local/confluent/vt-1.2/share/examples vthought.txt"
  echo ""
  echo "would modify all vthought.txt files in the directory hierarchy"
  echo "/usr/local/confluent/vt-1.2/share/examples."
  echo ""
  echo "This could be undone with:"
  echo ""
  echo "   set dir = /usr/local/confluent/vt-1.2/share"
  echo "   $cmd "'$dir/examples vthought.txt $dir'
  echo ""
  exit(0)
endif

# Get the document directory and name.
set dir = "$1"
set document = "$2"
set undo_path = "$3"
@ undo = ($#argv > 2)

# The document directory must exist.
if (! -d $dir) then
  echo ""
  echo "Error: The following directory does not exist:"
  echo ""
  echo "   $dir"
  echo ""
  echo "Aborting modification of document files."
  echo ""
  exit(1)
endif

# Convert the directory path to absolute form.
pushd $dir >& /dev/null
set dirs = (`dirs -l`)
popd >& /dev/null
set dir = $dirs[1]

# Get the parent directory.
set top_dir = $dir:h

# Recursively find all document files.
set filelist = `find $dir -name $document -print`

# Modify the file contents.
foreach file ($filelist)
  cp $file $file.tmp
  if ($undo) then
    sed -e "s;$undo_path;/TOP_DIR;" $file.tmp > $file
  else
    sed -e "s;/TOP_DIR;$top_dir;" $file.tmp > $file
  endif
  rm $file.tmp
end

# Exit.
exit(0)
