#!/bin/bash

set -e

if [ "$1" = "upgrade" ]; then
	exit 0
fi

if [ -f /usr/share/debconf/confmodule ]; then
	. /usr/share/debconf/confmodule
	db_version 2.0 || [ $? -lt 30 ]

  rm -f /usr/share/ldap-account-manager/sess
  rm -f /usr/share/ldap-account-manager/tmp
  rm -f /usr/share/ldap-account-manager/config

  set +e
	db_get "ldap-account-manager/config-webserver"
	if [ $? -eq 0 ]; then
		set -e
		webservers="$RET"
		restart=""
		for server in $webservers; do
				server=${server%,}
				case "$server" in
						apache2)
								set +e
								test -x /usr/sbin/a2query && test -x /usr/sbin/a2disconf || continue
								a2query -q -c ldap-account-manager && a2disconf -q ldap-account-manager
								set -e
								rm -f /etc/$server/conf-available/ldap-account-manager.conf
								test -x /usr/sbin/$server || continue
								restart="$restart $server"
								;;
				esac
		done
	fi

	set +e
	db_get "ldap-account-manager/restart-webserver"
	if [ $? -eq 0 ] && [ "$RET" = "true" ]; then
		set -e
		for server in $restart; do
				server=${server%,}
				if [ `which invoke-rc.d` ]; then
          set +e
					invoke-rc.d $server reload
          set -e
				fi
		done
	fi

	# 3rd party libs
	jsThirdPartyLibs='ckeditor jstree'
	for jsThirdPartyLib in $jsThirdPartyLibs; do
		if [ -L /usr/share/ldap-account-manager/templates/lib/extra/${jsThirdPartyLib} ] ; then
			rm /usr/share/ldap-account-manager/templates/lib/extra/${jsThirdPartyLib}
		fi
	done
  if [ -L /usr/share/ldap-account-manager/lib/3rdParty/composer/phpseclib/phpseclib/phpseclib ] ; then
	  rm /usr/share/ldap-account-manager/lib/3rdParty/composer/phpseclib/phpseclib/phpseclib
  fi
  if [ -L /usr/share/ldap-account-manager/lib/3rdParty/composer/monolog/monolog/src/Monolog ] ; then
	  rm /usr/share/ldap-account-manager/lib/3rdParty/composer/monolog/monolog/src/Monolog
  fi
  if [ -L /usr/share/ldap-account-manager/lib/3rdParty/composer/psr/log/Psr ] ; then
	  rm /usr/share/ldap-account-manager/lib/3rdParty/composer/psr/log/Psr
  fi
  if [ -L /usr/share/ldap-account-manager/lib/3rdParty/composer/voku/portable-ascii/src/voku ] ; then
	  rm /usr/share/ldap-account-manager/lib/3rdParty/composer/voku/portable-ascii/src/voku
  fi
  if [ -L /usr/share/ldap-account-manager/lib/3rdParty/composer/phpmailer/phpmailer ] ; then
	  rm /usr/share/ldap-account-manager/lib/3rdParty/composer/phpmailer/phpmailer
  fi


	set -e
	if [ "$1" = "purge" ]; then
		rm -r -f /usr/share/ldap-account-manager
		rm -r -f /var/lib/ldap-account-manager
		db_purge
	fi

	#DEBHELPER#

	db_stop

fi

exit 0
