#!/usr/bin/perl -w
use strict;

use lib '/home/joe';

use DBI;
use Data::Dumper;
use Joe;


if (-f 'index.php') {
  print "already published.\n";
  exit;
}

open (IN, "< index.html") || die "can't read index.html";
my $index_html = join "", <IN>;
close(IN);

my $extended_title = 0;
my ($title) = $index_html =~ /<TITLE>The Urinals? of ([^<]+)<\/TITLE>/i;
unless ($title) {
  ($title) = $index_html =~ /<TITLE>([^<]+)<\/TITLE>/i;
  $extended_title = 1 if $title; 
}

my ($subtitle) = $index_html =~ /<h3>(.*?)<\/h3>/s;
$subtitle =~ s/\n/ /g;
$subtitle =~ s/\s+/ /g;
$subtitle .= '.' unless $subtitle =~ /\.$/;

my ($copyright) = $index_html =~ /Copyright ([\d\-]+)/;

my $images = [];

my @image_info = $index_html =~ 
    /img\s+src=(\S+)\s+width="?(\d+)"?\s+height="?(\d+)"?>.*?<td>(.*?)<\/td>/sg;
while (my ($src, $width, $height, $comment) = splice(@image_info, 0, 4)) {
  push @$images, {
    src => $src,
    width => $width,
    height => $height,
    comment => $comment,
  };
}

@image_info = $index_html =~ 
  /img\s+src=(\S+)\s+height="?(\d+)"?\s+width="?(\d+)"?>.*?<td>(.*?)<\/td>/sg;
while (my ($src, $height, $width, $comment) = splice(@image_info, 0, 4)) {
  push @$images, {
    src => $src,
    width => $width,
    height => $height,
    comment => $comment,
  };
}

unless (@$images) {
  @image_info = $index_html =~ 
    /img\s+src=(\S+)\s+width="?(\d+)"?\s+height="?(\d+)"?>.*?<p>(.*?)<\/?p>/isg;
}
while (my ($src, $width, $height, $comment) = splice(@image_info, 0, 4)) {
  push @$images, {
    src => $src,
    width => $width,
    height => $height,
    comment => $comment,
  };
}

@image_info = $index_html =~
    /img\s+src="?([^">\s]+)\s*>.*?<p>(.*?)<\/?p>/isg;
while (my ($src, $comment) = splice(@image_info, 0, 2)) {
  push @$images, {
    src => $src,
    width => undef,
    height => undef,
    comment => $comment,
  };
}

my ($email_src) = $index_html =~ m|img_src=/.*?/([\w.-]+)|;
my ($contributor) = $index_html =~ /<small>(.*?)<\/small>/s;
if ($contributor) {
  $contributor =~ s/\n/ /g;
  $contributor =~ s/\s+/ /g;
  $contributor =~ s/^\s+//g;
  $contributor =~ s/\s+$//g;
}

my ($name) = `pwd` =~ /([\w-]+)$/;

my $dbh = DBI->connect("DBI:mysql:database=udn", "joe", "fixture") ||
  die "couldn't connect";

my $sth = $dbh->prepare('insert into gallery 
  (name, title, extended_title, subtitle, copyright, contributor) values 
  (?, ?, ?, ?, ?, ?)'
);
$sth->execute($name, $title, $extended_title, $subtitle, $copyright, 
  $contributor);

my $gallery_id = $dbh->last_insert_id(undef, undef, 'gallery', 'id');

my $sequence = 1;
foreach my $image (@$images) {
  my $src = $image->{src};
  my $height = $image->{height};
  my $width = $image->{width};
  my $comment = $image->{comment};

  if ($src =~ /"/) {
    ($src) = $src =~ /^"(.*)"$/;
  }
  if ($src =~ /\//) {
    ($src) = $src =~ /\/([^\/]+)$/;
  }
  my $email = ($src eq $email_src) ? 1 : 0;

  if ($comment =~ m!/cgi-bin/email.pl!) {
    $comment = '';
  } else {
    $comment =~ s/\n/ /g;
    $comment =~ s/\s+/ /g;
    $comment =~ s/^\s*<br>\s*//i;
    $comment =~ s/\s*<br>\s*$//i;
  }

  unless ($height and $width) {
    ($width, $height) = Joe->size($src);
    Joe->clean_up();
  }

  my $sth = $dbh->prepare("insert into images 
     (src, comment, gallery_id, sequence, width, height, email) 
      values (?, ?, ?, ?, ?, ?, ?)");
  $sth->execute(
    $src, $comment, $gallery_id, $sequence, $width, $height, $email);
  $sequence++;
}

$dbh->disconnect();

