#!/usr/bin/perl

# 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.

print "\n---[WMHeadlines Installation Options]-------------------------------------------\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") {
			if (!defined($fields[$i + 1]) || ($fields[$i + 1] !~ /^\//)) {
				print "Please use '-menufile /etc/X11/WindowMaker/menu.$scriptname', or whatever is\nappropriate.  Note... you must specify the FULL pathname.\n";
				$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";

# 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);
