#!/bin/sh

dir=`dirname "$0"`

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

target="$1"

target=${target%/}

find "$target" -name "*.e2x.gz" -delete

for fl in *.xml.gz
do
  base=${fl%.xml.gz}
  echo "$base"
  seconds_start=$(date "+%s")
  if [ -s "$dir/meshtree.txt" ]
  then
    gunzip -c "$fl" |
    xtract -transform "$dir/meshtree.txt" -e2index |
    gzip -1 > "$target/$base.e2x.gz"
  else
    gunzip -c "$fl" |
    xtract -e2index |
    gzip -1 > "$target/$base.e2x.gz"
  fi
  seconds_end=$(date "+%s")
  seconds=$((seconds_end - seconds_start))
  echo "$seconds seconds"
  sleep 1
done
