#!/bin/sh

target="$1"
flag="none"

if [ "$#" -eq 0 ]
then
  echo "Must supply path to archive files"
  exit 1
fi

if [ "$#" -gt 1 ]
then
  flag="$1"
  target="$2"
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

# stashing on PC hangs if XML is sent through stdin, so decompress and use -input instead

for fl in *.xml.gz
do
  base=${fl%.xml.gz}
  if [ -f "$base.snt" ]
  then
    continue
  fi
  echo "$base"
  gunzip -c "$fl" > "$base.xml"
  xtract -input "$base.xml" -archive "$target" -gzip -flag "$flag" \
    -index MedlineCitation/PMID -pattern PubmedArticle
  touch "$base.snt"
  rm "$base.xml"
done
