#!/bin/bash
#
# Regenerage all the images from the crode blocks in the index.html
#
if [ "X$1" != "X-f" ]; then
  echo >&2 "ABORTING generation for bugs examples -- use '-f' for force"
  exit 10
else
  shift
fi

page_bg_color=LightSteelBlue

echo "Removing all old images"
rm -f *.png
rm -rf .xvpics

convert -background $page_bg_color -pointsize 18 \
        label:"`identify -version | head -1 | cut -d\  -f2-4`"  version.gif
gifsicle -O -b version.gif
chmod 644 version.gif

perl -0777ne '
  for ( /<CODE>\s*\n(.*?)<\/CODE>/sg ) {
    for ( split /(?<!\\)\n/ ) {
      s/\s+$//;
      next if /^\s*#/;
      next if /^$/;
      s/&lt;/</;
      s/&gt;/>/;
      s/&amp;/&/;

      ( $image = $_ ) =~ s/.*\s+//s  and
        print "Generating \"$image\" ...\n";

      print "$_\n";
      system $_;

      chmod 0644, $image;

#       if ( $image =~ /.gif$/ ) {  # compress gifs
#         print "    Optimizing GIF format of \"$image\"...\n";
#         system "gifsicle -O -b \"$image\"";
#       }

#       if ( ( my $jpeg = $image ) =~ s/.png$/.jpg/ ) {
#         print "    Converting \"$image\" to \"$jpeg\" ...\n";
#         system "convert \"$image\" -threshold -1 \\
#                         -fill '"$page_bg_color"'  -opaque white \\
#                         +matte miff:- | composite \"$image\" - \"$jpeg\"
#                ";
#         chmod 0644, $jpeg;
#       }
    }
  }' index.html

echo "DONE"

