#  Copyright 1998-2008 "Nosey" Nick Waterman, Ward Cunningham and Jim Wilson
#  Distributed under the GNU GPL V2 license.
#  See http://noseynick.net/va3nnw/cw/ and http://c2.com/morse
#
#  This file is part of the Nilex Morse Tutor.
#
#  The Nilex Morse Tutor 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.
#
#  The Nilex Morse Tutor 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 the Nilex Morse Tutor: http://noseynick.net/va3nnw/cw/license.txt
#  If not, write to the Free Software Foundation, Inc., 59 Temple
#  Place, Suite 330, Boston, MA  02111-1307 USA

FREQS  = 440 500 550 600 700 880
SPEEDS = 15 20 25 30 35 40
SYMS   = A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
SYMS  += 0 1 2 3 4 5 6 7 8 9 stop slash equal question comma cq

SYM_stop  = .
SYM_slash = /
SYM_equal = =
SYM_question = '?'

diag:
	@echo $(words $(FREQS)) freqs
	@echo $(words $(SPEEDS)) speeds
	@echo $(words $(SYMS)) syms
	@echo -n "total mp3 files: "
	@echo $(words $(FREQS)) $(words $(SPEEDS)) $(words $(SYMS)) '**p' | dc
	@echo you can:
	@echo ' "make rz" to make all the above, or'
	@echo ' "make sm2" to just make the 7 mp3s required by the sm2 version'
	@echo ' "make all" to make both'

MPTHREES = $(FREQS:%=%hz.mp3) null.mp3
TOP=$(shell pwd)/
SUBMAKE = mkdir -p $1; cd $1; make -f $(TOP)Makefile TOP=$(TOP)
LAME = lame -s 16 --bitwidth 8 --unsigned -m m --preset phone --nohist

rz:  cw/all-freqs
sm2: $(MPTHREES)
all: rz sm2

%.mp3: %.raw
	$(LAME) $^ $@
%hz.raw: $(TOP)cw.pl
	$^ $* +0 sine > $@
null.raw: $(TOP)cw.pl
	$^ +0 +0 sine > $@
clean:
	rm -f $(MPTHREES) $(FREQS:%=%hz.wav) null.wav

%/all-freqs:
	$(call SUBMAKE,$*) all-freqs

all-freqs: $(FREQS:%=%hz/all-speeds)
%hz/all-speeds:
	$(call SUBMAKE,$*hz) FREQ=$* all-speeds

all-speeds: $(SPEEDS:%=%wpm/all-syms)
%wpm/all-syms:
	$(call SUBMAKE,$*wpm) FREQ=$(FREQ) SPEED=$* all-syms

TRANS_SYM = $(if $(SYM_$(SYM)),$(SYM_$(SYM)),$(SYM))
define ONE_SYM
 all-syms: $(SYM).mp3
 $(SYM).mp3: $(TOP)cw.pl
	$$^ $(FREQ) $(SPEED) $(TRANS_SYM) > $$@.raw
	$(LAME) $$@.raw $$@
	rm $$@.raw
endef
# BLOODY LAME REFUSING TO READ STDIN PROPERLY!
$(foreach SYM,$(SYMS),$(eval $(ONE_SYM)))
