#!/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 use File::Find; use File::Copy; use File::Temp qw( tempfile ); use strict; my $debug = $ENV{'DEBUG'}||0; my $doit = $ENV{'DOIT'} ||0; my $adsense =<<"ADVERT"; ADVERT 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 $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"; The Workshop - $title

$adsense

HEADER my $origin = $file; $origin =~ s@public_html/hacks/workshop@src@; if ( open( HEADER, "<$origin/header.html" )) { while ( my $l =
) { print $fh $l; } close( HEADER ); } else { print $fh <<"HEADER";

$title

This page contains links to some of my $title. It is autogenerated from the comments (if any) at the head of each file.

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(
$f(updated $mtime)
\n
); if ( -d "$file/$f" ) { print $fh "Subdirectory
"; } else { open( THISFILE, "$file/$f" ); while ( ) { next if /^#!/; # skip bangsplat line next if /^$/; last if !m{^($| |#|[/ ]+\*|;;)}; # escape things that might get interpreted s/&/\&/g; s//\>/g; print $fh $_; print $fh "
\n"; } close( THISFILE ); print $fh "

\n"; } print $fh "
\n"; } print $fh <<"TRAILER";

Waider Ugh. Did I write that?
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" ); } }