#!/usr/bin/perl
#
# Slrn configurator program. By Joey Hess, <joeyh@master.debian.org>

# Get a yes or no response from the user.
sub AskYN { my ($prompt,$default)=@_;
	$default=lc($default);
	print $prompt;
	my $yn="[y/n] ";
	$yn=~s/$default/uc($default)/e;
	print $yn;

	my $resp=lc(substr(<>,0,1));

	while (($resp ne 'y' && $resp ne 'n' && $resp ne "\n") ||
	       ($resp eq "\n" && !$default)) {

		print "Please answer 'y' or 'n'.\n";
		print $prompt.$yn;

		$resp=lc(substr(<>,0,1));
	}

	print "\n";

	if ($resp eq "\n") {
		return lc($default);
	}
	else {
		return $resp;
	}
}

# Get a numeric response from the user.
sub AskNumeric { my ($prompt,$max)=@_;
	print $prompt."[".(join '/',(1..$max))."] ";
	my $resp=int(substr(<>,0,1));

	while (!$resp || $resp > $max) {
		print "Please answer with a number from 1 to $max\n";
		print $prompt."[".(join '/',(1..$max))."] ";
		$resp=int(substr(<>,0,1));
	}

	print "\n";

	return $resp;
}

# Remove slrn modified sections from /etc/ppp/ip-up.
# This is left in (forever) for backwards compatability with old versions
# of this script that modified /etc/ppp/ip-up. Now we use the ip-ip.d
# directory, instead.
sub FixIpUp {
	return if ! -f "/etc/ppp/ip-up";

	open (IPUP,"</etc/ppp/ip-up") || exit print "Unable to read /etc/ppp/ip-up:$!\n";
	my @lines=<IPUP>;
	close IPUP;

	open (IPUP,">/etc/ppp/ip-up") || exit print "Unable to write /etc/ppp/ip-up:$!\n";
	my $ignore=undef;
	foreach (@lines) {
		if ((/# begin: GETDESC_WITH_PPP/ ne undef) ||
		    (/# begin: SLRNPULL_WITH_PPP/ ne undef)) {
			$ignore=1;
		}
		elsif ((/# end: GETDESC_WITH_PPP/ ne undef) ||
		       (/# end: SLRNPULL_WITH_PPP/ ne undef)) {
			$ignore=undef;
		}
		elsif (!$ignore) {
			print IPUP $_;
		}
	}
	close IPUP;
}

# Output current configuration.
sub ShowConfig {
	print "Current configuration: \n";
	print "\tRun slrnpull from a cron job.\n" 
		if $SLRNPULL_WITH_CRONJOB eq 'y';
	print "\tRun slrnpull via /etc/ppp/ip-up.d/slrnpull.\n" 
		if $SLRNPULL_WITH_PPP eq 'y';
	print "\tRun slrnpull by hand.\n" 
		if $SLRNPULL_WITH_PPP ne 'y' && $SLRNPULL_WITH_CRONJOB ne 'y';
	print "\n";
}

# Rather than parse the conf file, which is a shell script, I have
# sh source it and then echo out the variables it sets.
sub ReadConfig {
	open (READCONF,'sh -c \'source /etc/news/slrnpull.debian.conf;
		echo $SLRNPULL_WITH_CRONJOB;
		echo $SLRNPULL_WITH_PPP
		\' |') || die "Error processing /etc/news/slrnpull.debian.conf: $!";
	$SLRNPULL_WITH_CRONJOB=lc(substr(<READCONF>,0,1));
	$SLRNPULL_WITH_PPP=lc(substr(<READCONF>,0,1));
	close READCONF;
}

# This file needs to be set up right.
sub CheckNewsServerFile {
	if (! -f "/etc/nntpserver") {
		print <<eof;
	What news server (NNTP server) should I use for reading and
	posting news?
eof
		print "Enter its full name: ";
		$server=<>;

		open (NNTPSERVER,">/etc/nntpserver") 
			|| die "Unable to write to /etc/nntpserver: $!";
		print NNTPSERVER $server;
		close NNTPSERVER;
	}
}

# Save their configuration.
sub SaveConfig {
	open (CONFIG,">/etc/news/slrnpull.debian.conf") || 
		die "Unable to write to /etc/news/slrnpull.debian.conf: $!\nConfiguration was not saved.\n";
	print CONFIG 
qq{# This file configures how slrnpull is run to download news.
#
# This file can be edited by hand, or you can run /usr/sbin/slrnpullconfig for
# interactive setup. Each option should be answered 'y' or 'n'.

# Should slrnpull download newsgroup descritions via a cron job?
SLRNPULL_WITH_CRONJOB=$SLRNPULL_WITH_CRONJOB

# Should slrnpull be run from /etc/ppp/ip-up.d/slrn?
SLRNPULL_WITH_PPP=$SLRNPULL_WITH_PPP

};
	close CONFIG;
	print "Configuration saved to /etc/news/slrnpull.debian.conf.\n\n";

	# Fix old ip-up files that were modified. This should
	# *never* be removed.
	FixIpUp();
}

if (shift eq '--quiet' and -f "/etc/news/slrnpull.debian.conf") {
	# Don't do anything but refresh the config.
	&ReadConfig;
	&SaveConfig;
	exit;
}

print "Slrnpull Configuration\n";
print "----------------------\n";

CheckNewsServerFile();

if ( -f "/etc/news/slrnpull.debian.conf") {
	&ReadConfig;
	&ShowConfig;

	if (AskYN("Do you want to change the current configuration? ",'y') eq 'n') {
		print "Configuration was not changed.\n";
		# Refresh config anyway.
		&SaveConfig;
		exit;
	}
}

TOP: 

print <<eof;
Slrnpull needs to periodically connect to the network to download
new news. Please select how you want this to be handled:

  1. By a cron script that is run daily.
     This works well if you have a perminant network connection,
     or if you are using diald or a similar program that connects to the
     network on demand.

  2. By the /etc/ppp/ip-up.d/slrnpull script.
     This will have slrnpull download news when you connect to the network
     via ppp. This will happen *every time* you connect, if you select this
     option.

  3. By hand.
     You want to run a command manually every now and then when you are
     connected to the network to tell slrnpull to download new news. Or you
     want to roll your own solution.

eof

$resp=AskNumeric("Which do you choose? ",3);

$SLRNPULL_WITH_CRONJOB='n';
$SLRNPULL_WITH_PPP='n';

if ($resp eq 1) {
	$SLRNPULL_WITH_CRONJOB='y';
}
elsif ($resp eq 2) {
	$SLRNPULL_WITH_PPP='y';
}
else {
	print "The command to run to tell slrnpull to download news is:\nslrnpull -h \`cat /etc/nntpserver\` \n\n";
}

print "Configuration complete.\n\n";
&ShowConfig;
if (AskYN("Are you happy with this configuration? ",'y') ne 'y') { goto TOP }

&SaveConfig;
