#!/bin/sh
#
# Copyright 1999, TaBE Project, All Rights Reserved.
# Copyright 1999, Pai-Hsiang Hsiao, All Rights Reserved.
#
# $Id: tsipackdb.in,v 1.5 2001/11/11 12:33:09 thhsieh Exp $
#

PATH=@with_db_bin@
USE_DB=@USE_DB@

DB2_VER='6	Btree version number.'
DB3_VER='8	Btree version number.'
path_list="`echo $PATH | sed 's/:/ /g'`"
export PATH path_list

if [ "$#" != 1 ]; then
    echo "Usage: tsipackdb <filename.db>"
    exit 0
fi

mywhich () {
    for p in $path_list; do
	if [ -x $p/$1 ]; then
	    echo $p/$1
	    break
	fi
    done
}

pack_db_file () {
    db_stat=`mywhich ${1}_stat`
    db_dump=`mywhich ${1}_dump`
    db_load=`mywhich ${1}_load`
    test x$db_stat != x && test x$db_dump != x && test x$db_load != x && \
	$db_stat -d $2 2>&1 | (
	    read line
	    read line
	    test "$line" = "$3" && \
		echo "Using $db_dump and $db_load to pack $2 ...." && \
		${1}_dump $2 | ${1}_load $2.new && \
		mv $2.new $2 && \
		return 0
	    return -1 \
	)
}


if [ "$USE_DB" = "DB2" ]; then
    pack_db_file db  $1 "$DB2_VER" && exit 0
    pack_db_file db2 $1 "$DB2_VER" && exit 0
fi

if [ "$USE_DB" = "DB3" ]; then
    pack_db_file db  $1 "$DB3_VER" && exit 0
    pack_db_file db3 $1 "$DB3_VER" && exit 0
fi

exit 0
