#!/sbin/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.
#  
# *************************************************************

. /lib/svc/share/smf_include.sh

#The start method is used for installing and updating the
#extensions. The service keeps a list
#(share/extensions/install/installed) of the extensions which were
#already installed. During installation, the bundled extensions are
#copied to the install folder (share/extensions/install). Finally this
#service, which is part of the office installation package, will be
#started and the start "method" of this script is called. Then all
#extensions in the "install" folder are checked if they are already
#installed by reading the list "installed". Because the list is empty
#at this time, all the extensions will be installed.
#
#If this service is restarted then the script checks if there is an
#extensions which is not yet installed, that is there is no entry for
#it in the 'installed' file. Only if this is the case then that
#extensions will be installed and its path is added to 'installed'.
#
#In case of an update, new versions of existing extensions and
#completely new extensions may be copied to the 'install' folder. Also
#a new 'installed' file will be copied which replaces the existing
#file. The new 'installed' file does not contain any entries of
#installed extensions. Therefore the next time when the start method is
#run all extensions contained in share/extensions/install will be
#installed.

#Create the folder which contains the temporary user installation
INSTDIR=`/usr/bin/mktemp -d "/tmp/userinstall.XXXXXX"`

OOO_BASE_DIR="/opt/openoffice/basis${OOOBASEVERSION}"

case "$1" in
'start')
    EXTENSIONDIR=/opt/openoffice${OOOBRANDPACKAGEVERSION}/share/extension/install
    for FILE in $EXTENSIONDIR/*.oxt
    do
	#We check if the file exist, because if there is no extension
	#then $FILE will contain "<..>/*.oxt"
	if [ -f "$FILE" ]; then
            #Determine if this extension is already installed. We do
	    #that by checking the file "installed" which contains a
	    #list of all installed extensions including the full path
	    EXTENSIONFILE=`basename $FILE`
	    INSTALLED=`sed -n "/$EXTENSIONFILE/p" $EXTENSIONDIR/installed`
	    
	    if [ -z "$INSTALLED" ]; then
	        #We have not found the name of the extension in the
		#list. That is, it has not been installed (with unopkg) yet.
		#Therefore we do it now.
		echo installing $FILE
		/opt/openoffice${OOOBRANDPACKAGEVERSION}/program/unopkg add --shared --bundled "$FILE" '-env:UserInstallation=file://$INSTDIR' '-env:UNO_JAVA_JFW_INSTALL_DATA=$OOO_BASE_DIR/share/config/javasettingsunopkginstall.xml' '-env:JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY=1'
		#Let us remember that this extensions has been installed
		#by adding the path name of the extension to the file 
		#installed
		echo $FILE >> $EXTENSIONDIR/installed
	    fi
	fi
    done
    
    #Now check for extensions which need to be uninstalled
    #(unopkg). This is the case if the list of extensions in the file
    #installed contains the name of an extension which does not exist
    #in the folder <..>/share/extension/install.
#     LINE=""
#     NEWCONTENT=""
#     REMOVED=""
#     LIST=`cat $EXTENSIONDIR/installed`
#     #remove blank lines
#     LIST=`echo "$LIST" | sed '/^[:blank:]*$/d'`
 
#     echo "$LIST" |  while [ 1 ]
#     do
# 	read LINE || break
# 	if [ ! -f "$LINE" ]; then
# 	    #The extension file has been removed from
# 	    #share/extension/install. Now we remove the installed
# 	    #extension
# 	    echo removing `basename $LINE`
# 	    /opt/openoffice${OOOBRANDPACKAGEVERSION}/program/unopkg remove --shared --bundled "`basename $LINE`" '-env:UserInstallation=file://$INSTDIR' '-env:UNO_JAVA_JFW_INSTALL_DATA=$OOO_BASE_DIR/share/config/javasettingsunopkginstall.xml' '-env:JFW_PLUGIN_DO_NOT_CHECK_ACCESSIBILITY=1'
# 	    REMOVED=1
# 	else
# 	    NEWCONTENT+=$LINE 
# 	    NEWCONTENT+="\n"	    
# 	fi
#     done 

#     #Write the new list to the file "installed". It now has all names
#     #remove which referred to previously removed extensions (removed
#     #from .../share/extension/install)
#     if  [ "$REMOVED" ]; then
# 	#remove the last empty line
# 	NEWCONTENT=`echo "$NEWCONTENT" | sed '/^[:space:]*$/d'`
# 	echo "$NEWCONTENT" > $EXTENSIONDIR/installed
#     fi
    
    ;;
    'stop')
	echo "#### stop ####"
	;;
    *)
	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac

exit $SMF_EXIT_OK