#! /bin/sh

prefix=@prefix@
localstatedir=@localstatedir@

usage()
{
    cat <<EOF
Usage: scrollkeeper-rebuilddb [-v] [-q] [-p <SCROLLKEEPER_DB_DIR>] 
EOF

    exit $1
}


# Initialize variables with default setting
scrollkeeper_db_dir=`scrollkeeper-config --pkglocalstatedir`
verbose=""
quiet=""

while getopts "p:qv" opt; do
    case $opt in
    p ) scrollkeeper_db_dir=$OPTARG ;;
    q ) quiet="-q" ;;
    v ) verbose="-v" ;;
    \? ) usage 1 ;;
    esac
done

shift `expr $OPTIND - 1`

datestring=`date +"%b %d %X"`
if [ -z "$scrollkeeper_db_dir" ]; then 
    echo "$datestring scrollkeeper-rebuilddb: Empty database path returned from scrollkeeper-config." >> @localstatedir@/log/scrollkeeper.log
    echo "$datestring scrollkeeper-rebuilddb: Cannot rebuild ScrollKeeper database." >> @localstatedir@/log/scrollkeeper.log
    if [ ! $quiet ] || [ $quiet != "-q" ]; then
        echo "scrollkeeper-rebuilddb: Empty database path returned from scrollkeeper-config."
        echo "scrollkeeper-rebuilddb: Cannot rebuild ScrollKeeper database."
    fi
    exit 1; 
else
    rm -rf $scrollkeeper_db_dir
fi

echo "$datestring scrollkeeper-rebuilddb: Rebuilding ScrollKeeper database..." >> @localstatedir@/log/scrollkeeper.log

scrollkeeper-update $quiet $verbose -p $scrollkeeper_db_dir

datestring=`date +"%b %d %X"`
echo "$datestring scrollkeeper-rebuilddb: Done rebuilding ScrollKeeper database." >> @localstatedir@/log/scrollkeeper.log

exit 0
