# 
# tagging-method.sh - print or change the tagging method
################################################################
# 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 "print or change a project tree tagging method\\n"
                printf "usage: tagging-method [options] [method] \\n"
                printf "\\n"
                printf " -V --version                  print version info\\n"
                printf " -h --help                     display help\\n"
                printf "\\n"
                printf " -d --dir DIR                  cd to DIR first\\n"
                printf "\\n"
                printf " --strict                      error if not set\\n"
                printf "\\n"
                printf "Print or change the method by which source files are\\n"
                printf "identified in DIR (or the current directory).\\n"
                printf "\\n"
                printf "When setting, METHOD must be one of:\\n"
                printf "\\n"
                printf "        names           -- use naming conventions only\\n"
                printf "        implicit        -- use naming conventions but permit\\n"
                printf "                           for inventory tags\\n"
                printf "        explicit        -- require explicit designation of source\\n"
                printf "\\n"
                printf "When printing, if \"--strict\" is provided but no tagging\\n"
                printf "method is explicitly set, print an error.\\n"
                printf "\\n"
                exit 0
                ;;

      *)
                ;;
    esac
  done
fi

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

dir=.
strict=

while test $# -ne 0 ; do

  case "$1" in 

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


    -d|--dir)           shift
                        if test $# -eq 0 ; then
                          printf "tagging-method: -d and --dir require an argument\\n" 1>&2
                          printf "try --help\\n" 1>&2
                          exit 1
                        fi
                        dir="$1"
                        shift
                        ;;

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

    *)                  break
                        ;;
  esac

done



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

if test $# -gt 1 ; then
  printf "usage: tagging-method [options] [method]\\n" 1>&2
  printf "try --help\\n" 1>&2
  exit 1
fi

if test $# -ne 0 ; then
  method="$1"
  shift
else
  method=
fi


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

here="`pwd`"
cd "$dir"
dir="`pwd`"

cd "$dir"
wdroot="`larch tree-root`"

if test ! -z "$method" ; then
  set=yes
else
  set=
  if test ! -e "$wdroot/{arch}/=tagging-method" ; then
    method=names
  else
    method="`cat \"$wdroot/{arch}/=tagging-method\" | grep -E -e \"^(explicit|implicit|names)[[:space:]]*$\" | tail -n 1`"
  fi
fi


case "$method" in
  names)        ;;
  implicit)     ;;
  explicit)     ;;
  *)            if test -z "$method" ; then
                  method=names
                else
                  printf "tagging-method: unrecognized method\\n" 1>&2
                  printf "  method: %s\\n" "$method" 1>&2
                  printf "\\n" 1>&2
                  printf "try --help\\n" 1>&2
                  exit 1
                fi
                ;;
esac


################################################################
# Record or Print
# 

if test -z "$set" ; then

  if test ! -z "$strict" -a ! -e "$wdroot/{arch}/=tagging-method" ; then
    printf "tagging-method: no tagging method set for this tree\\n" 1>&2
    printf "\\n" 1>&2
    printf "  You must set a tagging method.\\n" 1>&2
    printf "\\n" 1>&2
    printf "  Try \"larch tagging-method --help\"\\n" 1>&2
    printf "\\n" 1>&2
    exit 1
  fi

  printf "%s\\n" "$method"

else

  cd "$wdroot/{arch}"
  if test ! -e "=tagging-method" ; then
    printf "%s\n" "$method" > "=tagging-method"
  else
    mv "=tagging-method" ,tagging
    cat ,tagging \
    | awk -v method="$method" \
          'BEGIN {
                   changed = 0;
                 }
           match($0, "^(explicit|implicit|names)[ \t]*") {
                                                           sub("explicit|implicit|names", method, $0);
                                                           changed = 1;
                                                         }
           { print $0; } 
           END {
                 if (!changed)
                   print method;
               }'\
    > "=tagging-method"
    rm ,tagging
  fi
fi

