#!/usr/bin/newlisp

(define (indent strarg pfxarg) (replace "([^\r\n]+)([\r\n]+)" strarg (string (or pfxarg "    ") $1 $2) 4))

(define (fmt-timesig arg) (format {\time %s} (or arg timesig)))
(define (fmt-key arg) (format {\key %s \%s} (pop (parse (or arg key))) (pop (parse (or arg key)) 2)))
(define (fmt-tempo arg) (format {\tempo 4=%d} (or arg tempo)))

(define (pause) (format "%s s2 %s" (fmt-tempo 127) (fmt-tempo)))
(define (fermata arg) (format "%s %s\fermata %s" (fmt-tempo (div tempo 2)) arg (fmt-tempo)))

(define (header) (format
{\version "2.11.34"

\header {
	title = "%s"
	source = "%s"
	sourcepage = "%d"
	poet = "%s"
	composer = "%s"
	arranger = "%s"
	translator = "%s"
	tagline = \markup { \column {
		\fill-line { "License: Creative Commons Attribution 2.5" "Reactor Core Hymn Repository" }
		\fill-line { "Created: %s" \with-url #"http://hymns.reactor-core.org/" "http://hymns.reactor-core.org/" }
	}}
}

#(set-global-staff-size 16)
#(set-default-paper-size "letter" 'portrait)

\paper {
	print-first-page-number = ##t
	first-page-number = %d
	indent = 0\mm
}
}
	(or title "YouForgotTheTitle")	
	sourcebook
	sourcepage
	poet
	composer
	arranger
	translator
))

(define (layout) (format
{	\layout {
		\context { \Staff
			\override TimeSignature #'style = #'()
			printPartCombineTexts = ##f
		}
		\context { \Score
			\override MetronomeMark #'transparent = ##t
			\remove "Bar_number_engraver"
		}
		\context { \ChoirStaff
			\accepts "Lyrics"
		}
		\context { \Lyrics
			\override LyricSpace #'minimum-distance = #1.2
			\override LyricText #'self-alignment-X = #LEFT
			\consists "Bar_engraver"
			\consists "Separating_line_group_engraver"
			\override BarLine #'transparent = ##t
		}
	}
    \midi {
		\context { \Staff
			midiInstrument = #"%s"
		}
	}
}
	(or instrument "church organ")
))

(define (relative clef notes) (format {\relative %s { %s }} (if (= clef "treble") "g'" "f") notes))

(define (staff clef voice1 voice2) (format
{\new Staff <<
			\clef %s
			%s
			%s
			%s
{
%s
%s
}
			\\
{
%s
%s
}
		>>}
	clef
	(fmt-key)
	(fmt-timesig)
	(fmt-tempo)
	(relative clef (voice1 1))
	(relative clef (voice1 2))
	(relative clef (voice2 1))
	(relative clef (voice2 2))))

(define (make-pdf)
	(print (format
{%s

\score {
	\new ChoirStaff <<
		%s
		%s
	>>
%s
}
}
	(header)
	(staff "treble" soprano alto)
	(staff "bass" tenor bass)
	(layout))))

(context 'PDF)
(define (emit) (print (format "PDF %s\n" MAIN:x)))
(context 'HTML)
(define (emit) (print (format "HTML %s\n" MAIN:x)))
(context 'MIDI)
(define (emit) (print (format "MIDI %s\n" MAIN:x)))
(context MAIN)

;;;
;;; For the given hymn, produce the following:
;;;
;;    * PDF sheet music, lyrics and notes.
;;    * MP3 soprano, alto, tenor, bass, separate and combined.
;;    * OGG soprano, alto, tenor, bass, separate and combined.
;;    * MIDI soprano, alto, tenor, bass, separate and combined.
;;    * HTML page for the hymn, lyrics and PNG image of the notes.
;;    * HTML page for the hymnal, linked to hymn page, PDF, and combined files.

;;; Load and interpret every hymn file given on the command line.

(map (fn (x) (map (fn (y) (load x) (y:emit)) (list PDF HTML MIDI))) (2 (main-args)))

(exit 0)