#!/usr/bin/perl 
# mksigen.pl by TOYODA Eizi, 1998

	$ARGV_ZERO = $0;
	$VERBOSE = 0;
	$CONFIGBASE = "mksigen.conf";
	$SIGEN = "SIGEN";
	$isMSDOS = &isMSDOS;
	($INCONV, $OUTCONV) = &initFilter;
	$MKSIGENDIR = &ThisScriptDir;
	&parse_opt;

	&LoadConfig;
	do {
		print "--- configuration of mksigen ---\n";
		&query;
		print "--- your configuration ---\n";
		&printconfig;
		&query_config_bool("ok", "is this okay?", 1);
	} until ($config{"ok"});

	&query_config_string("file", "configuration file", $CONFIG);
	$output = $config{"file"};
	&open_w($output) || die "cannot open $output for writing";
	select($output);
	&printconfig;
	close($output);
exit 0;

sub printconfig {
	foreach (keys %config) {
		next unless /:$/;
		print "$_ $config{$_}\n";
	}
}

sub query {
	&query_config_string("topdir:", "top directory", "/home");
	&query_config_string("toptitle:", "top directory title", "/home");
	&query_config_string("fgcolor:", "text color (name or #aabbcc)",
		"#333333");
	&query_config_string("bgcolor:", "background color (name or #aabbcc)",
		"#CCFFCC");
	&query_config_string("linkcolor:", "link color (name or #aabbcc)",
		"#000099");
	&query_config_string("vlinkcolor:", "visited link color (name or #aabbcc)",
		"#333399");
	&query_config_string("alinkcolor:", "click link color (name or #aabbcc)",
		"#FF0000");
	&query_config_string("hovercolor:", "hover link color (name or #aabbcc)",
		"#FF0000");
	&query_config_string("ignorepat:", "ignored filename pattern",
		'\\.bak$|~$|^lost\+found$');
	$hostname = `hostname -f` || `hostname` || '(hostname)';
	&query_config_string("heading:", "hostname written in HTML",
		$hostname);
	$config{"acktext:"} = 'this page is generated with ::mksigen::';
	$config{"acklink:"} = 'http://www.gfd-dennou.org/arch/cc-env/mksigen/desc.htm';
	&query_config_bool("indent:", "make <DL> indentation?", 1);
	&query_config_bool("summarize:", "make summary?", 1);
	&query_config_string("summarytext:", "summary section title", 
		"summary");
	&query_config_string("detailtext:", "detail section title", 
		"detail");
	&query_config_string("forcegid:",
		"force mksigen to be in a group", "");
}

sub query_config_string {
	local($fieldname, $desc, $default) = @_;
	local($value);
	$default = $config{$fieldname} || $default;
	$| = 1;
	print "$desc: [$default]";
	chop($value = <STDIN>);
	$value = $default unless $value;
	$config{$fieldname} = $value;
}

sub query_config_bool {
	local($fieldname, $desc, $default) = @_;
	local($default, $value);
	$config{$fieldname} = $config{$fieldname} || $default;
	$default = $config{$fieldname} ? 'Y' : 'N';
	$| = 1;
	print "$desc: (y/n) [$default]";
	chop($value = <STDIN>);
	($config{$fieldname} = ($value !~ /[nN]/) + 0) if $value;
}

sub LoadConfig {
	if (!$CONFIG) {
		foreach $confdir ('/etc', DirCat($MKSIGENDIR, "../lib")) {
			$CONFIG = DirCat($confdir, $CONFIGBASE);
			last if -f $CONFIG;
		}
	}
	warn "config <$CONFIG>\n" if ($VERBOSE > 0);
	&open_r($CONFIG) || return;
	%config = &ReadHeaders($CONFIG);
	close($CONFIG);
	die "config $CONFIG reading\n" if $?;
}

sub parse_opt {
	require "getopts.pl" || return;
	&Getopts("VvDc:n");
	$opt_c && ($CONFIG = $opt_c);
	$opt_v && ($VERBOSE = $opt_v);
	$opt_V && die q$Id: mksigenconfig,v 1.2 1999/07/01 10:11:04 toyoda Exp $ . "\n";
	$opt_n && ($VERBOSE = 1, $NOWRITE = 1);
	$opt_D && ($VERBOSE = 2);
}

#
# yori ippanteki na routine
#

sub DirChildren {
	local($dir) = @_;
	opendir($dir, $dir) || (warn "Error: $dir cannot opendir", return ());
	local(@children) = grep(!/^\./, readdir($dir));
	closedir($dir);
	return @children;
}

sub UpDir {
	local($_) = @_;
	return ".." if /^\.?$/;
	return "." if !/\//;
	s|/[^/]+$||;
	$_;
}

sub DirCat {
	local($dir, $file) = @_;
	return "/$file" if ($dir eq "/");
	return "$file" if ($dir eq ".");
	return "$dir$file" if ($file =~ /^[\/\\]/);
	"$dir/$file";
}

sub ReadHeaders {
	local($emlfile) = @_;
	local($name, $val, %headers);
	$name = ""; undef %headers;
	#
	while (<$emlfile>) {
		chop;
		s/\r$//;
		last if /^$/;
		if (!/^\s/) {
			if (!/^([-A-Za-z0-9]*:)\s*(.*)/) {
				warn "Error: broken header \"$_\" in $emlfile\n";
				next;
			}
			($name = $1) =~ tr/A-Z/a-z/;
			($val = $2) =~ s/[\t\r]/ /g;
			if (defined $headers{$name}) {
				$headers{$name} .= " $val";
			} else {
				$headers{$name} = $val;
			}
		} else {
			s/[\t\r]/ /g;
			s/^ */ /;
			$headers{$name} .= $_;
		}
	}
	%headers;
}

sub ThisScriptDir {
	(local($myname) = &which($ARGV_ZERO)) || return ".";
	($myname !~ m|[/\\:]|) && return ".";
	$myname =~ s|([/\\:])[^/\\:]*$|$1|;
	$myname =~ s|[/\\]$||;
	$myname;
}

sub which {
	local($cmd) = @_;
	#return $cmd if (($cmd =~ /^\//) && -e $cmd);
	return $cmd if (-e $cmd);
	local($pathdelim) = $isMSDOS ? ";" : ":";
	local($test);
	foreach (split(/$pathdelim/, $ENV{"PATH"})) {
		$test = "$_/$cmd";
		return $test if -e $test;
	}
	return undef;
}

sub FmtTime {
	local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
	 = localtime time;
	sprintf "%4d/%02d/%02d %02d:%02d:%02d",
		$year + 1900, $mon + 1, $mday, $hour, $min, $sec;
}

sub isMSDOS {
	# MS-DOS has no /dev/null.
	return 0 if ( -c '/dev/null' );
	# MS-DOS has /DEV/CON even in drive without /DEV.
	return 1 if ( -f '/DEV/CoN' && ! -d '/DEV' );
	# MS-DOS allows CON or NUL have extension.
	return 2 if ( -f '/coN.3b7' && -f '/NuL.j0Q' && -f '/nUl.!#$' );
	0;
}

sub initFilter {
	# global $INCONV, $OUTCONV
	local($nkf) = &which("nkf");
	$INCONV = ($isMSDOS ? "$nkf -s" : "$nkf -e");
	$OUTCONV = ($isMSDOS ? undef : "nkf -s");
	($INCONV, $OUTCONV);
}

sub open_r {
	local($file) = @_;
	return 0 if (!-r $file);
	$? = 0;
	return open($file, $file) if (!$INCONV);
	open($file, "$INCONV $file |");
}

sub open_w {
	local($file) = @_;
	warn "output to $file\n" if ($VERBOSE > 0);
	return open($file, ">/dev/null") if ($NOWRITE);
	$? = 0;
	return open($file, ">$file") if (!$OUTCONV);
	return open($file, "| $OUTCONV") if ($file eq "-");
	open($file, "| $OUTCONV > $file");
}

