#! /bin/sh -e
# Options: [path to lurker-*] [config file]

epath="$1"
cfg="$2"

if ! test -z "$epath"; then \
        if ! echo "$epath" | grep -q "^/"; then
                echo If a path is specified, it must be absolute
                exit 1
        fi
        epath="$epath/";
fi
if ! test -z "$cfg"; then \
        if ! echo "$cfg" | grep -q "^/"; then
                echo If a config file is specified, its path must be absolute
                exit 1
        fi
        copt="-c"
fi

dbdir=`"${epath}lurker-params" $copt "$cfg" -d`
cd "$dbdir"

if test -d .tmp; then \
        echo "The database appears to be in the middle of a conversion already."
        echo "Please deal with the '.tmp' folder in the $dbdir and try again"
        exit 1
fi

mkdir .tmp
cd .tmp
mv -i ../* .

for l in `"${epath}lurker-list" $copt "$cfg" -i`; do
  echo Reindexing $l
  if ! test -f $l; then continue; fi
  gzip -dfc < $l | "${epath}lurker-index" $copt "$cfg" -p ./db -l $l -v
done

cd ..
rm -rf .tmp

echo
echo "Database has been regenerated; please double-check the permissions"
echo "Now flushing all web cache (re-run as root if you lack www permission)"
echo "  ${epath}lurker-prune $copt $cfg -p"
"${epath}lurker-prune" $copt "$cfg" -p
