#!/usr/bin/perl -w
# vim:ft=perl:cindent:ts=8
# 
# postinst for dwww
# $Id: dwww.postinst,v 1.8 2005-09-15 22:15:16 robert Exp $
#

use Debconf::Client::ConfModule qw(:all);

$stddocdir = '/var/www';
$stdcgidir = '/usr/lib/cgi-bin';
$dwwwdocdir = '/var/lib/dwww/html';
$cfgfile   = '/etc/dwww/dwww.conf';

version('2.0');


if ($ARGV[0] eq 'configure' or $ARGV[0] eq 'reconfigure')
{

	&ScanDwwwConfFile ( "$cfgfile" );

	# docrootdir
	$old_docdir = defined $dwwwcfg{'docrootdir'} ? canondir($dwwwcfg{'docrootdir'}) : '';
	$old_cgidir = defined $dwwwcfg{'cgidir'} ? canondir($dwwwcfg{'cgidir'}): '';

	$dwwwcfg{'docrootdir'} = canondir(scalar get('dwww/docrootdir'));
	$dwwwcfg{'cgidir'}     = canondir(scalar get('dwww/cgidir'));
	$dwwwcfg{'cgiuser'}    = get('dwww/cgiuser');
	$dwwwcfg{'serverport'} = get('dwww/serverport');
	$dwwwcfg{'servername'} = get('dwww/servername');
	
	my $do_index = (-x '/usr/bin/index++' 
		        and not -s '/var/cache/dwww/dwww.swish++.index'
			and scalar get('dwww/index_docs') eq 'true') ? 1 : 0;

	stop(); # stop debconf

	&SetupDwwwLinks ("$old_docdir", "$dwwwcfg{'docrootdir'}",
	 		 "$old_cgidir", "$dwwwcfg{'cgidir'}");

	&WriteDwwwConfFile( "$cfgfile" );

	if ( ! -d "/var/cache/dwww" ) {
		mkdir "/var/cache/dwww", 0755 or die "Cannot create directory /var/cache/dwww: $!\n";
		chown 0, 0, "/var/cache/dwww";
	} else {			
		system("rm", "-rf", "/var/cache/dwww/db");
	}
	mkdir "/var/cache/dwww/db", 0755 or die "Cannot create directory /var/cache/dwww/db: $!\n";
	my $uid = (getpwnam("$dwwwcfg{'cgiuser'}"))[2] ;
	defined $uid or die "User $dwwwcfg{'cgiuser'} does not exist\n";
	$uid or die "You DON'T want to run the dwww CGI script as root!\n";
	chown $uid, 0, "/var/cache/dwww/db";

	if ( -r "/etc/dwww/apache.conf") {
		foreach my $apache ("apache","apache-perl", "apache-ssl", "apache2") {
			my $confd="/etc/$apache/conf.d";
			symlink ("../../dwww/apache.conf", "$confd/dwww") if (-d "$confd" && ! -e "$confd/dwww");
		}
	}

	if ( -x "/etc/cron.daily/dwww" )
	{
		print STDERR "\nBuilding dwww pages in the background...\n";
		system("/etc/cron.daily/dwww &");
	}

	if ($do_index and -x "/usr/sbin/dwww-index++")	
	{	
		print STDERR "\nIndexing documentation files in the background...\n";
		system("dwww-index++ &");
	}
	
}
else
{
	stop(); # stop debconf
}


my $dh_commands="set -- @ARGV\\n" . << 'EOF';
set -e
if [ "$1" = "upgrade" ] && dpkg --compare-versions "$2" lt-nl "1.5.0"; then
    rm -f /etc/cron.daily/dwww.saved_by_dwww_preinst \
          /etc/menu-methods/dwww.saved_by_dwww_preinst
fi
#DEBHELPER#
EOF
system ($dh_commands) / 256 == 0
	or die "Problem with shell scripts: $!";

exit 0;




# SOUBROUTINES


sub SetupDwwwLinks
{
		my ($olddocdir, $newdocdir, $oldcgidir, $newcgidir) = @_;

			if ($olddocdir ne "")
			{
					unlink "$olddocdir/dwww";
			}

			if ( -d $newdocdir )
			{
					unlink "$newdocdir/dwww";
					symlink ("$dwwwdocdir", "$newdocdir/dwww")
						or die "Cannot symlink $newdocdir/dwww to $dwwwdocdir: $!";
			}

			if (($oldcgidir ne $stdcgidir) and ($oldcgidir ne ""))
			{
					unlink "$oldcgidir/dwww";
			}

			if (($newcgidir ne $stdcgidir) and ( -d $newcgidir ))
			{
					unlink "$newcgidir/dwww";
					system("cp","-p","$stdcgidir/dwww", "$newcgidir/dwww");
			}
}





# Subroutine to Scan Dwww configuration file
# Configfile to parse is the argument to the subroutine
sub ScanDwwwConfFile
{

	if ( ! -r "$_[0]" ) { return; };
	open(DWWWCONFFILE,"<$_[0]") || die "Could not open $_[0]";

    # set defaults
    #     $dwwwcfg{'serverport'}  = 80;

	while (<DWWWCONFFILE>)
	{
		# Check for DWWW_DOCROOTDIR
		if( s/^\s*DWWW_DOCROOTDIR=//)
		{
			chomp($dwwwcfg{'docrootdir'}=$_);
		}

		# Check for DWWW_CGIDIR
		if( s/^\s*DWWW_CGIDIR=//)
		{
			chomp($dwwwcfg{'cgidir'}=$_);
		}

		# Check for DWWW_CGIUSER
		if( s/^\s*DWWW_CGIUSER=//)
		{
			chomp($dwwwcfg{'cgiuser'}=$_);
		}

		# Check for DWWW_SERVERNAME
		if( s/^\s*DWWW_SERVERNAME=//)
		{
			chomp($dwwwcfg{'servername'}=$_);
			# some compatibility code: assume that everything after i
			# the last : is  a port number

			if ($dwwwcfg{'servername'} =~ /^(.*):(\d+)$/)
			{
				$dwwwcfg{'servername'} = $1;
				$dwwwcfg{'serverport'} = $2;
			}
		}


		# Check for DWWW_SERVERPORT
		if( s/^\s*DWWW_SERVERPORT=//)
		{
			chomp($dwwwcfg{'serverport'}=$_);
		}

	}
	close DWWWCONFFILE;
}



# Write /etc/dwww/dwww.conf
sub WriteDwwwConfFile
{
	# First, build up array of entries
	my $filename 	 = "$_[0]";
	my @dwwwconffile = ();


	if ( -r "$filename" )
	{

		open(DWWWCONFFILE,"< $filename") ||	die "Could not open $filename for reading";

		while (<DWWWCONFFILE>)
		{
			next if ( /^\s*DWWW_DOCROOTDIR/ ||
					  /^\s*DWWW_CGIDIR/ ||
					  /^\s*DWWW_CGIUSER/ ||
					  /^\s*DWWW_SERVERNAME/ ||
					  /^\s*DWWW_SERVERPORT/
					);
			push @dwwwconffile, "$_";
		}
		close DWWWCONFFILE;
	}

	open(DWWWCONFFILE,"> $filename") ||
			die "Could not open $filename for writing";

	foreach $i ( @dwwwconffile )
	{
		print DWWWCONFFILE "$i";
	}

	print DWWWCONFFILE "DWWW_DOCROOTDIR=$dwwwcfg{'docrootdir'}\n";
	print DWWWCONFFILE "DWWW_CGIDIR=$dwwwcfg{'cgidir'}\n";
	print DWWWCONFFILE "DWWW_CGIUSER=$dwwwcfg{'cgiuser'}\n";
	print DWWWCONFFILE "DWWW_SERVERNAME=$dwwwcfg{'servername'}\n";
	print DWWWCONFFILE "DWWW_SERVERPORT=$dwwwcfg{'serverport'}\n";

	close DWWWCONFFILE;
}


sub canondir
{
	my $dir = $_[0];
	
	$dir =~ s|/+|/|g;
	$dir =~ s|/$||;
	return $dir;
}


