#! /bin/csh -f

set usage = "$0 [ -to DIR ] DIRECTORY..."

set junkdir = "../JUNK"

while ( ( $#argv >= 1 ) && ( "x$1" =~ x-* ) ) 
  if ( ( $#argv >= 2 ) && ( "x$1" == "x-to" ) ) then
    set junkdir = "$2"; shift; shift
  else
    echo "unrecognized option "'"'"$1"'"';
    echo "usage: ${usage}" ; exit 1
  endif
end

if ( $#argv < 1 ) then
  echo "usage: ${usage}"; exit 1
endif

if ( ! ( ( -d "${junkdir}" ) && ( -w "${junkdir}" ) ) ) then
  echo "${junkdir} is not a writable directory"; exit 1
else
  set junkdir = "`cd ${junkdir} && pwd`"
endif

set curd = "`pwd`"

foreach dir ( $* )

  cd "${curd}"

  if ( ! ( -d ${dir} ) ) then
    echo "${dir}: not a directory"
  else 
    set tail = "${dir:t}"
    if ( "x$tail" != "x$dir" ) then
      cd ${dir:h}
    endif
    if ( "x${tail}" !~ x9[567]-??-??-* ) then
      echo "${dir}: not a dated directory"
    else
      /bin/mv -i ${tail} ${junkdir}
    endif
  endif

end
