#!/usr/bin/perl
#
# DebConf 'config' script for wu-ftpd
# Written by Chris Butler <chrisb@debian.org>
#

use Debconf::Client::ConfModule ':all';
use strict;

my $homedir;
my $ftpusers = "/etc/ftpusers";

input("medium", "wu-ftpd/run_mode");
if (-l "$ftpusers") {
	subst("wu-ftpd/ftpusers-symlink", "target", readlink("$ftpusers"));
	subst("wu-ftpd/ftpusers-symlink", "ftpusers", $ftpusers);
	input("high", "wu-ftpd/ftpusers-symlink");
}
input("medium", "wu-ftpd/anonymous");
go();


if (get("wu-ftpd/anonymous") eq "true") {
	if ( -f "$ftpusers" ) {
	        my $found_ftpuser = 0;
		open (FFTPUSERS, "<$ftpusers");
		while (<FFTPUSERS>) {
			/^(ftp|anonymous)$/ && do {
				subst("wu-ftpd/ftpusers", "ftpusers", $ftpusers);
				input("medium", "wu-ftpd/ftpusers");
				$found_ftpuser = 1;
				last;
			};
		}
		close(FFTPUSERS);
		unless ($found_ftpuser) { set("wu-ftpd/ftpusers", "false"); }
	}
	my $homedir_exists = 0;
	setpwent;
	if ($homedir = (getpwnam("ftp"))[7]) {
		if (-d $homedir) {
			set("wu-ftpd/homedir", $homedir);
			input("low", "wu-ftpd/homedir");
			input("medium", "wu-ftpd/update-binaries") if (checkbin($homedir, <$homedir/bin/*>));
			input("medium", "wu-ftpd/create-incoming");
			go();
			exit(0);
		} else {
			subst("wu-ftpd/home-noexist", "homedir", $homedir);
			input("medium", "wu-ftpd/home-noexist");
			go();

			if (get("wu-ftpd/home-noexist") eq "true") {
				set("wu-ftpd/homedir", $homedir);
				$homedir_exists = 1;
			} else {
				exit(0);
			}
		}
	}
	endpwent;
	
	while(1) {
		input("medium", "wu-ftpd/homedir") unless $homedir_exists;
		$homedir_exists = 0;
		my @ret = go();

		last if ($ret[0] == 30);
		
		$homedir = get("wu-ftpd/homedir");
		$homedir =~ s/\s+//g;    # remove spaces ...
		$homedir =~ s#//+#/#g;   # we don't want things like //
		$homedir =~ s#/$##;      # remove trailing slash if it exists

		if (!($homedir =~ m#^/#)) {
			input("medium", "wu-ftpd/homedir-not-absolute");
			go();
			redo;
		}
		if (-d "$homedir") {
			subst("wu-ftpd/homedir-exists", "homedir", $homedir);
			input("medium", "wu-ftpd/homedir-exists");
			go();

			redo unless (get("wu-ftpd/homedir-exists") eq "true");
		}
		last;
	}

	input("medium", "wu-ftpd/update-binaries") if (checkbin($homedir, <$homedir/bin/*>));
	input("medium", "wu-ftpd/create-incoming");
	input("low", "wu-ftpd/libnss");

	go();
}

sub getlibs {
	my $file;
	my %libs;
	foreach $file (@_) {
		foreach (`ldd $file`) { chomp;
			my $lib;
			/\s=>\s/ or next;
			($lib) = /.*=>\s(.*?)\s/;
			$libs{$lib} = 1;
		}
	}
	return sort keys %libs;
}

# Checks $homedir/bin/* to see if they're up-to-date
sub checkbin {
	my ($ftphome, @testbins) = @_;
	my @dynlinker = ();
	my (%bins, $errors);
	
	foreach (@testbins) {
		my ($src, $dst);
		$dst = $_; /^.*\/(.*)/; $src = "`which $dst`";
		-r $src and $bins{$src} = $dst;
	}

	foreach (keys %bins) {
		my ($src, $dst) = $_, $bins{$_};
		my ($srcmd5, $dstmd5);

		$srcmd5 = (split /\s/, `md5sum $src`, 2)[0];
		$dstmd5 = (split /\s/, `md5sum $dst`, 2)[0];

		($srcmd5 eq $dstmd5) and next;
		$errors++;
	}
	$errors and return 1;

	foreach (getlibs(keys %bins)) {
				/ld-linux/ and push @dynlinker, "$ftphome$_";
				-f "$ftphome$_" or $errors++;
	}
	$errors and return 1;

	return 0;
}

