#!/bin/bash
#postrm script for airtime

set -e

if [ -f /usr/share/debconf/confmodule ]; then
  . /usr/share/debconf/confmodule
fi

if [ "$DPKG_DEBUG" = "developer" ]; then
    set -x
fi

package_name="airtime"
datadir="/srv/airtime"
wwwdir="/usr/share/airtime"
tmpdir="/var/lib/airtime/tmp"
configdir="/etc/airtime"

webserver="apache2"
php="php5"

case "$1" in
  purge|remove)

                # airtime uninstaller does not remove these

		if [ -L /var/lib/airtime/airtime_mvc ]; then
			rm -rf /var/lib/airtime/ || true
		fi

                if [ -f /var/lib/airtime/.htaccess ]; then
                        rm -f /var/lib/airtime/.htaccess || true
                fi

		if [ -f ${tmpdir}/install_minimal/distribute-0.6.10.tar.gz ]; then
			rm -f ${tmpdir}/install_minimal/distribute-0.6.10.tar.gz || true
		fi

                if [ -f /usr/share/python-virtualenv/distribute-0.6.10.tar.gz ]; then
                        rm -f /usr/share/python-virtualenv/distribute-0.6.10.tar.gz || true
                fi

                if [ -d ${tmpdir}/python_apps/pypo/liquidsoap_bin ]; then
                        rm -f ${tmpdir}/python_apps/pypo/liquidsoap_bin/* || true
			rm -rf ${tmpdir}/python_apps/pypo/liquidsoap_bin || true
		fi

                if [ -d /var/lib/airtime/python_apps/pypo/liquidsoap ]; then
                        rm -rf /var/lib/airtime/python_apps/pypo/liquidsoap || true
                fi

                if [ -d ${tmpdir}/install_minimal/upgrades/airtime-1.9.0/airtimefilemonitor ]; then
                        rm -rf ${tmpdir}/install_minimal/upgrades/airtime-1.9.0/airtimefilemonitor || true
                fi

                if [ -f ${tmpdir}/install_minimal/upgrades/airtime-1.9.0/storDump.txt ]; then
                        rm -f ${tmpdir}/install_minimal/upgrades/airtime-1.9.0/storDump.txt || true
                fi

                if [ -L /usr/bin/airtime-clean-storage ]; then
                        rm -f /usr/bin/airtime-clean-storage || true
                fi

                if [ -L /usr/bin/airtime-user ]; then
                        rm -f /usr/bin/airtime-user || true
                fi

                if [ -L /usr/bin/airtime-log ]; then
                        rm -f /usr/bin/airtime-log || true
                fi

    # Un-configure webservers
    if [ -L /etc/$webserver/conf.d/airtime.conf ]; then
      rm -f /etc/$webserver/conf.d/airtime.conf || true
      restart="$webserver $restart"
    fi

    if [ -L /etc/$php/conf.d/airtime.ini ]; then
      rm -f /etc/$php/conf.d/airtime.ini || true
      restart="$webserver $restart"
    fi

    if [ -f /etc/$webserver/sites-available/airtime-vhost ]; then
      a2dissite airtime-vhost &>/dev/null || true
      # TODO: if airtime-vhost is not modified -> delete it
      restart="$webserver $restart"
    fi

    servers="apache2"
    # may not exist if package was manually installed
    if [ -r /usr/share/wwwconfig-common/restart.sh ]; then
      . /usr/share/wwwconfig-common/restart.sh || true
      echo $error
    fi

    # Remove Airtime menu entry and icon
    if test -x /usr/bin/update-menus; then
      update-menus;
    fi

    # Remove legacy permission overrides
    dpkg-statoverride --list $datadir &>/dev/null && \
    dpkg-statoverride --remove $datadir || true

    # Only remove settings if purge is called as an argument
    if [ "$1" = "purge" ]; then
                        echo "Removing configuration files from /etc/airtime/" >&2
			rm -rf /etc/airtime || true
                        echo "Purging Airtime settings from debconf database" >&2
                        db_purge || true
    fi

  ;;

  upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)

  ;;

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

#DEBHELPER#

exit 0