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

# Get y or n 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;
	}
}

# Add a section to /etc/ppp/ip-up.
sub AddIpUp { my ($tag,$section)=@_;
	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;

	if ($lines[$#lines]=~m/\n$/ eq undef) {
		$lines[$#lines].="\n"; # make sure eof has trailing \n.
	}

	open (IPUP,">/etc/ppp/ip-up") || exit print "Unable to write /etc/ppp/ip-up: $!\n";
	foreach (@lines) { print IPUP $_ }
	print IPUP "# begin: $tag (automatically added by slrnconfig)\n";
	print IPUP "$section\n";
	print IPUP "# end: $tag\n";
	close IPUP;
}

# Remove a section from /etc/ppp/ip-up.
sub RemoveIpUp { my $tag=shift;
	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: $tag / ne undef) {
			$ignore=1;
		}
		elsif (/# end: $tag/ 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.\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.\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";
}

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

if (! -f "/etc/news/server") {
	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/news/server") || exit print "Unable to write to /etc/news/server: $!";
	print NNTPSERVER $server;	
	close NNTPSERVER;
}

if ( -f "/etc/slrnget.conf") {
	# Rather than parse the conf file, which is a shell script, I have 
	# sh source it and then echo out the variables it sets.
	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
		\' |') || exit print "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;

	&ShowConfig;

	if (AskYN("Do you want to change the current configuration? ",'y') eq 'n') {
		print "Configuration was not changed.\n";
		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') {
	if (-f '/etc/ppp/ip-up') {
		print <<eof;
An alternative to a cron job is to configure the /etc/ppp/ip-up 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. I can add commands to /etc/ppp/ip-up to
accomplish this.

eof
		$GETDESC_WITH_PPP=AskYN("Do you want /etc/ppp/ip-up to refresh the \nnewsgroup descriptions? ",$GETDESC_WITH_PPP);
	}
	else {
		print "Hmmm. I don't see a /etc/ppp/ip-up file. Skipping that option.\n";
		$GETDESC_WITH_PPP='n';
	}
}
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') {
		if (-f '/etc/ppp/ip-up') {
			print <<eof;
An alternative to a cron job is to configure the /etc/ppp/ip-up script so
that it will have slrnpull get the new newsgroup descriptions each time you
connect to the network. I can add commands to /etc/ppp/ip-up to accomplish 
this.

eof
			$SLRNPULL_WITH_PPP=AskYN("Do you want /etc/ppp/ip-up to run slrnpull? ",$SLRNPULL_WITH_PPP);
		}
		else {
			print "Hmmm. I don't see a /etc/ppp/ip-up file. Skipping that option.\n";
			$SLRNPULL_WITH_PPP='n';
		}		
	}
	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 }

# Save their configuration.
open (CONFIG,">/etc/slrnget.conf") || exit print "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 be used to run slrn when the network comes up?
# (If you answer 'y', you will need to edit ip-up appropriatly.)
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?
# (If you answer 'y', you will need to edit ip-up appropriatly.)
SLRNPULL_WITH_PPP=$SLRNPULL_WITH_PPP

};
close CONFIG;
print "Configuration saved to /etc/slrnget.conf.\n\n";

# Handle modifications to ip-up.
RemoveIpUp("GETDESC_WITH_PPP");
RemoveIpUp("SLRNPULL_WITH_PPP");
if ($GETDESC_WITH_PPP eq 'y') {
	AddIpUp("GETDESC_WITH_PPP","source /etc/slrnget.conf\nif [ \"\$GETDESC_WITH_PPP\" = \"y\" ] ; then /usr/sbin/slrn_getdescs >/dev/null; fi\n");

	print <<eof;
I have modified /etc/ppp/ip-up to call slrn to refresh newsgroup 
descriptions once a week.

eof
}
if ($USE_SLRNPULL eq 'y' && $SLRNPULL_WITH_PPP eq 'y') {
	AddIpUp("SLRNPULL_WITH_PPP","source /etc/slrnget.conf\nif [ \"\$USE_SLRNPULL\" = \"y\" -a \"\$SLRNPULL_WITH_PPP\" = \"y\" ] ; then slrnpull -h `cat /etc/news/server` >/dev/null; fi\n");

	print <<eof;
I have modified /etc/ppp/ip-up to run slrnpull each time you connect to the
network.

eof
}

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";
	}
}
