#!/bin/bash

set -e

case "$1" in
    remove)
        service mpfs-core-uwsgi-disk stop || true
        service mpfs-queue-disk stop || true
        service mpfs-queue-index stop || true

        if [ -L /etc/init.d/mpfs-core-uwsgi-disk ]; then
            update-rc.d -f mpfs-core-uwsgi-disk remove
            rm /etc/init.d/mpfs-core-uwsgi-disk
        fi

        if [ -L /etc/init.d/mpfs-queue-disk ]; then
            update-rc.d -f mpfs-queue-disk remove
            rm /etc/init.d/mpfs-queue-disk
        fi

        if [ -L /etc/init.d/mpfs-queue-index ]; then
            update-rc.d -f mpfs-queue-index remove
            rm /etc/init.d/mpfs-queue-index
        fi

        if [ -L /etc/init.d/mpfs-queue-photoslice ]; then
            update-rc.d -f mpfs-queue-photoslice remove
            rm /etc/init.d/mpfs-queue-photoslice
        fi

        if [ -L /etc/init.d/mpfs-queue-minor ]; then
            update-rc.d -f mpfs-queue-minor remove
            rm /etc/init.d/mpfs-queue-minor
        fi
        [ -x /etc/init.d/nginx ] && ( nginx -t -c /etc/nginx/nginx.conf && service nginx restart )

        # pyclean
        find /usr/share/pyshared/mpfs/ -name '*.pyc' -delete
        find /usr/lib/python2.7/dist-packages/mpfs/ -name '*.pyc' -delete

        rm /usr/lib/python2.7/dist-packages/mpfs
        rm /usr/lib/python2.7/dist-packages/python_mpfs_disk-0.1.egg-info || true
    ;;
    purge|upgrade|failed-upgrade|abort-remove|abort-upgrade|disappear)
        echo "postrm called with argument: '$1'" >&2
        # pyclean
        find /usr/share/pyshared/mpfs/ -name '*.pyc' -delete
        find /usr/lib/python2.7/dist-packages/mpfs/ -name '*.pyc' -delete
    ;;
    *)
        echo "postrm called with unknown argument '$1'" >&2
        exit 0
    ;;
esac
