#!/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;
	}
}

# 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 "\tUse cron job to refresh newsgroup descriptions.\n" 
		if $GETDESC_WITH_CRONJOB eq 'y';
	print "\tRefresh newsgroup descriptions via /etc/ppp/ip-up.d/slrn.\n" 
		if $GETDESC_WITH_PPP eq 'y';
	print "\tRefresh newsgroup descriptions by hand.\n" 
		if $GETDESC_WITH_PPP ne 'y' && $GETDESC_WITH_CRONJOB ne 'y';
	if ($USE_SLRNPULL eq 'y') {
		print "\tUse slrnpull to generate a local news spool.\n";
		print "\t\tRun slrnpull from a cron job.\n" 
			if $SLRNPULL_WITH_CRONJOB eq 'y';
		print "\t\tRun slrnpull via /etc/ppp/ip-up.d/slrn.\n" 
			if $SLRNPULL_WITH_PPP eq 'y';
		print "\t\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/slrnget.conf;
		echo $USE_SLRNPULL;
		echo $GETDESC_WITH_CRONJOB;
		echo $GETDESC_WITH_PPP;
		echo $SLRNPULL_WITH_CRONJOB;
		echo $SLRNPULL_WITH_PPP
		\' |') || die "Error processing /etc/slrnget.conf: $!";
	$USE_SLRNPULL=lc(substr(<READCONF>,0,1));
	$GETDESC_WITH_CRONJOB=lc(substr(<READCONF>,0,1));
	$GETDESC_WITH_PPP=lc(substr(<READCONF>,0,1));
	$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/slrnget.conf") || 
		die "Unable to write to /etc/slrnget.conf: $!\nConfiguration was not saved.\n";
	print CONFIG qq{
# This file configures how slrn is called to refresh newsgroups
# descriptions, and how slrnpull is called to download news.
#
# This file can be edited by hand, or you can run /usr/sbin/slrnconfig for
# interactive setup. Each option should be answered 'y' or 'n'.

# Should slrnpull be used at all?
USE_SLRNPULL=$USE_SLRNPULL

# Should slrn download newsgroup descriptions via a cron job?
GETDESC_WITH_CRONJOB=$GETDESC_WITH_CRONJOB

# Should /etc/ppp/ip-up.d/slrn be used to run slrn when the network comes up
# to refresh newsgroup descriptions?
GETDESC_WITH_PPP=$GETDESC_WITH_PPP

# 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/slrnget.conf.\n\n";

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

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

print "Slrn Configuration\n";
print "------------------\n";

CheckNewsServerFile();

if ( -f "/etc/slrnget.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;
Slrn needs to periodically connect to the network to download
new descriptions of newsgroups. One way to accomplish this is
by a cron script that is run weekly. This works well if you
have a perminent network connection, or if you are using diald
or a similar program that connects to the network on demand.

eof
$GETDESC_WITH_CRONJOB=AskYN(
"Do you want a cron job to be set up to automatically refresh
the newsgroup descriptions for you? ",$GETDESC_WITH_CRONJOB);

if ($GETDESC_WITH_CRONJOB eq 'n') {
	print <<eof;
An alternative to a cron job is to configure the /etc/ppp/ip-up.d/slrn script
so that it will have slrn get the new newsgroup descriptions when you connect
to the network. The new descriptions will still only be retrieved once a
week if you choose this method, no matter how often you connect to the
network.

eof
	$GETDESC_WITH_PPP=AskYN("Do you want /etc/ppp/ip-up.d/slrn to refresh the \nnewsgroup descriptions? ",$GETDESC_WITH_PPP);
}
else {
	$GETDESC_WITH_PPP='n';
}

if ($GETDESC_WITH_CRONJOB eq 'n' && $GETDESC_WITH_PPP eq 'n') {
	print <<eof;
The only option that is left is for you to run a command manually every
now and then when you are connected to the network to tell slrn to 
update the newsgroup descriptions. Or you can roll your own solution.

The command to run (as root) is:
	/usr/sbin/slrn_getdescs

eof
	print "Press return:";
	<>;
	print "\n";
}

$USE_SLRNPULL=AskYN(
"Do you plan to use slrnpull to download a small local
news spool for offline news reading? ",$USE_SLRNPULL);

if ($USE_SLRNPULL eq 'y') {
	print <<eof;
Slrnpull needs to periodically connect to the network to download
new news. One way to accomplish this is by a cron script that is run
daily. This works well if you have a perminent network connection,
or if you are using diald or a similar program that connects to the
network on demand.

eof
	$SLRNPULL_WITH_CRONJOB=AskYN("Do you want a cron job to be set up to automatically run slrnpull? ",$SLRNPULL_WITH_CRONJOB);
	if ($SLRNPULL_WITH_CRONJOB eq 'n') {
		print <<eof;
An alternative to a cron job is to configure the /etc/ppp/ip-up.d/slrn script
so that it will have slrnpull get the new newsgroup descriptions each time you
connect to the network.

eof
		$SLRNPULL_WITH_PPP=AskYN("Do you want /etc/ppp/ip-up.d/slrn to run slrnpull? ",$SLRNPULL_WITH_PPP);
	}
	else {
		$SLRNPULL_WITH_PPP='n';
	}

	if ($SLRNPULL_WITH_CRONJOB eq 'n' && $SLRNPULL_WITH_PPP eq 'n') {
		print <<eof;
The only option that is left is for you to run slrnpull periodically
by hand when you are connected to the network. Or you can roll your own
solution.

eof
		print "Press return:";
		<>;
		print "\n";
	}
}

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

&SaveConfig;

if (! -f "/var/lib/slrn/newsgroups.dsc") {
	if (AskYN("Are you online now? If so, I'd like to refresh the\nnewsgroup descriptions. May I? ",$SLRNPULL_WITH_CRONJOB) eq 'y') {
		print "Refreshing...\n";
		system "/usr/sbin/slrn_getdescs";
	}
}
