#!/bin/sh

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

target="$1"

target=${target%/}

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%/}

for fld in NORM STEM YEAR CODE TREE
do
  seconds_start=$(date "+%s")
  echo "$fld"
  find "." -name "*.mrg.gz" |
  sort |
  xargs -n 100 echo |
  while read files
  do
    rchive -promote "$target" "$fld" $files
  done
  seconds_end=$(date "+%s")
  seconds=$((seconds_end - seconds_start))
  echo "($fld $seconds seconds)"
done
