#!/bin/sh

logger -p local6.notice -t installer "app-gateway-management - uninstalling Gateway Management"

# Disable DNSthingy
/etc/dnsthingy/status disable >/dev/null 2>&1

# Remove init scripts
chkconfig --del dnsthingymgr >/dev/null 2>&1
chkconfig --del dnsthingyipe >/dev/null 2>&1

# Remove added files
rm -f /etc/cron.d/app-dnsthingy >/dev/null 2>&1
rm -f /etc/dnsmasq.d/app-dnsthingy.conf >/dev/null 2>&1
rm -f /etc/dnsthingy/app.conf >/dev/null 2>&1
rm -f /etc/dnsthingy/dnsthingyipe.conf >/dev/null 2>&1
rm -f /etc/dnsthingy/dnsthingymgr.conf >/dev/null 2>&1
rm -f /etc/dnsthingy/dh.pem >/dev/null 2>&1
rm -f /etc/dnsthingy/dt_cert.pem >/dev/null 2>&1
rm -f /etc/dnsthingy/privkey.pem >/dev/null 2>&1
rm -f /etc/dnsthingy/signature.pk >/dev/null 2>&1
rm -f /etc/dnsthingy/signature.sk >/dev/null 2>&1

# Remove logs
rm -f /var/log/dnsthingymgr.log >/dev/null 2>&1
rm -f /var/log/dnsthingyipe.log >/dev/null 2>&1

# Remove the checkalive script conf
rm -f /tmp/dnsthingycheck.conf >/dev/null 2>&1

# Kill the services
if [ -e /etc/init.d/dnsthingymgr ]; then
    /etc/init.d/dnsthingymgr stop >/dev/null 2>&1
fi
if [ -e /etc/init.d/dnsthingyipe ]; then
    /etc/init.d/dnsthingyipe stop >/dev/null 2>&1
fi

# remove sudoers modification
sed -i '\|^Cmnd_Alias CLEARSYNC =| s|, /etc/dnsthingy/status||' /etc/sudoers

# remove hosts entries
sed -i '/mybox\.management/d' /etc/hosts

# Restart services
/sbin/service clearsync restart >/dev/null 2>&1
/sbin/service dnsmasq restart >/dev/null 2>&1
/sbin/service firewall restart >/dev/null 2>&1
