#!/bin/sh
# 
# valid-archive-location.sh - test an archive location 
################################################################
# 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 "test an archive location for validity\\n"
		printf "usage: valid-archive-location [options] -- location\\n"
		printf "\\n"
		printf " -V --version                  print version info\\n"
		printf " -h --help                     display help\\n"
		printf "\\n"
		printf " -e --errname prog             specify program name for errors\\n"
		printf "\\n"
		printf "Exit with status 0 if LOCATION is a valid archive location\\n"
		printf "status 1 otherwise.\\n"
		printf "\\n"
		printf "If an error name is specified (-e or --errname), then invalid\\n"
		printf "names cause an error message on stdout.  Otherwise, the exit\\n"
		printf "status is the only output.\\n"
		printf "\\n"
		printf "Note that the command checks only the syntax of the archive\\n"
		printf "location -- it does not check to see that the archive actually\\n"
		printf "exists.  There syntactic restrictions on archive names are\\n"
		printf "quite minimal.\\n"
		printf "\\n"
		exit 0
      		;;

      *)
		;;
    esac
  done
fi

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

while test $# -ne 0 ; do

  case "$1" in 
    --)			shift
    			break
			;;
			
    -e|--errname)	shift
			if test $# -eq 0 ; then
			  printf "valid-archive-location: -e and --errname require an argument\\n" 1>&2
			  printf "try --help\\n" 1>&2
			  exit 1
			fi
			errname="$1"
			shift
			;;

    -*)			printf "valid-archive-location: unrecognized option (%s)\\n" "$1" 1>&2
			printf "try --help\\n" 1>&2
			exit 1
			;;

    *)			break
    			;;
  esac

done



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

if test $# -ne 1 ; then
  printf "usage: valid-archive-location [options] -- location\\n" 1>&2
  printf "try --help\\n" 1>&2
  exit 1
fi

location="$1"

################################################################
# Check Archive Name
# 
# 
set +e
filtered="`printf \"%s\\n\" \"$location\" | grep -E \"^[[:alnum:][:punct:]]*\\$\"`"
set -e

if test "x$filtered" = x ; then
  if test "x$errname" != x ; then
    printf "%s: invalid archive location (%s)\\n" "$errname" "$location" 1>&2
  fi
  exit 1
fi

exit 0

