#!/usr/local/bin/perl
############################################################################
#            cpm.pl  -  Convert from ChordPro to HTML                      #
#                   -----------------------------                          #
#    begin                : 14-02-2001                                     #
#    copyright            : (C) 2001 by Matteo Merli                       #
#    email                : merlim@libero.it                               #
############################################################################
#                                                                          #
############################################################################
#                                                                          #
#   This program is free software; you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by   #
#   the Free Software Foundation; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
#                                                                          #
############################################################################

#--- Print the head of page ---#
sub head {
	print "
<html>
<head>
	<style type=\"text/css\"><!--
	body { margin-left: 2cm; margin-right: 2cm}
--></style>
	<title>KhordPro</title>
</head>
<body>
	";
}

#--- Print the end of page --- #
sub end {
	print "
	<p>&nbsp;</p>
	<p>&nbsp;</p>
</body>
</html>\n
	";
}

#--- MAIN  ---#
$filename = $ARGV[0];
open(FILE,"<$filename") or die "Error opening the file\n\n";
head;
$chords_code = "";
$chorus = 0;
$tab = 0;
while ($row = <FILE>) {
	#$row =~ s/\^M//g;
	$row1 ="";
	$row2 = "";

	#-- Start of Tab --#
	if ($row =~ /{(start_of_tab|sot)}/) {
		$tab = 1;
		goto done;
	}
	#-- End of Tab --#
	if ($row =~ /{(end_of_tab|eot)}/) {
		$tab = 0;
		goto done;
	}
	#-- Write Tab in monospaced char --#
	if ($tab==1) {
		$row =~ s/\s/&nbsp;/g;
		print "<tt>$row</tt><br>";
		goto done;
	}

	#-- Extract chords --#
	while ($row =~ /([^\[\]]*)\[([^\]]*)\]([^\[\]]*)(.*)/) {
		if ($1 ne "") {
			$row1 .= "<td></td>\n";
			$row2 .= "<td><font face=\"times\">$1</font></td>\n";
		}
		$row1 .= "<td><i><font color=\"blue\">$2</td>\n";
		$row2 .= "<td><font face=\"times\">$3</font></td>\n";
		$row = $4;
	}
	if ($row1 ne "" || $row2 ne "") {
		if ($chorus==1) {
			$row1 = "<td width=40></td>".$row1;
			$row2 = "<td width=40></td>".$row2;
		}
	print "
	<table border=0>
		<tr>
		$row1
		</tr>
		<tr>
		$row2
		</tr>
	</table>
		";
		goto done;
	}

	#-- Title --#
	if ($row =~ /{(title|t):(.*)}/) {
		print "<h1 align=\"center\"><font face=\"times\">$2</font></h1>";
		goto done;
	}

	#-- Sub Title --#
	if ($row =~ /{(subtitle|st):(.*)}/) {
		print "<h3 align=\"center\">$2</h3>";
		goto done;
	}


	#-- Comments like {c:comment} --#
	if ($row =~ /{(c|comment):(.*)}/) {
		print "<p><font color=\"red\">$2</font></p>";
		goto done;
	}
	#-- Comments italic {ci:assda} --#
	if ($row =~ /{(ci|comment_italic):(.*)}/) {
		print "<p><i><font color=\"red\">$2</font></i></p>";
		goto done;
	}
	#-- Comments box {cb:assda} --#
	if ($row =~ /{(cb|comment_box):(.*)}/) {
		print "<table border=1 bgcolor=#F7FCAB><td>
		<font color=\"red\">$2
		</font></td></table>";
		goto done;
	}

	#-- Ignore comments with # --#
	if ($row =~ /^(\s*#)/) {
		#$row =~ s/#//;
		#$row =~ s/\s/&nbsp;/g;
		#print "<p><i>$row</i></p>";
		goto done;
	}

	#	Draw the Chords
	#like:
	#      A
	# 0||-------
	#  ||---2---
	#  ||---2---
	#  ||---2---
	# 0||-------
	# X||-------
	if ($row =~ /{define: *([^\s]+) *(base-fret)? *([^\s]+) *(frets)?
*([^\s]+) *([^\s]+) *([^\s]+) *([^\s]+) *([^\s]+) *([^\s]+) *}/) {

	if ($5 eq "-1" or $5 eq "-" or $5 eq "x") {$m1="X"; $t1="-";}
		else {if ($5 eq "0") {$m1="0"; $t1="-";}
			else {$m1="&nbsp;"; $t1=$5;}}
	if ($6 eq "-1" or $6 eq "-" or $6 eq "x") {$m2="X"; $t2="-";}
		else {if ($6 eq "0") {$m2="0"; $t2="-";}
			else {$m2="&nbsp;"; $t2=$6}}
	if ($7 eq "-1" or $7 eq "-" or $7 eq "x") {$m3="X"; $t3="-"}
		else {if ($7 eq "0") {$m3="0"; $t3="-"}
			else {$m3="&nbsp;"; $t3=$7}}
	if ($8 eq "-1" or $8 eq "-" or $8 eq "x") {$m4="X"; $t4="-"}
		else {if ($8 eq "0") {$m4="0"; $t4="-"}
			else {$m4="&nbsp;"; $t4=$8}}
	if ($9 eq "-1" or $9 eq "-" or $9 eq "x") {$m5="X"; $t5="-"}
		else {if ($9 eq "0") {$m5="0"; $t5="-"}
			else {$m5="&nbsp;"; $t5=$9}}
	if ($10 eq "-1" or $10 eq "-" or $10 eq "x") {$m6="X"; $t6="-"}
		else {if ($10 eq "0") {$m6="0"; $t6="-"}
			else {$m6="&nbsp;"; $t6=$10}}

	$chords_code .= "<pre>
<table border=1 align=\"left\"><tr><td>
	<p align=\"center\"><font color=\"blue\">$1</font></p>
	<p align=\"right\">
	   $3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
	<code>$m1||---$t1---&nbsp;<br>
	$m2||---$t2---&nbsp;<br>
	$m3||---$t3---&nbsp;<br>
	$m4||---$t4---&nbsp;<br>
	$m5||---$t5---&nbsp;<br>
	$m6||---$t6---&nbsp;<br></code>
	</p>
	</td></tr>
</table></pre>
	";
	goto done;
	}

	#-- Start of Chorus --#
	if ($row =~ /{(start_of_chorus|soc)}/) {
		$chorus = 1;
		#print "<font color=\"green\">";
		goto done;
	}
	#-- End of Chorus --#
	if ($row =~ /{(end_of_chorus|eoc)}/) {
		$chorus = 0;
		goto done;
	}

	#-- Default: print the row without changes--#
	print "<p align=\"left\"><font face=\"times\">$row</font></p>";

	done:
}
print "<hr>";
if ($chords_code ne "") {
	print "$chords_code<br><br><br><br><br><br>
	<br><br><br><br><br><br>";
}
end;



