#! /bin/bash

set -e

if [ $# != 0 ]; then
    cat 1>&2 <<-EOF
	rebuild-gcj-db: re-build the gcj classmap database
	
	usage: $0
	EOF
    exit 1
fi


rebuild_db()
{
    dbtool=$1; shift
    dbLocation=$1; shift
    dirs=

    for dir; do [ -d $dir ] && dirs="$dirs $dir"; done
    mkdir -p $(dirname $dbLocation)
    $dbtool -n $dbLocation.tmp 64
    find $dirs -follow -name '*.db' -print0 | \
	xargs -r -0 $dbtool -m $dbLocation.tmp $dbLocation.tmp || exit 1
    mv $dbLocation.tmp $dbLocation
}


rebuild_databases()
{
    v=$1
    dbtool=gcj-dbtool-$1
    dbLocation=`$dbtool -p`

    rebuild_db \
	$dbtool \
	$dbLocation \
	/usr/share/gcj/classmap.d \
	/usr/share/gcj-$v/classmap.d
}

# still two different databases for gcj-4.1 and gcj-4.2

if [ -x /usr/bin/gcj-dbtool-4.1 ]; then
    rebuild_databases 4.1
fi

if [ -x /usr/bin/gcj-dbtool-4.2 ]; then
    rebuild_databases 4.2
fi
