#!/usr/bin/perl -w
#
# Update the workshop section of the website
#
# - Trawl the source directories
#
# - If there's a file in the workshop that's also in the source
#   directory, and the source directory version is newer, copy it
#   over. This allows me full control over what's in the website
#   while at the same time keeping it up-to-date
#
# - Also generate an entry on index.html for the file
#
# September 2002:
#  Sort the files by filename
# 08/03/2004 Add timestamp to file listing
# 16/11/2004 Woo, google adsense
# 04/07/2020 Remove the adsense stuff. tsk.
use File::Find;
use File::Copy;
use File::Temp qw( tempfile );

use strict;

my $debug = $ENV{'DEBUG'}||0;
my $doit = $ENV{'DOIT'} ||0;


find( { wanted=> \&whackshop, no_chdir => 1, follow_skip => 2 },
      $ENV{'HOME'} . "/src" );
find( { wanted=> \&genindex, no_chdir => 1, follow_skip => 2 },
      $ENV{'HOME'} . "/public_html/hacks/workshop" );

exit;

# =============================================================================
sub whackshop {
  my $file = $_;

  # Things to ignore
  return if -d $file;
  return if $file =~ m{/(CVS|RCS)(/.*|)$};

  # Skip it if it's not in the workshop already
  my $shopfile = $file;
  $shopfile =~ s|$ENV{'HOME'}/src|$ENV{'HOME'}/public_html/hacks/workshop|;
  return if ! -f $shopfile;

  print STDERR "Checking $shopfile..." if $debug;

  my ( undef, undef, undef, undef, undef, undef, undef, undef, undef,
       $mtime1, undef, undef, undef, undef ) = stat( $file );
  my ( undef, undef, undef, undef, undef, undef, undef, undef, undef,
       $mtime2, undef, undef, undef, undef ) = stat( $shopfile );

  # safety
  $mtime1 ||= 0;
  $mtime2 ||= 0;

  if ( $mtime1 > $mtime2 ) {
    print STDERR "newer version available" if $debug;
    unlink( $shopfile ); # in case it's write protected or whatever
    copy( $file, $shopfile ) or die "Failed to update $file: $!";
  }

  print STDERR "\n" if $debug;
}

sub genindex {
  my $file = $_;
  return if ! -d $file;
  return if -l $file;
  return if $file =~ /workshop$/; # don't touch this index!
  print STDERR "genindex in $file\n" if $debug;
  opendir( DIR, "$file" );
  my @files = grep !/^(.\.?|index\.html-new|index\.html)$/, readdir( DIR );
  closedir( DIR );

  # Oog
  my $level = $file;
  $level =~ s|$ENV{'HOME'}/public_html/||;
  $level =~ s|[^/]+|..|g;
  my $title = $file;
  $title =~ s|^.*/||;
  $title .= " hacks";

  my ( $fh, $filename ) = tempfile();
  print $fh <<"HEADER";
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Waider">
   <META NAME="Description" CONTENT="Works-in-progress ($title)">
   <link rel="stylesheet" href="$level/waider.css" type="text/css">
<TITLE>The Workshop - $title</TITLE>
</HEAD>
<body>
HEADER

  my $origin = $file;
  $origin =~ s@public_html/hacks/workshop@src@;
  if ( open( HEADER, "<$origin/header.html" )) {
      while ( my $l = <HEADER> ) {
          print $fh $l;
      }
      close( HEADER );
  } else {
      print $fh <<"HEADER";
<h1>$title</h1>
<p>This page contains links to some of my $title. It is autogenerated from the comments (if any) at the head of each file.
</p>
<dl>
HEADER
  }

  for my $f ( sort @files ) {
    print STDERR "  $f\n" if $debug;
    my ( undef, undef, undef, undef, undef, undef, undef, undef, undef,
         $mtime, undef, undef, undef, undef ) = stat( "$file/$f" );
    $mtime = scalar( gmtime( $mtime ));
    print $fh qq(<dt><a href="$f">$f</a><span style="float: right">(updated $mtime)</span></dt>\n<dd><tt>);
    if ( -d "$file/$f" ) {
      print $fh "Subdirectory<br>";
    } else {
      open( THISFILE, "$file/$f" );
      while ( <THISFILE> ) {
        next if /^#!/; # skip bangsplat line
        next if /^$/;
        last if !m{^($| |#|[/ ]+\*|;;)};

        # escape things that might get interpreted
        s/&/\&amp;/g;
        s/</\&lt;/g;
        s/>/\&gt;/g;

        print $fh $_;
        print $fh "<br>\n";
      }
      close( THISFILE );
      print $fh "<br><br>\n";
    }
    print $fh "</tt></dd>\n";
  }

  print $fh <<"TRAILER";
</dl>
<hr>
<ADDRESS>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
  <tr valign="top">
    <td align="left">Waider</td>
    <td align="right" class="tagline">Ugh. Did I write that?</td>
  </tr>
</table>
</ADDRESS>

</BODY>
</HTML>
TRAILER

  close( $fh );

  if ( $doit ) {
      unlink "$file/index.html";
  }

  # check if it needs updating
  if ( -f "$file/index.html" ) {
    if ( system( "cmp", $filename, "$file/index.html" )) {
      print STDERR " updating $file/index.html from $filename\n" if $debug;
      unlink( "$file/index.html" );
      rename( "$filename", "$file/index.html" );
      chmod( 0644, "$file/index.html" );
    } else {
      print STDERR " no change in $file/index.html\n" if $debug;
      unlink( "$filename" );
    }
  } else {
    print STDERR " creating $file/index.html\n" if $debug;
    rename( "$filename", "$file/index.html" );
    chmod( 0644, "$file/index.html" );
  }
}
