aboutsummaryrefslogtreecommitdiff
path: root/gen.pl
blob: 50c7527256f3481d37f0ec9fbb20ad918ab3c4ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl

# gen.pl -- generate static files for perlisdead.org

use 5.010;

use strict;
use warnings;
use experimental 'switch';

use File::Copy;
use FindBin '$Bin';
use Getopt::Std;

# Parse command-line options

my %opt;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('cd', \%opt);

sub main::HELP_MESSAGE {
	print <<HELP;
usage: $0 [-d]
  -c	use cached run results
  -d	enable debug messages
HELP
}
sub main::VERSION_MESSAGE {}

# Change directory to script path

chdir $Bin;

# Generate index.html

mkdir 'out' if ! -d 'out';
open my $src, '<', 'src/index.html' or die "Could not open < src/index.html: $!";
open my $out, '>', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!";
copy 'out/index.html', 'out/index.html.bkp';
select $out;
for (<$src>) {
	when (/^\.inc (.*)/) {
		open my $inc, '<', "inc/$1" or do {
			warn "Could not open inc/$1: $!";
			next;
		};
		print STDERR "INCLUDE: $_\n" if $opt{d};
		print $_ while <$inc>;
		close $inc;
	}
	when (/^\.eval (.*)/) {
		print STDERR "EVAL: $1\n" if $opt{d};
		{
			no strict; no warnings; no experimental;
			eval $1;
			warn $@ if $@;
		}
	}
	when (/^\.run (.*)/) {
		if (not $opt{c}) {
			open my $run, '-|', "run/$1" or do {
				warn "Could not start run/$1: $!";
				next;
			};
			open my $cache, '>', "cache/$1.tmp" or do {
				warn "Could not open cache/$1.tmp: $!";
				next;
			};
			print STDERR "RUN: $1\n" if $opt{d};
			my $i;
			for (<$run>) {
				$i++;
				print;
				print $cache $_;
			}
			close $cache;
			close $run;
			if ($i) {
				move "cache/$1.tmp", "cache/$1";
				next;
			}
		}
		# Fall back to cached run
		open my $cache, '<', "cache/$1" or do {
			warn "Could not open cache/$1: $!";
			next;
		};
		print STDERR "GET CACHED RUN: $1\n" if $opt{d};
		print for <$cache>;
		close $cache;
	}
	default {
		print $_;
	}
}
select STDOUT;
close $out;
close $src;

# Rename temporary file

move 'out/index.html.tmp', 'out/index.html';

# Copy modified static files

opendir $src, 'src' or die "Could not open directory src: $!";
opendir $out, 'out' or die "Could not open directory src: $!";
my %src = map { ($_, (stat "src/$_")[9]||0) } grep { not /^\./ or /^index\.html$/ } readdir $src;
my %out = map { ($_, (stat "out/$_")[9]||0) } grep { not /^\./ or /^index\.html$/ } readdir $out;
for (keys %src) {
	no warnings 'uninitialized';
	if ($src{$_} > $out{$_}) {
		print STDERR "COPY: src/$_ => out/$_\n" if $opt{d};
		copy "src/$_", "out/$_";
	}
}
closedir $out;
closedir $src;

print STDERR "DONE\n" if $opt{d};