#
#	malign.awk
#
#  -----------------------------------------------------------------------
#  Copyright 2007  Alfred Steffens Jr.
#  -----------------------------------------------------------------------
#  -----------------------------------------------------------------------
#    Copying Permission:
#
#    malign 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.
#
#    malign is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with malign (see file called "COPYING"); if not, write to the Free
#    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#    02111-1307  USA
#  ------------------------------------------------------------------------
#
#   Mscore release 1.1
#
#	ver. 1.0	2/12/2005		A. Steffens Jr.
#
#



BEGIN {
	lineno = 1
}


{

	#
	#	read all lines in the file
	#
	rawline[lineno] = $0

	#
	#	find the maximum width of each column
	#
	if (index($0, "@")) {

		numfields = NF

		#
		#	line contains notes (tracks)
		#
		for (col=1; col<=numfields; col++) {

			len = length($col)
			if (col in maxwidth) {
				if (len > maxwidth[col]) {
					maxwidth[col] = len
				}
			}
			else {
				maxwidth[col] = len
			}

		} # for -- col

	}
	else {

		if ($2 == "MIDI" || $2 == "META") {

			len = length($1)
			if (1 in maxwidth) {
				if (len > maxwidth[1]) {
					maxwidth[1] = len
				}
			}
			else {
				maxwidth[1] = len
			}

		} # if

	} # if


	lineno++
}



END {

	total_lines = lineno-1


	#
	#	now determine where the center marks should be
	#

	#
	#	first column (score duration)
	#
	center[1] = int((maxwidth[1] + 2) / 2) + 1
	for (col=2; col<=numfields; col++) {

		center[col] = center[col-1] + int((maxwidth[col-1]+2)/2) + int((maxwidth[col]+2)/2) + 1

	} # for -- col


	sep_length = center[numfields] + int((maxwidth[numfields]+2)/2) + 1
	separator_string = make_separator(sep_length)


	#
	#	re-print the file
	#
	for (lineno=1; lineno<=total_lines; lineno++) {

		aerr = split(rawline[lineno], ary, " ")

		if (index(rawline[lineno], "@")) {

			if (aerr != numfields) {
				print "ERROR: array size not equal num fields"
				exit
			}

			tmpstr = ""
			lastpos = 0
			for (j=1; j<=aerr; j++) {

				width = length(ary[j])
				halfwidth = int(width/2)
				maxwd = maxwidth[j]
				halfmaxwd =  int(maxwd/2)

				pad = center[j] - lastpos - halfwidth - 1
				padstr = string_of_n_spaces(pad)

				tmpstr = sprintf("%s%s%s", tmpstr, padstr, ary[j])			

				lastpos = length(tmpstr)
			} # for

			print tmpstr
			last_line = 1

		} # if -- index
		else {

			if (is_event_bar(rawline[lineno])) {

				width = length(ary[1])
				halfwidth = int(width/2)
				maxwd = maxwidth[1]
				halfmaxwd =  int(maxwd/2)
	
				pad = center[1] - halfwidth - 1
				padstr = string_of_n_spaces(pad)
	
				tmpstr = sprintf("%s%s      ", padstr, ary[1])			
				for (j=2; j<=aerr; j++) {
					tmpstr = sprintf("%s      %s", tmpstr, ary[j])
				}
				print tmpstr
				last_line = 2

			}
			else {

				if (is_a_separator(rawline[lineno])) {
					print separator_string
				}
				else {
					print rawline[lineno]
				}
			}

		} # else

	} # for -- lineno

}



function string_of_n_spaces(n,   i,tmpstr)
{

	if (n <= 0) {
		return ""
	}

	tmpstr = ""
	for (i=1; i<=n; i++) {
		tmpstr = sprintf("%s ", tmpstr)
	}
	return tmpstr
}
#	------------------------  string_of_n_spaces  ------------------------


function is_event_bar(str)
{

	if (match(str, /MIDI/)) {
		if (match(str, /TK/)) {
			return 1
		}
	}
	else if (match(str, /META/)) {
		if (match(str, /TK/)) {
			return 1
		}
	}

	return 0
}
#	------------------------ is_event_bar --------------------------------



function make_separator(size,     i,tmpstr)
{

	tmpstr = "#   "
	for (i=5; i<=size; i++) {
		tmpstr = sprintf("%s-", tmpstr)
	}
	return tmpstr
}
#	----------------------- make_separator -------------------------------



function is_a_separator(str)
{
	if (index(str, "#	-")) {
		return 1
	}
	return 0
}
#	--------------------- is_a_separator ---------------------------------



