#!/bin/sh

usage()
{
    echo "This script regerenates the system OOo UNO package cache"
    echo
    echo "Usage: ${0##*/} [--help] [--force] ooo_home [broken_extension...]"
    echo
    echo "Options:"
    echo
    echo "	--help 		 this help"
    echo "	--force		 regenerate the cache without checking against the list of broken extensions"
    echo "	ooo_home	 path where OOo is installed, e.g. /usr/lib/ooo3"
    echo "	broken_extension list of broken extensions, e.g. LanguageTool-0.9.3.oxt"
}

force=
ooo_home=
ooo_broken_extensions=
while test -n "$1" ; do
    case "$1" in
	--help)
	    usage
	    exit 0;
	    ;;
	--force)
	    force=1
	    ;;
	-*)
	    echo "Error: unknown option: $1"
	    exit 1;
	    ;;
	*)
	    if test -z "$ooo_home" ; then
		ooo_home="$1"
		if ! test -d "$ooo_home" ; then
		    echo "Error: the OOo home direcotry does not exists: $ooo_home"
		    exit 1;
		fi
	    else
		ooo_broken_extensions="$ooo_broken_extensions $1"
	    fi
	    ;;
    esac
    shift
done

if test -z "$ooo_home" ; then
    echo "Error: Please define the ooo_home, try --help"
    exit 1;
fi

ooo_uno_cache="$ooo_home/share/uno_packages/cache"

# nothing to do if the cache does not exist (fresh installation or so)
test -d "$ooo_uno_cache" || exit 0;

# check for broken extensions
found=
if test -z "$force" ; then
    for extension in $ooo_uno_cache/uno_packages/*/* ; do
	extension_name=`basename $extension`
	if echo "$ooo_broken_extensions" | grep -q "$extension_name" ; then
	    echo "Need to regenerate the uno cache because of the broken extension $extension_name."
	    found=1
	    break
	fi
    done
fi

if test -n "$force" -o -n "$found" ; then
    # saving sources of the registered extensions
    temp_cache=`mktemp -d $ooo_uno_cache.XXXXXX`
    for extension in $ooo_uno_cache/uno_packages/*/* ; do
	extension_name=`basename $extension`
	echo "Saving $extension_name..."
	if test -f $extension ; then
	    cp $extension $temp_cache || exit 1;
	elif test -d $extension && cd $extension ; then
	    zip -q -r $temp_cache/$extension_name * || exit 1;
	    cd - >/dev/null 2>&1
	fi
    done

    echo "Removing the cache"
    rm -rf $ooo_uno_cache/*

    # registering the good extensions once again
    for extension in $temp_cache/* ; do
	extension_name=`basename $extension`
	if echo "$ooo_broken_extensions" | grep -q "$extension_name" ; then
	    echo "Skipping unusable $extension_name..."
	    continue;
	fi
	if test ! -f $extension ; then
	    echo "Error: is not a file: $extension"
	    continue;
	fi
	echo "Registering $extension_name..."
	unopkg add --shared --force $extension || true
    done
    rm -rf $temp_cache
fi
