#!/usr/bin/perl

$rdir = "$ENV{'HOME'}/.hyplan";
$home = 'home';
$html = 'home.html';
$ENV{'KZB_PATH'} = 'styles/kinzb/';

$new  = "<img src=\"img/new.gif\" width=32 height=16 alt=\"*NEW*\">";	# '';
$newd = 30;

grep($except{$_}++ && 0, qw( . .. .cshrc .htaccess .login tags ));
grep($strong{$_}++ && 0, qw(
	align vshnu vshnucfg webrowse xtitle z go inbrowse));
grep($em{$_}++     && 0, qw(
	atls ctime cutbuf dumpdates ftw getdate grepw nzmail pop push
	rdistsumm renam rolllogs safe starthttpd termcolors urlencode
	vigrep wh whichpkg width xdo));

@catorder = ('webadm'	=> 'Web Administration',
	     'web'	=> 'Web Usage',
	     'unixadm'	=> 'Unix Administration',
	     'unix'	=> 'Unix Usage',
	     'unixuni'	=> 'Unix Uniformity',
	     'unixcfg'	=> 'Unix Configuration',
	     'vi'	=> 'Vi/Vim',
	     'x11'	=> 'X11',
	     'android'	=> 'Android',
	     'macos'	=> 'MacOS',
	     'palm'	=> 'Palm',
	     'homedir'	=> 'Personal Home Directories Administration',
	     'hyplan'	=> 'Personal Home Page Administration',
	     'refer'	=> 'Reference',
	     'unknown'	=> 'Unclassified',
	     'other'	=> 'Other Authors');
%catlabel = @catorder;
@catorder = grep(++$n % 2, @catorder);

%catspec = ();
%catdflt = ('etc' => 'unixcfg',
	    'img' => 'x11');
@nokey	 = ('img');

# mkhome - generate and install an HTML index of home directory files
# Steve Kinzler, steve@kinzler.com, Sep 94/Mar 98
# http://kinzler.com/me/home.html#hyplan

$cwd = "$rdir/$home";
chdir $cwd || die "$0: cannot chdir $cwd ($!)\n";

open(HTML, "> $rdir/$html") || die "$0: cannot open $rdir/$html ($!)\n";
while (<DATA>) {
	last if /__TAIL__/;
	print(HTML (s/^#include\s+`(.*)`\s*$/$1/) ? `$_` : $_);
}

$newt = time - $newd * 24 * 60 * 60;
print HTML "\n" . <<EOF;
<p>$new
indicates that this file was added or changed within the last $newd days.</p>
EOF

unless (opendir(HOME, '.')) {
	warn "$0: cannot opendir $cwd ($!)\n";
} else {
	foreach (sort readdir HOME) {
		next if $_ eq '' || $except{$_};
		if (-f) {
			(-T _ && (stat(_))[2] & 04) ?
				&dofile($_, (stat(_))[9]) :
				warn "$0: $cwd/$_ isn't world readable text\n";
		} elsif (-d _) {
			warn("$0: cannot opendir $cwd/$dir ($!)\n"), next
				unless opendir(DIR, $dir = $_);
			foreach (sort readdir DIR) {
				next if $_ eq '' || $except{$_};
				&dofile("$dir/$_", (stat(_))[9])
				    if ! -l "$dir/$_" &&
				       -f "$dir/$_" && (-T _ || /\.pdf$/) &&
				       (stat(_))[2] & 04;
			}
			closedir DIR;
		} else {
			warn "$0: $cwd/$_ isn't a file or directory\n";
		}
	}
	closedir HOME;

	foreach $cat (@catorder) {
		if (@cat = grep($_ ne '', split(/\001/, $cat{$cat}))) {
			warn "$0: warning, unclassified filesets (@cat)\n"
				if $cat eq 'unknown';
			print HTML "\n", `htmlbit sect`,
				   "<p><font size=\"+1\"><b><a ",
				   "name=\"$cat\">$catlabel{$cat}",
				   "</a></b></font></p>\n\n<ul>\n";
			foreach (sort @cat) {
				print HTML $item{$_};
			}
			print HTML "</ul>\n";
		}
	}
}

while (<DATA>) {
	print(HTML (s/^#include\s+`(.*)`\s*$/$1/) ? `$_` : $_);
}
close HTML;

###############################################################################

sub dofile {
	local($file, $mtime) = @_;
	local($item, $key, $cat, $junk) = ('', '', '', '');

	($key = $file) =~ s,.*/,,;
	$key =~ s/^\.*([^.]*).*/$1/;
	$key = '' if grep($file =~ /^$_\//, @nokey);

	warn("$0: cannot open $cwd/$file ($!)\n"), return
		unless open(FILE, $file);
	$item = "<a name=\"$file\" href=\"$home/$file\">$file</a>";
	while (<FILE>) {
		$junk = $1, $item =~ s/href="[^"]*/href="$junk/
			if /^;?[!#"] see website (\S+)/i;
		$item !~ /\n- / && ($item .= "\n- " . &htmlencode($1))
			if /^;?[!#"] \S+ --? (\S.*)/ || /^-- \S+ --? (\S.*)/ ||
			   /^$key \\- (\S.*)/ ||
			   /^\s*<[Pp]>\s*$key - (\S.*)<\/[Pp]>\s*$/ ||
			   /^\s*<!-- $key\.html? - (\S.*) -->\s*$/;
		$cat = $1, last if /\/$html#(\w+)/o;
	}
	close FILE;
	$item  = "<em>$item</em>"	  if $em{$key};
	$item  = "<strong>$item</strong>" if $strong{$key};
	$item  = "<li>$item\n";
	$item .= "$new\n"		  if $new && $mtime >= $newt;
	$cat   = $catspec{$key}		  if $cat eq '';
	$cat   = $catdflt{(grep($file =~ m;^$_/;, keys %catdflt))[0]}
					  if $cat eq '';
	$cat   = 'unknown'		  if $cat eq '';
	warn("$0: unknown category ($cat) in $file\n"), $cat = 'unknown'
					  if $catlabel{$cat} eq '';

	if ($file{$key} eq '') {
		$file{$key}  = $file;
		$item{$file} = $item;
		$cat{$cat}  .= "\001$file";
	} else {
		$item{$file{$key}} .= $item;
	}
}

sub htmlencode {
	local($_) = join('', @_);
	s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g;
	return $_;
}

###############################################################################

__END__
<html><head>
<title>Steve Kinzler - Home Directory</title>
<link rev="made" href="mailto:steve@kinzler.com">

<!-- DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED -->

<link rel="icon" type="image/ico" href="favicon.ico">
<link rel="SHORTCUT ICON" href="favicon.ico">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">

<link rel="stylesheet" type="text/css" href=".css">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- for snippet when shared (Google+) -->
<meta itemscope itemtype="http://schema.org/SoftwareApplication">
<meta itemprop="applicationCategory" content="utility">
<meta itemprop="name" content="Steve Kinzler's Home Directory">
<meta itemprop="description" name="description"
      content="Scripts, configuration files and man pages from Steve
	       Kinzler's Unix/X Window System home environment.">
<!-- other schema properties -->
<meta itemprop="keywords" name="keywords"
      content="unix, linux, x11, android, macos, palm, web, vi, vim, scripts,
	       sh, bash, perl, utilities, command line, configuration files,
	       man pages, home directory, mkhome">

<!-- Facebook Open Graph -->
<meta property="og:title" content="Steve Kinzler's Home Directory">
<meta property="og:site_name" content="Steve Kinzler"> 
<meta property="og:url" content="http://kinzler.com/me/home.html"> 
<meta property="og:description"
      content="Scripts, configuration files and man pages from Steve
	       Kinzler's Unix/X Window System home environment.">

<!-- Google (Universal) Analytics code per Google;
     "ga('require'" lines enable Display Advertiser Features
       and Enhanced Link Attribution -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-52973492-3', 'auto');
  ga('require', 'displayfeatures');
  ga('require', 'linkid', 'linkid.js');
  ga('send', 'pageview');
</script>

</head>

#include `htmlbit body | sed 's/100%"/& align=center/; s/serif"/& size="+2"/'`
<a href="index.html"><img src="images/kinzler_icon.gif" border=0
  width=48 height=48 alt="" align=left></a>
<img src="styles/kinzb/bit.gif" width=48 height=48 alt="" align=right>
<a href="index.html">Steve Kinzler</a>'s Home Directory

#include `htmlbit sect`
<p>These are some scripts, configuration files and man pages from my
Unix/X Window System home environment.  They were developed and collected
to allow me to maintain an enhanced, consistent home environment across
many varieties of Unix and many separate home directories.  As such, they
generally favor portability over efficiency and restrict themselves to
ubiquitous Unix tools when possible, but take advantage of better tools
when available.  As a systems administrator in a heterogenous environment,
I've found them to be useful.</p>

<p>Feel free to browse and borrow from these files as you wish.
I'm afraid there's often little or no documentation or comments,
so you may be on your own to figure out what some of them are good
for.  Most of my scripts (ie, except those in the "Other Authors"
section) will only print a usage message if run with a <tt><font
face="Courier,monospace">-h</font></tt> flag.  Most of the scripts will
port "as is" to most versions of Unix and have any configuration options
specified at the top of the script file.</p>

<p><strong>Note:</strong> Please observe any copyrights in these files.
If none is given for a particular file, an implicit copyright by
the author may be presumed which permits copying, modification and
redistribution and with any express or implied warranties disclaimed
(ie, use at your own risk).</p>

<p><a href="images/desktop.gif"><img src="images/desktop_icon.gif" border=0
width=75 height=60 alt="Thumbnail of Desktop"> A screenshot of my typical
desktop.</a> <em>[.gif, 317 KB, 1280x1024]</em></p>
__TAIL__

#include `htmlbit sect`
<!-- Unix System Admin Web Ring Code Start [OBSOLETE, INVALID]
<center><p>
<b>The Unix System Administration Webring</b><br>
<a href="http://www.webring.org/cgi-bin/webring?ring=ugu&home">[Home]</a>
<a href="http://www.webring.org/cgi-bin/webring?ring=ugu&next&id=22">[Next]</a>
<a href="http://www.webring.org/cgi-bin/webring?ring=ugu&prev&id=22">[Prev]</a>
<a href="http://www.webring.org/cgi-bin/webring?ring=ugu&random">[Random]</a>
<a href="http://www.webring.org/cgi-bin/webring?ring=ugu&list">[List]</a>
</p></center>
     Unix System Admin Web Ring Code   End -->

<!-- Unix System Admin Web Ring Code Start -->
<center>
<script language="javascript" type="text/javascript"
	src="http://ss.webring.com/navbar?f=j;y=sbkinzler;u=m10017978">
</script>
Powered by <a href="http://dir.webring.com/rw" target="_top">WebRing</a>
</center>
<!-- optional: -->
<noscript><center>
<table bgcolor="gray" cellspacing="0" border="2"><tr><td>
<table cellpadding="2" cellspacing="0" border="0"><tr><td align="center">
<font face="Arial" size="-1">This site is a member of WebRing.<br>
To browse, visit
<a href="http://ss.webring.com/navbar?f=l;y=sbkinzler;u=m10017978">here</a>.
</font></td></tr></table></td></tr></table></center></noscript>
<!-- Unix System Admin Web Ring Code   End -->

<p>This document is generated daily with <a name="mkhome" href="bin/mkhome">
<tt><font face="Courier,monospace">mkhome</font></tt></a>, and is
a <a href="http://kinzler.com/">Kinzler.com</a> offering.</p>

#include `htmlbit tail | sed 's/"[^"]*">@</"index.html">@</'`
