#!/bin/sh
# *************************************************************
#  
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#  
# *************************************************************

USAGE="Usage: $0"

SCRIPTNAME=`basename "$0"`
PROGRAMDIR=`dirname "$0"`
OFFICEDIR="$PROGRAMDIR/.."
EXTENSIONDIR=$OFFICEDIR/share/extension/install
UNOPKG=$PROGRAMDIR/unopkg

help()
{
    echo 
    echo "Uninstallation script for office extensions located in <office>/share/extension/install"
    echo 
    echo "This uninstallation script can be executed after successful installation of packages."
    echo "Please execute this script, before uninstallation of packages."
    echo "Usage: $0"
    echo "No parameter required."
    echo
}

#
# This script is only for root installations
# (How about installations done with user privileges?)
#

# if [ $UID -ne 0 ]
# then
#     printf "\nThis script is for installation only wiht administrative rights only\n"
#     help
#     exit 2
# fi

#
# Checking existence of unopkg in program directory
#

if [ ! -f "$UNOPKG" ]; then
    echo "Error: File $UNOPKG does not exist"
    exit 1
fi

if [ ! -x "$UNOPKG" ]; then
    echo "Error: File $UNOPKG is not an executable file"
    exit 1
fi

#
# Collecting all files located in share/install/extensions
#

FILELIST=`find $EXTENSIONDIR -type f -name "*.oxt" -print`

if [ -z "$FILELIST" ]
then
    printf "\n$0: No extensions found in $EXTENSIONDIR\n"
    exit 2
fi

echo
echo "Uninstalling:"
for i in $FILELIST; do
    echo `basename $i`
done
echo

for i in $FILELIST; do
    COMMAND="$UNOPKG remove --shared `basename $i`"
    echo $COMMAND
    $COMMAND
done

echo
echo "Uninstallation done ..."
echo

exit 0