#!/bin/sh
# postinst script for salt-ooo-addons
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


clean_old_salts()
{
	FILES_TO_FURGATE="salt.zip saltutil.zip"
	DIRECTORY_IS="/var/spool/libreoffice/uno_packages/cache/uno_packages/"
	
	# Workaround to solve some upgrade problems
	# test if directory exists and contains some files.
	
	if [ -d "$DIRECTORY_IS"	] ; then

		# If the files and directory exists:
		for f in $FILES_TO_FURGATE ; do
        		AUX=$(find $DIRECTORY_IS -xtype d -name "$f")
			if [ -n "$AUX" ] ; then
		        	DIRTY_FILE="$(dirname "$AUX" | sed "s%_$%%")" 
        			rm -rf "$DIRTY_FILE" "$(dirname "$AUX"/)"
			fi
        		echo "$f is cleaning"
		done
	fi
}


case "$1" in
    configure)
	echo "Configure del nuevo"
	#clean_old_salts
	#/usr/lib/libreoffice/program/unopkg add -v --shared /usr/share/salt-ooo-addons/salt.zip
	#/usr/lib/libreoffice/program/unopkg add -v --shared /usr/share/salt-ooo-addons/saltutil.zip
    ;;
    abort-remove)
	echo "Abort remove del nuevo"
	clean_old_salts
	;;
    abort-upgrade|abort-deconfigure)
	echo "Abort upgrade del nuevo"
	#clean_old_salts
	#/usr/lib/libreoffice/program/unopkg remove -v --shared salt.zip|| true
        #/usr/lib/libreoffice/program/unopkg remove -v --shared saltutil.zip || true

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


