#!/bin/sh

set -e

remove_gns() {
  # abort if /etc/nsswitch.conf does not exist
  if ! [ -e /etc/nsswitch.conf ]; then
    return
  fi
  perl -i -pe '
		my @remove=(
			"gns [NOTFOUND=return]",
		);
		sub remove {
			my $s=shift;
			foreach my $bit (@remove) {
				$s=~s/\s+\Q$bit\E//g;
			}
			return $s;
		}
		s/^(hosts:)(.*)/$1.remove($2)/e;
	' /etc/nsswitch.conf
}

_USERNAME="gnunet"
_GROUPNAME="gnunet"
GNUNETDNS_GROUP="gnunetdns"
if [ -e /usr/share/debconf/confmodule ]; then
  . /usr/share/debconf/confmodule
  db_version 2.0

  db_get gnunet-dns/libnsswitch
  _LIBNSSWITCH="${RET}"
fi

case "${1}" in
purge)
  if $_LIBNSSWITCH; then
    rm -rf /usr/lib/x86_64-linux-gnu/usr/libnss_gns.so.2
    rm -rf /usr/lib/x86_64-linux-gnu/usr/libnss_gns4.so.2
    rm -rf /usr/lib/x86_64-linux-gnu/usr/libnss_gns6.so.2
    remove_gns
  fi

  rm -rf /var/log/gnunet.log /var/lib/gnunet /etc/default/gnunet
  ;;

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

  if $_LIBNSSWITCH; then
    rm -rf /usr/lib/x86_64-linux-gnu/usr/libnss_gns.so.2
    rm -rf /usr/lib/x86_64-linux-gnu/usr/libnss_gns4.so.2
    rm -rf /usr/lib/x86_64-linux-gnu/usr/libnss_gns6.so.2
    remove_gns
  fi

  ;;

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

#DEBHELPER#

exit 0
