#! /bin/sh

prefix=/usr
localstatedir=/var/gnome

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

    exit $1
}

exit_error()
{
     echo "File $1 is not a scrollkeeper data file." \
          "Please remove it to continue"
     exit 1
}

check_files()
{

dbdir=$1

scrollkeeper_files="C de es fr it sv ja ko zh_CN zh_TW index TOC scrollkeeper_docs"

# Check if files other than scrollkepeer installed ones exist

for file in `/bin/ls $dbdir/`
do
     scrollkeeper_file=0

     for filedir in `echo $scrollkeeper_files`
     do
         if [ $file = $filedir ] ; then
            scrollkeeper_file=1
            break
         fi
     done

     if [ $scrollkeeper_file -eq 0 ] ; then
           exit_error $dbdir/$file
     fi
done

# Check if locales other than the ones specified below exists

locales="C de es fr it sv ja ko zh_CN zh_TW"

for localedir in `echo $locales`
do
        if [ -d $dbdir/$localedir ]
        then
            for file in `/bin/ls $dbdir/$localedir`
            do
                 if [ $file != 'scrollkeeper_cl.xml' -a $file != 'scrollkeeper_extended_cl.xml' ]
                 then
                     exit_error $dbdir/$localedir/$file
                 fi
            done
        fi
done

# Check if index and TOC dirs contain file patterns other than [0-9]

otherdirs="index TOC"

for filedir in `echo $otherdirs`
do
    if [ -d $dbdir/$filedir ]
    then
         for file in `/bin/ls $dbdir/$filedir | /bin/grep "[^0-9]"`
         do
            exit_error $dbdir/$filedir/$file
         done
    fi
done

rm -f  $dbdir/scrollkeeper_docs
rm -rf $dbdir/index
rm -rf $dbdir/TOC

for localedir in `echo $locales`
do
    rm -rf $dbdir/$localedir
done

}

# Initialize variables with default setting
scrollkeeper_db_dir=`/usr/bin/scrollkeeper-config --pkglocalstatedir`
verbose=""
quiet=""
parg=0
omfarg=""
omfdir=""

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


#changes starts
GNOME_LOCALEDIRS="C de es fr it sv ja ko zh_CN zh_TW"

SCRKP_TEMPLATEDIR="`/usr/bin/scrollkeeper-config --pkgdatadir`/Templates"
SCRKP_LOCALSTATEDIR="`/usr/bin/scrollkeeper-config --localstatedir`"
SCRKP_LOGDIR=$SCRKP_LOCALSTATEDIR/log
SCRKP_DBDIR=$scrollkeeper_db_dir
#changes end

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." >> /var/gnome/log/scrollkeeper.log
    echo "$datestring scrollkeeper-rebuilddb: Cannot rebuild ScrollKeeper database." >> /var/gnome/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
       if [ $parg -eq 1 ] ; then
             # user has provided the -p option. determine if we can install in the given path
             check_files $scrollkeeper_db_dir
        else
           rm -rf $scrollkeeper_db_dir
        fi
fi

echo "$datestring scrollkeeper-rebuilddb: Rebuilding ScrollKeeper database..." >> /var/gnome/log/scrollkeeper.log

#changes starts
#scrollkeeper-update $quiet $verbose -p $scrollkeeper_db_dir $omfarg $omfdir
    if [ ! -f $SCRKP_LOGDIR ]; then
        mkdir -p $SCRKP_LOGDIR
        touch $SCRKP_LOGDIR/scrollkeeper.log
    fi

    if [ ! -d $SCRKP_DBDIR/TOC ]; then
        mkdir -p "$SCRKP_DBDIR/TOC"
        mkdir -p "$SCRKP_DBDIR/index"
    fi
 
    for localedir in $GNOME_LOCALEDIRS; do
        lang=$localedir
        [ $lang = zh_CN ] && lang=zh

       # skip non-existent locales
        [ $lang = "C" -o -d /usr/lib/locale/$lang ] || continue

        # skip non-existent doc pkgs
        [ -f "$SCRKP_TEMPLATEDIR/$localedir/scrollkeeper_cl.xml" ] || continue

        if [ ! -f "$SCRKP_DBDIR/$localedir/scrollkeeper_cl.xml" ]; then
            mkdir -p "$SCRKP_DBDIR/$localedir"
           cp "$SCRKP_TEMPLATEDIR/$localedir/scrollkeeper_cl.xml" \
                "$SCRKP_DBDIR/$localedir/scrollkeeper_cl.xml"
            cp "$SCRKP_TEMPLATEDIR/$localedir/scrollkeeper_cl.xml" \
                "$SCRKP_DBDIR/$localedir/scrollkeeper_extended_cl.xml"
        fi
        env LC_ALL="$lang" scrollkeeper-update $quiet $verbose -p $scrollkeeper_db_dir $omfarg $omfdir

    done

#changes end

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

exit 0
