#!/usr/bin/perl

# Hacked the daylights out of this for the debian GNU/Linux distribution.
# It does much much more than it did before.
#
# pkg-getopts.pl, by Jeff Meininger
#
# this is a generic script designed for use in creating 
# WMHeadlines 'packages' for different UNIX/Linux distros.
# (things like .deb's and .rpm's)
#
# it prompts the user for input about what options to be 
# used in the WMHeadlines installation.
#
# This is a very general script, and is designed to be hacked 
# for your particular needs.  Hack away!
#
# Note: I have not tested this very much.  Your milage may vary.

@menunames = ("menu.slashdot", "menu.freshmeat", "menu.segfault", "menu.linuxtoday", "menu.mattshouse", "menu.benews", "menu.beoscentral");

@headlines = ("wmslashdot.pl", "wmfreshmeat.pl", "wmsegfault.pl", "wmlinuxtoday.pl", "wmmattshouse.pl", "wmbenews.pl", "wmbeoscentral.pl");

@headnames = ("Slashdot", "Freshmeat","Segfault", "LinuxToday", "MattsHouse", "BeNews",  "BeOSCentral");

print "\n---[WMHeadlines Configuration Options]-----------------------------------------\n\n";

print "If you are exceedingly naughty and want your Headlines to branch directly\n";
print "off the Windowmaker root menu, say yes [y] here.  Note that debian policy\n";
print "states that this stuff should be in the Apps-Net category.  Fine and\n";
print 'upstanding, policy-loving debian users will say "no, no, a thousand times no!!!';
print "\n\n";
print "Root menu naughtiness? [y/n] ";

$userconf = <STDIN>;
if ($userconf =~ /^y/i) {
	$menusection = "Headlines";
	print "\nEvil!!\n\n";
}
else {
	$menusection = "Apps/Net/Headlines";
}



print "[-proxy host port] - enables use over a proxy\n";
print "[-n] - prevents netscape from opening a new window for each headline\n";
print "[-frontpage top|bottom] - adds a persistant item for the $sitename frontpage.\n\tIt can be placed at the top or bottom of the menu.\n";
print "[-loud] - useful for debugging problems.  Otherwise, the script exits quietly\n\tin the case of an error.\n";
print "[-24] - display time in 24 format instead of 12 AM/PM\n";
print "[-dayfirst] - display day before month (like 25-12-1999) in date.\n";
print "[-nodate] - do not display 'last updated' date in menu title.\n";
print "[-noyear] - do not display the year in the 'last updated' date.\n";
print "[-proxyfix] - proxy support not working?  Try this.\n";
print "[-maxlength num] - items will not be shown with more than [num] letters.\n";
print "[-nspath /path/to/netscape/executable] - if your 'netscape' is not in the PATH\n\tused by Window Maker, you may specify the full pathname to the netscape\n\texecutable here.\n";

print "\n\n";
print "Type the commandline options you wish to use, or 'h' for help.\n";
print "Example: -dayfirst -noyear -24 -proxy foo.bar.net 3128\n";


USERINPUT:
	$flag = 0;

# print "\n>> ";

$userline = <STDIN>;
chop($userline);

if (($userline eq "h") || ($userline eq "H")) {
	print "[-proxy host port] - enables use over a proxy\n";
	print "[-n] - prevents netscape from opening a new window for each headline\n";
	print "[-frontpage top|bottom] - adds a persistant item for the $sitename frontpage.\n\tIt can be placed at the top or bottom of the menu.\n";
	print "[-loud] - useful for debugging problems.  Otherwise, the script exits quietly\n\tin the case of an error.\n";
	print "[-24] - display time in 24 format instead of 12 AM/PM\n";
	print "[-dayfirst] - display day before month (like 25-12-1999) in date.\n";
	print "[-nodate] - do not display 'last updated' date in menu title.\n";
	print "[-noyear] - do not display the year in the 'last updated' date.\n";
	print "[-proxyfix] - proxy support not working?  Try this.\n";
	print "[-maxlength num] - items will not be shown with more than [num] letters.\n";
	print "[-nspath /path/to/netscape/executable] - if your 'netscape' is not in the PATH\n\tused by Window Maker, you may specify the full pathname to the netscape\n\texecutable here.\n";
#	print "[-menufile /etc/X11/WindowMaker/menu.$scriptname] - if you want to store the\n\tmenu somewhere other than ~/.$scriptname" . "menu, specify the FULL\n\tpathname with this option.  (Keep write permissions in mind...)\n";
#	print "[-forceupdate] - force the menu file to be written even if the headlines have\n\tnot changed.  For example, a change in preference options will _NOT_\n\tbe visible without using this option.\n";
} else {

	@fields = split(/ /, $userline);
	foreach $i (@fields) {
		if($fields[$i] eq "-proxy") {
			if ((!defined($fields[$i + 2])) || ($fields[$i + 1] =~ /^-/) || ($fields[$i + 2] =~ /^-/) || ($fields[$i + 2] =~ /\D/)) {
				print "Please use '-proxy proxy.yourisp.com 3128' or whatever is appropriate.\n";
				$flag++;
			}
		} elsif($fields[$i] eq "-frontpage") {
			if ((!defined($fields[$i + 1])) || (($fields[$i + 1] ne "top") && ($fields[$i + 1] ne "bottom"))) {
				print "Please use '-frontpage top' or '-frontpage bottom' to enable this feature.\n";
				$flag++;
			}
		} elsif($fields[$i] eq "-maxlength") {
			if ((!defined($fields[$i + 1])) || ($fields[$i + 1] =~ /\D/)) {
				print "Please use '-maxlength NUM' where NUM is an integer, like 60.\n";
				$flag++;
			}
		} elsif($fields[$i] eq "-nspath") {
			if (!defined($fields[$i + 1]) || ($fields[$i + 1] !~ /^\//)) {
				print "Please use '-nspath /opt/netscape-4.5/netscape', or whatever is appropriate.\nNote... you must specify the FULL pathname.\n";
				$flag++;
			}
		} elsif($fields[$i] eq "-menufile") {
				print "The -menufile option is already set by the Debian packaging script..";
				$flag++;
		}

		if ($flag > 0) {
			print "Please try again...\n";
			goto USERINPUT;
		}
	}

	print "Enter 'Y' to confirm your selection, or 'N' to re-enter it.\nConfirm: ";
	$userconf = <STDIN>;
}

unless ($userconf =~ /^y/i) {
	goto USERINPUT;
}

# print "The line to be added to the crontab should be something like this...\n";
# print "0,30 * * * * /path/to/wmwhatever.pl $userline\n";
# Debian users don't need to see this.

# this line writes a new file...
# open(CRONFILE, ">/whatever/file/you/want");

# this line appends to a file, or otherwise creates a new file...
# open(CRONFILE, ">>/whatever/file/you/want");

# print CRONFILE "0,30 * * * * /path/to/wmwhatever.pl $userline\n";

# close(CRONFILE);
print "\nYou will now be asked which WMHeadlines news sites you want on your Wmaker menu.\n\n";


open(MENUFILE, ">/usr/lib/menu/wmheadlines");
open(CRONFILE, ">/etc/cron.d/wmheadlines");
open(UPDATEFILE, ">/tmp/wmheadlines.tmp");

print UPDATEFILE "#!/bin/sh \n";

for ($i=0; $i < @headlines; $i++) {
	print "\n", @headnames[$i], " [y/n] ";
		$userconf = <STDIN>;
		if ($userconf =~ /^y/i) {
			print MENUFILE "?package(wmheadlines):needs=wmaker \\\n";
			print MENUFILE 'section="', $menusection, '" title="', @headnames[$i], '" \\', "\n";
			print MENUFILE 'command="OPEN_MENU /etc/X11/WindowMaker/', @menunames[$i], '"', "\n\n";
			print CRONFILE "*/30 * * * * root /usr/bin/", @headlines[$i], " ", $userline, " -forceupdate -menufile /etc/X11/WindowMaker/", @menunames[$i], "\n";
			print UPDATEFILE "echo retrieving ", @headnames[$i], " headlines.......\n";
			print UPDATEFILE "/usr/bin/", @headlines[$i], " ", $userline, " -forceupdate -menufile /etc/X11/WindowMaker/", @menunames[$i], "\n";
		}
}

close(MENUFILE);
close(CRONFILE);
close(UPDATEFILE);

$mode = 0755;   chmod $mode, '/tmp/wmheadlines.tmp'; 

print "\nYou can reset these config options at any time by running wmheadlines-config\n\n";

system("/tmp/wmheadlines.tmp");

print "*** Updating WindowMaker Menus\n";

exec("update-menus");
