#!/bin/bash
#
# xpcomp_updatecache - build a binary cache from a bunch of completion
#                      script snippets in a directory
#
#  Copyright (C) 2001 Ingo K"ohne
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

set -e

# This file is generated by ./configure
# Substitutions were made in the following four lines:
prefix=/usr
exec_prefix=${prefix}
xpcomp_so=${XPCOMP_SO:-${exec_prefix}/lib/xpcomp/xpcomp.so}
scriptdir=${prefix}/share/xpcomp/completions
cache=/var/cache/xpcomp/cache

OPTIND=1
while getopts "X:c:C:v" option
do
    case "$option" in
    X) xpcomp="$OPTARG" ;;
    c) scriptdir="$OPTARG" ;;
    C) cache="$OPTARG" ;;
    v) verbose=1 ;;
    esac
done

tmpcache=$cache.tmp$$

if test ! -d $(dirname $cache); then
    echo Creating $(dirname $cache)
    mkdir -p -m755 $(dirname $cache)
fi

enable -f $xpcomp_so xpcomp
enable -f $xpcomp_so xpcompcore
enable -f $xpcomp_so optcomplete

oldpwd=$(pwd)
cd $scriptdir
for script in [a-z]*
do
    if test -r $script -a -f $script; then
	source ./$script
    fi
done
cd $oldpwd

if test -f $tmpcache; then
    echo "File $tmpcache already exists" >&2
else
    xpcomp -w $tmpcache
    xpcomp -X
    mv -f $tmpcache $cache
    chmod a-wx $cache
fi

set +e
