#!/bin/sh


#--- Help function -------------
printhelp()
{
   echo "   "
   echo "  pq2-verify"
   echo "   "
   echo "  Purpose: verify the content one or more datasets"
   echo "   "
   echo "  Syntax:"
   echo "           pq2-verify [-h|--help] [-v] [-k] [(-d|--dataset=)] datasetname"
   echo "                      [-o options] [-r redir|--redir=redir]"
   echo "                      [-t tmpdir|--tmp=tmpdir] [-u serviceurl|--url=serviceurl]"
   echo "   "
   echo "   -h | --help   Print this help"
   echo "   -v            Verbose mode"
   echo "   -k            Keep temporary files"
   echo "   datasetname   Datasets to be analysed; the '*' wild card in the items (in such a case"
   echo "                 the full string - as shown by pq2-ls - should be given, e.g. \"/default/ganis/h1-set5*\""
   echo "   options       Options for verifying datasets; a combination of:"
   echo "                    A               process all files"
   echo "                    D               process only files marked as staged ('on Disk') (if 'A'"
   echo "                                    is not specified the default is to process only files marked)"
   echo "                                    as non-staged)"
   echo "                    O               open the files marked as staged when processing only files"
   echo "                                    marked as non-staged"
   echo "                    T               open and touch the files marked as staged when processing"
   echo "                                    only files marked as non-staged"
   echo "                    I               do not check the actual stage status on selected files"
   echo "                    N               do nothing on the selected files"
   echo "                    P               open the selected files and extract the meta information"
   echo "                    L               only locate the selected files"
   echo "                    S               issue a stage request for the selected files not yet staged"
   echo "                    V               notify the actions"
   echo "                 Some combination are not valid: an error will be printed"
   echo "   redir:        URL to be used to relocate the file"
   echo "   serviceurl:   URL of the PROOF master or data server providing the information;"
   echo "                 for data servers, it must include the directory."
   echo "                 Can be specified via the envs PQ2PROOFURL or PQ2DSSRVURL."
   echo "   tmpdir        Directory for temporary files [/tmp/<user>]."
   echo "   "
}

PQ2=`which pq2 2> /dev/null`
if test "x$PQ2" = "x" ; then
   echo "Unknown command 'pq2'"
   exit 1
fi

DBGOPT=""
DSNAME=""
KEEPOPT=""
OPTS=""
REDIR=""
SRVURL=""
TDIR=$TMPDIR
#
# Parse long options first
other_args=
short_opts=
for i in $@ ; do
   opt=""
   case $i in
      --*) opt=`echo "$i" | sed 's/--//'` ;;
      -*) short_opts="$short_opts $i" ;;
      *) other_args="$other_args $i"; short_opts="$short_opts $i" ;;
   esac
   if test ! "x$opt" = "x" ; then
      case "$opt" in
         *=*) oarg=`echo "$opt" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
         *) oarg= ;;
      esac ;
      case $opt in
         dataset=*) DSNAME="-d $oarg" ;;
         help)      printhelp ; exit ;;
         keep)      KEEPOPT="-k" ;;
         redir=*)   REDIR="-r $oarg" ;;
         tmp=*)     TDIR="$oarg" ;;
         url=*)     SRVURL="-u $oarg" ;;
      esac
   fi
done

if test ! "x$short_opts" = "x" ; then
   while getopts d:o:r:t:u:hkv i $short_opts ; do
      case $i in
      d) DSNAME="-d $OPTARG" ;;
      h) printhelp ; exit ;;
      k) KEEPOPT="-k" ;;
      o) OPTS="-o $OPTARG" ;;
      r) REDIR="-r $OPTARG" ;;
      t) TDIR="$OPTARG" ;;
      u) SRVURL="-u $OPTARG" ;;
      v) DBGOPT="-v" ;;
      \?) printhelp; exit 1 ;;
      esac
      if test ! "x$OPTARG" = "x" ; then
         noa=
         for a in $other_args ; do
            if test ! "x$OPTARG" = "x$a" ; then
               noa="$noa $a"
            fi
         done
         other_args=$noa
      fi
   done

   # Fill empty fields with any non-prefixed argument
   if test ! "x$other_args" = "x" && test "x$DSNAME" = "x" ; then
      DSNAME="-d $other_args"
   fi
fi

if test "x$DSNAME" = "x"; then
   echo "Some arguments are missing (d:$DSNAME)!"
   printhelp
   exit
fi

# Run
export TMPDIR="$TDIR"; $PQ2 verify $DBGOPT $KEEPOPT $DSNAME $OPTS $REDIR $SRVURL
exit $?

