#! /bin/csh -fe

set path = ( /usr/ucb /usr/bsd /usr/bin /bin )

unset ask
unset mumble

while ( ( $#argv > 0 ) && ( "$1" =~ -* ) )
  if ( "$1" == "-i" ) then
    set ask
    shift
  else if ( "$1" == "-v" ) then
    set mumble
    shift
  else
    echo "usage: $0 [ -i | -v ] file..."
    exit 1
  endif
end

foreach f ( $* )
  if ( ( "x$f" == "x" ) || ( "x$f" == "x." ) || \
     ( "x$f" == "x.." ) || ( "x$f" == "x~" ) \
  ) then
    echo "${f}: bad filename, ignored"
  else if ( ! ( -e "$f" ) ) then
    echo "${f}: no such file or directory"
  else if ( ( "x$f" == "x${STOLFIHOME}" ) || ( "x$f" == "x$cwd" ) ) then
    echo "${f}: better not."
  else 
    set doit
    if ( $?ask ) then
      echo -n "safe-remove ${f}? "
      if ( "$<" != 'y' ) unset doit
    else if ( $?mumble ) then
      echo "$f"
    endif
    if ( $?doit ) then
      if ( -e "${f}~~" ) /bin/mv - "${f}~~" "${f}~~~"
      if ( -e "${f}~" ) /bin/mv - "${f}~" "${f}~~"
      /bin/mv - "$f" "${f}~"
    endif
  endif
end
