#!/usr/bin/perl -w
#
# htmlizer.pl
# Joel Uckelman
# 12 January 2001
#
my $start_prog = time;

require 5.005;
use DBI;

# Configuration
my ($prog, $ver) = ('htmlizer.pl', '0.2');
my ($scripts_config, $prog_config, $subs) = ('/home/g1/bin/scripts.conf', '/home/g1/bin/htmlizer.conf', '/home/g1/bin/htmlizerlib.pl');

print "$prog version $ver\n";

# Assign to some variables in the config to avoid a warning about using them only once.
$database = $password = $port = $host = $user = '';

# looks nice
my $config_start = time;
print 'configuring: ';

# Load the subs and config files
eval { require $scripts_config; };
die "\n$prog: $scripts_config: $@" if $@;
eval { require $subs; };
die "\n$prog: $prog_config: $@" if $@;
eval { require $prog_config; };
die "\n$prog: $prog_config: $@" if $@;

# Are there targets?
defined $ARGV[0] or die "\n$prog: no target";

# Connect to game database
$db = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $user, $password) or die "\n$prog: database connect failed: $DBI::errstr";

# Flush after each print (for progress dots)
$| = 1;

# Finish remaining configuration
config() or die "\n$prog: config failed"; 
eval { require $outfile; };
die "\n$prog: $outfile: $@" if $@;
print 'done, ', time-$config_start, "s\033[60G[ ok ]\n";

# Iterate over target list
my $target;
while ($target = shift @ARGV) {
	my $start_target = time;
	print "making $target: ";

	die "\n$prog: undefined target '$target'" unless grep /$target/, (@all, 'all');

	if ($target eq 'all') {
		unshift @ARGV, @all;
		$result = $target;
	}
	else {
		die "\n$prog: undefined target array '$target'" unless defined(@$target);

		# Execute each command in the target's command list
		my $output = '';
		foreach $command (@$target) {
			$output .= eval $command;
			die "\n$prog: eval failed at line ", __LINE__, ": $@" if $@;			
		}

		$result = 'ok';
		print 'done, ', time-$start_target, "s\033[60G[ $result ]";
	}	
	print "\n";
}

$db->disconnect or die "\n$prog: database disconnect failed: $DBI::errstr"; # Wierd! Should never happen.
print 'done, ', time-$start_prog, "s\n";

