#!/usr/bin/perl

use warnings;
use strict;

my $recepies = '/srv/sysadmin-cookbook/recepies';

use File::Find;
use File::Slurp;

my $in_ul = 0;

my @html;
sub html { push @html, @_ }

my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
my $escape_re  = join '|' => keys %escape;

sub file {
	my $path = shift;
	my $content = read_file $path;
	$content =~ s{[\n\r\s]+$}{}s;
	$content =~ s/($escape_re)/$escape{$1}/gs;
	return $content;
}

my @names;
find({ follow => 0, no_chdir => 1, wanted => sub {
	push @names, $_ unless m{/\.};
}}, $recepies );

foreach my $path ( sort @names ) {

	my $name = $path;
	$name =~ s{^$recepies/*}{};

	if ( -d $path ) {
		html "</ul>" if $in_ul++;
		html "<h1>$name</h1><ul>";
	} elsif ( -l $path ) {
		my $to = readlink $path;
		html "<li>$name</li>";
	} else {
		html "<li>$name<pre>", file( $path ), "</pre></li>";
	}

};

html "</ul>" if $in_ul;

print qq|
<html><head>
<title>Sysadmin Cookbook</title>
<!--
<link type=text/css rel=stylesheet href="style.css">
-->
<style type=text/css>
pre {
	background: #eee;
}
</style>
</head><body>
	|
	, join("\n", @html)
	, "</body></html>"
	;

