+mkdir 'static/js';
+
+sub gzip_file {
+ my ($file) = @_;
+ my $zopfli = which 'zopfli';
+ system $zopfli => $file if $zopfli;
+ gzip $file => "$file.gz", -Level => 9, Minimal => 1 unless $zopfli;
+}
+
+sub write_gzfile {
+ my ($file, @content) = @_;
+ write_file $file, @content;
+ gzip_file $file
+}
+
+sub read_css_into_blocks {
+ my ($file) = @_;
+ my (@blocks, $block);
+ for (read_file $file) {
+ $block .= $_;
+ if (/^}/) {
+ push @blocks, $block;
+ $block = '';
+ }
+ }
+ \@blocks
+}
+
+my $default_theme = 'cyborg';
+
+sub theme_prefix {
+ my ($theme, $decl, $default) = @_;
+ return $decl if $theme eq $default_theme || !$decl;
+ return '' if $decl eq $default;
+
+ $default =~ s/[^{]*{\n//;
+ $default =~ s/\n}[^}]*//;
+ $decl =~ s/^$_$//m for split "\n", $default;
+ $decl =~ s/\n+/\n/g;
+
+ my $prefix = "html.$theme";
+ my ($first_line) = $decl =~ /([^{]*){/;
+ $first_line =~ s/(,\s+)/$1 $prefix /g;
+ $first_line = "$prefix $first_line";
+ $decl =~ s/([^{]*){/$first_line\{/;
+ $decl
+}
+
+sub make_css {
+ my $css = join '', map { read_file $_ } <css/*.css>;
+
+ my (%themes);
+ for (<css/themes/*>) {
+ my ($theme) = m,themes/(.*)\.css,;
+ $themes{$theme} = read_css_into_blocks $_;
+ }
+ my @themes = sort grep { $_ ne $default_theme } keys %themes;
+
+ while (grep { scalar @$_ } values %themes) {
+ my %blocks = map { $_ => (shift @{$themes{$_}}) // '' } keys %themes;
+ $css .= $blocks{$default_theme};
+ $css .= theme_prefix $_, $blocks{$_}, $blocks{$default_theme} for @themes
+ }
+
+ write_gzfile "static/css/all.css", minify $css
+}
+
+sub make_js {
+ system java => -jar => 'compiler.jar', qw,-O SIMPLE --create_source_map static/js/js.map --js_output_file static/js/all.js --language_in ECMASCRIPT6_STRICT --language_out ECMASCRIPT5_STRICT --source_map_location_mapping js/|/static/js/,, <js/*>;
+ my $js = read_file 'static/js/all.js';
+ write_gzfile 'static/js/all.js', '//# sourceMappingURL=/static/js/js.map', "\n", $js;
+ system 'cp', '-rp', 'js', 'static/';
+}
+
+my $css_mtime = -M 'static/css/all.css' // 0;
+for (<css/*>, <css/themes/*>) {
+ if (!$css_mtime || $css_mtime > -M) {
+ make_css;
+ last
+ }
+}