#!/bin/bash

echo "WARNING!!"
echo "All your LDAP data and the Zentyal modules depending on it are going to be erased."
echo "Press Control-C now if you are not sure of what you are doing."
read

INSTALLED_MODULES=`dpkg -l | grep 'ebox-' | grep ^ii | cut -d' ' -f3| xargs`

UNCONFIGURE_MODULES="asterisk egroupware ftp jabber mail samba squid webmail zarafa users"

for mod in $UNCONFIGURE_MODULES
do
    (echo $INSTALLED_MODULES | grep -q $mod) || continue
    echo "Unconfiguring module $mod..."
    sudo /usr/share/ebox/ebox-unconfigure-module $mod
done

sudo apt-get -y --force-yes remove --purge slapd
sudo rm -rf /var/lib/ldap* /etc/ldap*

echo "Removal complete. Reinstalling the removed Zentyal modules..."

sudo apt-get -y --force-yes install $INSTALLED_MODULES || exit 1

echo "Done."
echo "Now you can go to 'Users and Groups -> Mode' and configure it again."
