#!/usr/bin/perl -w

use strict;

my $in = shift || die;

open FH, "$in" || die;

print "<?xml version=\"1.0\"?>\n";
print "<SLIDESHOW title=\"$in\">\n";
print "  <current_slide>0</current_slide>\n";

my $page_num = 0;

my $state = "nothing";
my $top_pixel;

my $default_state = 1; # Has ``default'' been turned off on this page?
my $line_no; # the line of the page that we're on
my $size = 0;
my $centre = 0;
my $italic = 0;

while ( my $foo = <FH> ) {
    chop $foo;

    if (substr($foo, 0, 1) eq "#") {
	next;
    } elsif (substr($foo, 0, 1) eq "%") {
	# Command mode
	if (substr($foo, 1) eq "page") {
	    # New page
	    if ($page_num) {
		print "  </SLIDE>\n";
	    }
	    $page_num++;
	    print "  <SLIDE>\n";
	    print "    <index>$page_num</index>\n";
	    
	    $state = "in page";
	    $default_state = 1;
	    $line_no = 0;
	    $top_pixel = 2;
	    $size = 0;
	    $centre = 0;
	    $italic = 0;
	} elsif (substr($foo, 1, 3) eq "bar") {
	    print "    <RECT>\n";
	    print "      <color>(40179,40179)</color>\n";
	    print "      <top_left>(0.000000,$top_pixel)</top_left>\n";
	    print "      <bottom_right>(1024.000000," . ($top_pixel + 7) . ")</bottom_right>\n";
	    print "    </RECT>\n";
	    $top_pixel += 12;
	} elsif (substr($foo, 1) eq "nodefault") {
	    $default_state = 0;
	} elsif (substr($foo, 1, 4) eq "size") {
	    $size = substr($foo, 5);
	} elsif (substr($foo, 1, 5) eq "image") {
	    my $name = substr($foo, 8); # strip the quote
	    chop $name; # strip the quote

	    print "    <IMAGE>\n";
	    print "      <top_left>(0.000000,$top_pixel)</top_left>\n";
	    print "      <filename>$name</filename>\n";
	    print "    </IMAGE>\n";
	} elsif (substr($foo, 1) eq "center") {
	    $centre = 1;
	} elsif (substr($foo, 1) eq "italic") {
	    $italic = 1;
	} elsif (substr($foo, 1) eq "normal") {
	    $italic = 0;
	}
    } else {
	if ($state eq "in page") {
	    $line_no++;
	    my $line_height = 32;

	    if (0 && $default_state && $line_no == 3) {
		# Add the default bar
		print "    <RECT>\n";
		print "      <color>(40179,40179)</color>\n";
		print "      <top_left>(0.000000," . ($top_pixel + 10) . ")</top_left>\n";
		print "      <bottom_right>(1024.000000," . ($top_pixel + 17) . ")</bottom_right>\n";
		print "    </RECT>\n";
		$top_pixel += 12;
	    }

	    # Process any tabs--we go two deep for now, will need to add
	    # another level with arrows for real magicpoint compatibility
	    # mode.
	    my $tabs = 0;
	    while (1) {
		if (substr($foo, $tabs, 1) eq "\t") {
		    $tabs++;
		} else {
		    last;
		}
	    }

	    # top_pixel is the y-coordinate of the upper left of the text field
	    if ($tabs == 0) {
		if ($default_state && $line_no == 2) {
		    $line_height = 23;
		} elsif ($default_state && $line_no == 1) {
		    $line_height = 38;
		} else {
		    $line_height = 38;
		}

		if ($foo) {
		    print "    <TEXT>\n";
		    if ($centre) {
			print "      <justify>centre</justify>\n";
			print "      <top_left>(512.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(512.000000,$top_pixel)</bottom_right>\n";
		    } else {
			print "      <top_left>(8.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(8.000000,$top_pixel)</bottom_right>\n";
		    }
		}
	    } elsif ($tabs == 1) {
		print "    <TEXT>\n";
		    if ($centre) {
			print "      <justify>centre</justify>\n";
			print "      <top_left>(512.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(512.000000,$top_pixel)</bottom_right>\n";
		    } else {
			print "      <top_left>(65.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(65.t000000,$top_pixel)</bottom_right>\n";
		    }
		$line_height = 38;
	    } elsif ($tabs == 2) {
		print "    <TEXT>\n";
		    if ($centre) {
			print "      <justify>centre</justify>\n";
			print "      <top_left>(512.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(512.000000,$top_pixel)</bottom_right>\n";
		    } else {
			print "      <top_left>(110.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(110.000000,$top_pixel)</bottom_right>\n";
		    }
		$line_height = 30;
	    } elsif ($tabs == 3) {
		print "    <TEXT>\n";
		    if ($centre) {
			print "      <justify>centre</justify>\n";
			print "      <top_left>(512.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(512.000000,$top_pixel)</bottom_right>\n";
		    } else {
			print "      <top_left>(140.000000,$top_pixel)</top_left>\n";
			print "      <bottom_right>(140.t000000,$top_pixel)</bottom_right>\n";
		    }
		$line_height = 24;
	    }

	    if ($size == 5) {
		$line_height = 38;
	    } elsif ($size == 4) {
		$line_height = 30;
	    } elsif ($size == 3) {
		$line_height = 24;
	    } elsif ($size == 4) {
		$line_height = 16;
	    }

	    goto done unless ($foo);

	    if ($italic || ($default_state && $line_no == 1)) {
		print "      <font>-*-utopia-bold-i-*-*-$line_height-*-*-*-*-*-iso8859-1</font>\n";
	    } else {
		print "      <font>-*-utopia-bold-r-*-*-$line_height-*-*-*-*-*-iso8859-1</font>\n";
	    }

	    $foo =~ s/&/&amp\;/g;
	    $foo =~ s/</&lt\;/g;
	    $foo =~ s/>/&gt\;/g;

	    print "      <text>" . substr($foo, $tabs) . "</text>\n";
	    if (1) {
		print "      <color>65535</color>\n";
	    } else {
		print "      <color>0</color>\n";
	    }
	    print "    </TEXT>\n";

	    if ($tabs == 1) {
		# Make the green rectangle
		print "    <RECT>\n";
		print "      <color>(2016,2016)</color>\n";
		print "      <top_left>(45.000000," . ($top_pixel + 15) . ")</top_left>\n";
		print "      <bottom_right>(55.000000," . ($top_pixel + 25) .
		  ")</bottom_right>\n";
		print "    </RECT>\n";
	    } elsif ($tabs == 2) {
		# Make the yellow circle
		print "    <OVAL>\n";
		print "      <color>(65504,65504)</color>\n";
		print "      <top_left>(95.000000," . ($top_pixel + 11) . ")</top_left>\n";
		print "      <bottom_right>(105.000000," . ($top_pixel + 21) . ")</bottom_right>\n";
		print "    </OVAL>\n";
	    }

	  done:
	    $top_pixel += $line_height;
	}
    }
}

# Clean up any dangling tags
if ($state eq "in page") {
    print "  </SLIDE>\n";
}

print "</SLIDESHOW>\n";
