#!/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

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

target="$1"
shift

target=${target%/}

xtras="false"

if [ "$#" -gt 0 ]
then
  xtras="$1"
  shift
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%/}

word="TITL TIAB STEM TLEN TNUM"
mesh="CODE TREE"
jour="YEAR JOUR VOL ISS PAGE LANG"
auth="ANUM AUTH FAUT LAUT CSRT INVR"
xtra="MAJR MESH SUBH"

term=$( echo "$word $mesh $jour $auth" )
if [ "$xtras" = "true" ]
then
  term=$( echo "$word $mesh $jour $auth $xtra" )
fi

for fl in *.mrg.gz
do
  echo "$fl"
done |
sort |
xargs -n 100 echo |
while read files
do
  rchive -promote "$target" "$term" $files
done
