#!/bin/csh -f
#     upload [ignore_dirs] [-adr] [-] [name ...]
#     download [ignore_dirs] [-adr] [-] [name ...]
# For each name copies (updating only) matching files on the appropriate
# directory (see note below) of the home machine to the corresponding files
# on /th (up) or vice-versa (down), if the file is a plain file.
# Matching plain files on the target are deleted (using del).
# If name is a directory (but ignore_dirs is not included),
# the process is applied to all the plain files in the directory,
# and any plain files not present in the source directory but present in the
# target directory are deleted (using del).
# With the -r option, directories are created and deleted and 
# the operations are applied recursively to subdirectories
# encountered.  The -a option makes all entries be considered; without it
# those beginning with a dot are not considered.  The -d option restricts
# consideration to those entries beginning with dot.
# The entries . and .. are always ignored.
# The options -a and -d cannot appear together in the same option specifier
# If they appear in the same command line, the -a takes precedence.
# With no names, the command recursively executes itself with new names as in
#     cd $HOME
#     upload -d <options> . ;
#     upload <options> bin emacs-lisp tex
# (see notes).

# NOTE: Quoted names are expanded in the context of the source and
# target directories each of which is the cwd, the cwd prepended by /th,
# or the cwd with a leading /th stripped off, according to whether the
# cwd is on /th and whether you are uploading or downloading.  Also, an
# initial /usr in the cwd is changed to /u if /th is to be prepended.

# NOTE:  File names ending in .forward are never included in the treatment.

set debug = 0

set noglob

set command = $0

# PATH SETUP
if ($?DEL) then
  if ($DEL == "") then
    set path = ($path /bin /usr/bin /usr/ucb)
  else
    set path = ($DEL /bin /usr/bin /usr/ucb)
  endif
else
  set path = (/th/u/rig/bin /u/rig/bin /bin /usr/bin /usr/ucb)
endif
if ($debug) echo ''
if ($debug) echo "${command}:" path=\"$path\"

set wd = `pwd`
if ("$wd" =~ /th/*) then
  set fshome_dir = $wd
  set home_dir = `echo $wd | sed 's+/th++'`
else
  set home_dir = $wd
  set fshome_dir = /th`echo $wd | sed 's+^/usr/+/u/+' | sed 's+^/usr$+/u+'`
endif
if ($debug) echo "${command}:" home_dir=\"$home_dir\" ''\
			      fshome_dir=\"$fshome_dir\"

switch ($command:t)
case up*:
  set src_dir = $home_dir
  set dest_dir = $fshome_dir
  breaksw
case down*:
  set src_dir = $fshome_dir
  set dest_dir = $home_dir
  breaksw
endsw
if ($debug) echo "${command}:" src_dir=\"$src_dir\"\ \ dest_dir=\"$dest_dir\"
if (! -d $src_dir) then
  echo "${command}: Source directory $src_dir does not exist."
  exit 3
else if (! -d $dest_dir) then
  echo "${command}: Destination directory $dest_dir does not exist."
  exit 2
endif

if ("$1" == "ignore_dirs") then
  set ignore_dirs
  shift
  if ($debug) echo ignore_dirs=\"$ignore_dirs\"
endif

set options
foreach i ($*)
  switch($i)
  case -a:
  case -d:
  case -r:
  case -ar:
  case -dr:
  case -ra:
  case -rd:
    shift
    set options = "$options $i"
    breaksw
  case -:
    break
    breaksw
  case -*:
    echo "${command}: Unrecognized option $i."
    exit 1
    breaksw
  default:
    break
  endsw
end
if ($debug) echo "${command}:" options=\"$options\"
if ($debug) echo "${command}:" \$\*=\"$*\"

if ($#argv == 0) then
  cd $HOME
  $command -d $options . ; $command $options bin emacs-lisp tex
  exit 0
endif
if ("$1" == "-") shift

set entry_patterns = *
if ("$options" =~ *d*) set entry_patterns = (.[ --]* ..?* .[/-]*)
if ("$options" =~ *a*)\
  set entry_patterns = ([ --]* .[ --]* ..?* .[/-]* [/-]*)
if ($debug) echo "${command}:" entry_patterns=\"$entry_patterns\"

unset noglob
set orig_dir = `pwd`
cd $src_dir
set src_tmps = ($* /dev/null*)
set src_files
foreach tmp ($src_tmps)
  if (-e $tmp) set src_files = ($src_files $tmp)
end
cd $orig_dir
if ($debug) then
  echo "${command}:" src_tmps=\"$src_tmps\"
  echo "${command}:" src_files=\"$src_files\"
endif
cd $dest_dir
set dest_tmps = ($* /dev/null*)
set dest_files
foreach tmp ($dest_tmps)
  if (-e $tmp) set dest_files = ($dest_files $tmp)
end
cd $orig_dir
if ($debug) then
  echo "${command}:" dest_tmps=\"$dest_tmps\"
  echo "${command}:" dest_files=\"$dest_files\"
endif
set noglob

set src_count = 1 ; set dest_count = 1
while ($src_count < $#src_files || $dest_count < $#dest_files)
  if ("$src_files[$src_count]" =~ *.forward) then
    @ src_count++
    continue
  endif
  while (($dest_count < $#dest_files) &&\
         ($src_count == $#src_files ||\
          `expr "$dest_files[$dest_count]" \< "$src_files[$src_count]"`))
    if ((-f $dest_dir/$dest_files[$dest_count] || "$options" =~ *r*) &&\
        ("$dest_files[$dest_count]" !~ *.forward)) then
      set delfile = $dest_dir/$dest_files[$dest_count]
      del - $delfile:h $delfile:t
    endif
    @ dest_count++
  end
  if ($src_count == $#src_files) exit 0
  set src_file = $src_files[$src_count]
  set dest_file = $dest_files[$dest_count]
  if ($debug)\
    echo "${command}:" src_file=\"$src_file\" '' dest_file=\"$dest_file\"
  if (-d $src_dir/$src_file) then
    if (! $?ignore_dirs) then
      set src_entries
      foreach pattern ($entry_patterns)
        set src_entries = ($src_entries $src_file/$pattern)
      end
      if ($debug) echo "${command}:" src_entries=\"$src_entries\"
      if ("$options" =~ *r*) then
        if (-f $dest_dir/$src_file) then
          set delfile = $dest_dir/$src_file
          del - $delfile:h $delfile:t
        endif
        if (! -e $dest_dir/$src_file) mkdir $dest_dir/$src_file
        $command $options - "$src_entries"
      else
        if (! -d $dest_dir/$src_file) then
          echo "${command}: No such target subdirectory: $dest_dir/$src_file"
        else
          $command ignore_dirs $options - "$src_entries"
        endif
      endif
    endif
  else
    echo "${command}:" Doing cp of\
      $src_dir/$src_file to $dest_dir/$src_file
    cp -u $src_dir/$src_file $dest_dir/$src_file
  endif
  if ("$dest_file" == "$src_file") @ dest_count++
  @ src_count++
end
if ($debug) echo ''
