# delete.sh - remove an explicit tag for a file
# 
################################################################
# Copyright (C) 2001, 2002 Tom Lord
# 
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.
# 

set -e 

################################################################
# special options
# 
# Some options are special:
# 
#       --version | -V
#       --help | -h
# 
if test $# -ne 0 ; then

  for opt in "$@" ; do
    case $opt in

      --version|-V) exec larch --version
                    ;;


      --help|-h)
                printf "remove an explicit inventory tag\\n"
                printf "usage: delete [options] FILE ... \\n"
                printf "\\n"
                printf " -V --version                  print version info\\n"
                printf " -h --help                     display help\\n"
                printf "\\n"
                printf "Remove an explicit inventory tag for FILE (which may be a\\n"
                printf "regular file, symbolic link, or directory).\\n"
                printf "\\n"
                exit 0
                ;;

      *)
                ;;
    esac
  done
fi

################################################################
# Ordinary Options
# 
# 

while test $# -ne 0 ; do

  case "$1" in 

    --silent)           shift   # ignored
                        ;;
    --quiet)            shift   # ignored
                        ;;
    --report)           shift   # ignored
                        ;;
    --verbose)          shift   # ignored
                        ;;


    --)                 shift
                        break
                        ;;
                        
    -*)                 printf "delete: unrecognized option (%s)\\n" "$1" 1>&2
                        printf "try --help\\n" 1>&2
                        exit 1
                        ;;

    *)                  break
                        ;;
  esac

done



################################################################
# Ordinary Arguments
# 

if test $# -lt 1 ; then
  printf "usage: delete [options] file ...\\n" 1>&2
  printf "try --help\\n" 1>&2
  exit 1
fi


################################################################
# Process Arguments
# 

here="`pwd`"

for file in "$@" ; do

  ################################################################
  # Sanity Check and Process Defaults
  # 

  cd "$here"

  if test -d "$file" ; then
    file_dir="$file"
    id_file="=id"
  else
    file_dir="`dirname \"$file\"`"
    file_basename="`basename \"$file\"`"
    id_file="$file_basename.id"
  fi

  cd "$file_dir"
  file_dir="`pwd`"
  wdroot="`larch tree-root`"

  ################################################################
  # Record
  # 

  cd "$file_dir"

  if test ! -e ".arch-ids/$id_file" ; then
    printf "delete: file not previously added\\n" 1>&2
    printf "\\n" 1>&2
    exit 1
  fi

  rm ".arch-ids/$id_file"

done
