#!/bin/sh -e

# postinst for Echolot

. /usr/share/debconf/confmodule

# Make sure the echolot user exists
getent group echolot >/dev/null 2>&1 || (
	echo Adding echolot group
	addgroup echolot )
getent passwd echolot >/dev/null 2>&1 || (
	echo Adding echolot user
	adduser --system --shell /bin/bash --home /var/lib/echolot --ingroup echolot --gecos "Echolot Pinger" echolot )

# Give the echolot user write permissions to /var/log/echolot/echolot.log
touch /var/log/echolot/echolot.log
chown echolot.adm /var/log/echolot/echolot.log
chmod 640 /var/log/echolot/echolot.log
# and /var/lib/echolot
if ( ! dpkg-statoverride --list /var/lib/echolot > /dev/null ); then
	dpkg-statoverride --update --add root echolot 02775 /var/lib/echolot
fi
# and /var/run/echolot
if ( ! dpkg-statoverride --list /var/run/echolot > /dev/null ); then
	dpkg-statoverride --update --add root echolot 02770 /var/run/echolot
fi


########################################################################
# User configuration
########################################################################
if [ "$1" = "configure" ]; then

	########################
	# Echolot configuration

	filename=`tempfile`;
cat > $filename << 'EOF'
#!/usr/bin/perl -w

		use strict;
		use Debconf::Client::ConfModule;
		use IO::File;
		
		my $postblock = "";
		my $preblock = "";

		if ( -e "/etc/echolot/pingd.conf" ) {
			my $fh = new IO::File;
			$fh->open("/etc/echolot/pingd.conf") or
				die ("Cannot open /etc/echolot/pingd.conf: $!");
			while (<$fh>) {
				last if (/^### BEGIN DEBCONF SECTION/);
				$preblock .= $_;
			};
			while (<$fh>) {
				last if (/^### END DEBCONF SECTION/);
			};
			while (<$fh>) {
				$postblock .= $_;
			};
			$fh->close() or
				die ("Cannot close /etc/echolot/pingd.conf: $!");
		};

		my $address = Debconf::Client::ConfModule::get("echolot/address");
		my $operator = Debconf::Client::ConfModule::get("echolot/operator");
		my $sitename = Debconf::Client::ConfModule::get("echolot/sitename");

		my ($local, $domain) = split (/@/, $address, 2);

		my $debconf  = sprintf("\$CONFIG->{q{my_localpart}    } = q{%s};\n", $local);
		   $debconf .= sprintf("\$CONFIG->{q{my_domain}       } = q{%s};\n", $domain);
		   $debconf .= sprintf("\$CONFIG->{q{operator_address}} = q{%s};\n", $operator);
		   $debconf .= sprintf("\$CONFIG->{q{sitename}        } = q{%s};\n", $sitename);

		my $fh = new IO::File;
		$fh->open(">/etc/echolot/pingd.conf") or
			die ("Cannot open /etc/echolot/pingd.conf: $!");
		print $fh $preblock .
		          "### BEGIN DEBCONF SECTION\n" .
				  $debconf .
		          "1;\n" .
		          "### END DEBCONF SECTION\n" .
		          $postblock or
			die ("Cannot write to /etc/echolot/pingd.conf: $!");
		$fh->close or
			die ("Cannot close /etc/echolot/pingd.conf: $!");
EOF
	chmod +x $filename;
	$filename
	rm $filename
fi

#DEBHELPER#

########################################################################
# Configuration reprise
########################################################################
if [ "$1" = "configure" ]; then
    db_get echolot/addnow || true
    if [ "$RET" = "true" ]; then
		/etc/init.d/echolot add REMAILER_LIST
        # But don't do it again unless the user asks for it
        db_set echolot/addnow false
    fi
fi


db_stop

# vim:set ts=4:
# vim:set shiftwidth=4: