#!/usr/bin/perl
print "Content-type: text/html\n\n";

open MEMBERS, "members.txt";

foreach (<MEMBERS>) {
	($id, $data) = split /#/, $_;
	$MEMBERS{$id} = $data;
}

close MEMBERS;

open HTML, "mitglieder.htm";

foreach (<HTML>) {
	$template .= $_;
}

close HTML;

$html = "
<tr bgcolor=\"#%%COLOR2%%\">\n
	<td width=\"50\" align=\"center\"><font class=\"list\">%%MEMBER_ID%%</font></td>\n
	<td width=\"1\" bgcolor=\"#%%COLOR%%\"><img src=\"spacer.gif\" height=\"1\" width=\"1\" /></td>\n
	<td width=\"90\"><font class=\"list\">%%CALLSIGN%%</font></td>\n
	<td width=\"1\" bgcolor=\"#%%COLOR%%\"><img src=\"spacer.gif\" height=\"1\" width=\"1\" /></td>\n
	<td width=\"219\"><font class=\"list\">%%FULLNAME%%</font></td>\n
	<td width=\"1\" bgcolor=\"#%%COLOR%%\"><img src=\"spacer.gif\" height=\"1\" width=\"1\" /></td>\n
	<td width=\"250\"><font class=\"list\">%%CITY%%</font></td>\n
	<td width=\"1\" bgcolor=\"#%%COLOR%%\"><img src=\"spacer.gif\" height=\"1\" width=\"1\" /></td>\n
	<td width=\"24\" align=\"center\" bgcolor=\"#%%COLOR2%%\">%%PIC%%</td>\n
</tr>";

$pic = "<a href=\"javascript:omPic('%%CALLSIGN%%')\" /><img src=\"kamera.gif\" border=\"0\" /></a>";

$color2 = 'ffffff';
$color  = 'cccccc';

$mitgliederliste = '';

foreach (sort keys %MEMBERS) {

	$row = $html;
	$row =~ s/%%COLOR2%%/$color2/g;	
	$row =~ s/%%COLOR%%/$color/g;

	$row =~ s/%%MEMBER_ID%%/$_/;

	my @data = split /:/, $MEMBERS{$_};


	$call = lc $data[0];
	open PIC, "$call.jpg" or my $nopic = 1;

	$data[0] =~ s/^\s+|\s+$//g;
	$data[1] =~ s/^\s+|\s+$//g;
	$data[2] =~ s/^\s+|\s+$//g;

	$row =~ s/%%CALLSIGN%%/$data[0]/g;
	$row =~ s/%%FULLNAME%%/$data[1]/g;
	$row =~ s/%%CITY%%/$data[2]/g;

	if ($nopic != 1) {
		$row =~ s/%%PIC%%/$pic/;
		$row =~ s/%%CALLSIGN%%/$data[0]/g;
	}

	$row =~ s/%%(.*)%%//g;

	$mitgliederliste .= "$row\n\n";

	if ($color eq 'ffffff') {
		$color = 'cccccc';
		$color2 = 'ffffff';
	}
	else {
		$color = 'ffffff';
		$color2 = 'cccccc';
	}
}

$template =~ s/%%MEMBERS%%/$mitgliederliste/;
print $template;
