#!/bin/sh -e

# Source debconf library.
. /usr/share/debconf/confmodule

OCTO="octopussy"
GROUPDEL="/usr/sbin/groupdel"
RM="/bin/rm"
UPDATERCD="/usr/sbin/update-rc.d"
USERDEL="/usr/sbin/userdel"

#
# Delete Octopussy MySQL Database
#
db_get octopussy/mysql_root_password || RET=""
if [ "$RET" != "" ] && [ -x /usr/bin/mysql ]; then
        /usr/bin/mysql -u root --password=$RET --exec="DROP DATABASE IF EXISTS $OCTO" || true
fi

#
# Remove octo_logrotate from cron.daily
#
CRON_FILE="/etc/cron.daily/octo_logrotate"

if [ -x "$CRON_FILE" ]; then
	$RM -f $CRON_FILE || true
fi

#
# Remove init files
#
if [ -x "/etc/init.d/$OCTO" ]; then
	$UPDATERCD -f $OCTO remove || true
	$RM -f /etc/init.d/$OCTO || true
fi

#
# Remove Octopussy directories & files
#
if [ "$1" = "purge" ]; 
then
	$RM -rf /etc/aat/ || true
	$RM -rf /etc/$OCTO/ || true
	$RM -rf /usr/share/aat/ || true
	$RM -rf /usr/share/$OCTO/ || true
	$RM -rf /usr/share/perl5/AAT* || true
	$RM -rf /usr/share/perl5/Octopussy* || true
  	$RM -rf /var/cache/$OCTO/ || true
  	$RM -rf /var/lib/$OCTO/ || true
  	$RM -rf /var/run/aat/ || true
  	$RM -rf /var/run/$OCTO/ || true
  	$RM -rf /var/spool/$OCTO/ || true
	# Removes Octopussy user & group 
	$GROUPDEL $OCTO || true
	$USERDEL $OCTO || true
	# Remove my changes to the db.
  	db_purge
fi

exit 0
