#!/bin/sh

# Public domain notice for all NCBI EDirect scripts is located at:
# https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice

doall=false
dofresh=false
flag="none"

while [ $# -gt 0 ]
do
  case "$1" in
    -strict )
      flag="strict"
      shift
      ;;
    -mixed )
      flag="mixed"
      shift
      ;;
    -path )
      shift
      ;;
    -all )
      doall=true
      shift
      ;;
    -fresh )
      dofresh=true
      shift
      ;;
    -* )
      exec >&2
      echo "$0: Unrecognized option $1"
      exit 1
      ;;
    * )
      break
      ;;
  esac
done

if [ "$#" -gt 0 ]
then
  argument="$1"
  target=$(cd "$argument" && pwd)
  target=${target%/}
  case "$target" in
    */Archive ) ;;
    * ) target=$target/Archive ;;
  esac
else
  if [ -z "${EDIRECT_PMC_MASTER}" ]
  then
    echo "Must supply path to archive files or set EDIRECT_PMC_MASTER environment variable"
    exit 1
  else
    MASTER="${EDIRECT_PMC_MASTER}"
    MASTER=${MASTER%/}
    target="$MASTER/Archive"
  fi
fi

osname=`uname -s | sed -e 's/_NT-.*$/_NT/; s/^MINGW[0-9]*/CYGWIN/'`
if [ "$osname" = "CYGWIN_NT" -a -x /bin/cygpath ]
then
  target=`cygpath -w "$target"`
fi

target=${target%/}

HEAD=$(cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
EOF
)

TAIL=$(cat <<EOF
EOF
)

echo "$HEAD"
rchive -gzip -db pmc -flag "$flag" -fetch "$target"
echo "$TAIL"
