#
# GNU Solfege - eartraining for GNOME
# Copyright (C) 2000, 2001, 2002, 2003  Tom Cato Amundsen
#
# 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

@SET_MAKE@

include Makefile.inc

.PHONY : rpm deb 
SUBDIRS = po feta lesson-files mpd online-docs soundcard src test tools topdocs graphics

FAQ INSTALL.win32 README INSTALL : topdocs/defs.texi

%: topdocs/%.texi
	LC_ALL=C makeinfo -I topdocs --no-split --no-headers --output $(notdir $(basename $<)) $<

ifeq ($(WITH_GNOME),no)
SUBDIRS := $(SUBDIRS) gnomeemu
endif

dist_files = AUTHORS COPYING INSTALL FAQ INSTALL.win32 \
	Makefile.in Makefile.inc.in config.h.in \
	README TODO changelog configure configure.ac \
	default.config system.rc finn \
	install-sh py-compile run-solfege.py.in \
	solfege.manpage.xml.in solfege.bat runsrcdir.bat \
	solfege.desktop solfege.gtkrc solfege.py \
	windowsinstaller.iss.in windowsinstaller.iss \
	solfege.redhat8.spec.in solfege.redhat8.spec \
	solfege.redhat9.spec.in solfege.redhat9.spec \
	solfege.suse82.spec.in solfege.suse82.spec \
	acinclude.m4 aclocal.m4 \
	runpy2exe.bat setup.py

easy_dist_patches = $(wildcard easy-patches/*.diff)

ifeq ($(WITH_LOCAL_XMLCATALOG),yes)
SGML_CATALOG_FILES += online-docs/stupid_catalog_file.cat
endif

solfege.1: solfege.manpage.xml
ifeq ($(WITH_LOCAL_XMLCATALOG),yes)
	SGML_CATALOG_FILES=$(SGML_CATALOG_FILES) $(XSLTPROC) --nonet --catalogs  $(dir $(stylesheet_filename))/../manpages/docbook.xsl $<
else
	$(XSLTPROC) $(dir $(stylesheet_filename))/../manpages/docbook.xsl $<
endif

all: AUTHORS INSTALL README FAQ INSTALL.win32
	for dir in $(SUBDIRS); do \
	  $(MAKE) -C $$dir; \
	done

chl:# 2002-12-18 is the time of 1.9.3 release
	cvs2cl -l "-d'>2002-12-29 05:00'" -I "cvsignore" 

po-update:
#	$(XGETTEXT) --keyword=_ $(wildcard src/*.py)
	pygettext -o solfege.pot -p po --keyword=_i $(wildcard src/*.py) \
	   $(wildcard mpd/*.py) \
           $(filter-out lesson-files/CVS,$(wildcard lesson-files/*)) \
	   online-docs/create_toc.py \
                             $(wildcard gnomeemu/gnome/*.py)  
	#	--output-dir=po 
		#--default-domain=$(PACKAGE) --output-dir=po 
		#2> /dev/null
	$(MAKE) -C po po-update

install: all mkinstalldirs
	$(INSTALL_PROGRAM) run-solfege.py $(bindir)/solfege$(VERSION)
	$(INSTALL_PROGRAM) run-solfege.py $(bindir)/solfege
	$(INSTALL_DATA) solfege.gtkrc $(datadir)/$(PACKAGE)/$(VERSION)
	$(INSTALL_DATA) default.config $(datadir)/$(PACKAGE)/$(VERSION)
	$(INSTALL_DATA) solfege.1 $(mandir)/man1
	$(INSTALL_DATA) system.rc $(sysconfdir)/$(PACKAGE)$(MAJOR_VERSION).$(MINOR_VERSION)
ifeq ($(WITH_GNOME),yes)
	mkdir -p $(datadir)/applications/
	$(INSTALL_DATA) solfege.desktop $(datadir)/applications/
else
	$(MAKE) -C gnomeemu install
endif
	$(LOOP)

mkinstalldirs:
	mkdir -p $(mandir)/man1
	mkdir -p $(bindir)
	mkdir -p $(datadir)/$(PACKAGE)/$(VERSION)
	mkdir -p $(sysconfdir)

uninstall:
	$(LOOP)
	rm -f $(mandir)/man1/solfege.1*
	rm -f $(datadir)/$(PACKAGE)/$(VERSION)/solfege.gtkrc
	rm -f $(datadir)/$(PACKAGE)/$(VERSION)/default.config
	rm -f $(sysconfdir)/$(PACKAGE)$(MAJOR_VERSION).$(MINOR_VERSION)
ifeq ($(WITH_GNOME),yes)
	rm -f $(datadir)/applications/solfege.desktop
endif
	rm -f $(bindir)/solfege$(VERSION)
	rm -f $(bindir)/solfege
	rmdir $(datadir)/$(PACKAGE)/$(VERSION) || true
	rmdir $(datadir)/$(PACKAGE) || true

assert_cwd:
ifneq ($(notdir $(PWD)),$(DISTNAME))
	@echo "Sorry, but the current working directory has to be" $(DISTNAME)
	@echo "to do that. PWD=" $(PWD)
	exit -1
endif

EASYDISTNAME=solfege-easybuild-$(VERSION)
easy-dist: all
	rm -rf $(EASYDISTNAME)
	$(MAKE) DISTNAME=$(EASYDISTNAME) dist_workhorse
	$(MAKE) DISTNAME=$(EASYDISTNAME) -C online-docs easy-dist
	for dir in $(wildcard easy-patches/*.diff); do\
		cd $(EASYDISTNAME) && patch -p1 < ../$$dir; cd .. ;\
	done
	cp solfege.1 $(EASYDISTNAME)
	cp soundcard/solfege_c_midi_wrap.c $(EASYDISTNAME)/soundcard
	cd $(EASYDISTNAME) && autoconf && ./configure
	tar --gzip --create --file=../$(EASYDISTNAME).tar.gz $(EASYDISTNAME)
	rm -rf $(EASYDISTNAME)

dist: dist_workhorse
	tar --gzip --create --file=../$(DISTNAME).tar.gz $(DISTNAME)
	rm -rf $(DISTNAME)

dist_workhorse: AUTHORS INSTALL README FAQ INSTALL.win32
	# FIXME
	make -C online-docs/C update-version-strings
	make -C online-docs/no update-version-strings
	make -C online-docs/ru update-version-strings
	make -C online-docs/es_MX update-version-strings
	rm -rf $(DISTNAME)
	mkdir $(DISTNAME)
	cp $(dist_files) $(DISTNAME)
	mkdir $(DISTNAME)/easy-patches
	cp $(easy_dist_patches) $(DISTNAME)/easy-patches
	for dir in debian-dir $(filter-out gnomeemu, $(SUBDIRS)) gnomeemu ; do \
	  $(MAKE) -C $$dir dist; \
	done


win32-dist: AUTHORS INSTALL README FAQ INSTALL.win32
	./configure --without-gnome --without-gtkhtml
	$(MAKE)
	rm -rf $(DISTNAME)
	rm -rf $(PACKAGE)-win32-$(VERSION)
	mkdir $(DISTNAME)
	cp $(dist_files) $(DISTNAME)
	mkdir $(DISTNAME)/easy-patches
	cp $(easy_dist_patches) $(DISTNAME)/easy-patches
	for dir in $(SUBDIRS) ; do \
	  $(MAKE) -C $$dir dist; \
	done

	cp ../solfege-win32-binaries/midiplayer.exe $(DISTNAME)/tools
	$(MAKE) -C soundcard winmidi.pyd
	cp soundcard/winmidi.pyd $(DISTNAME)/soundcard
	cp src/configureoutput.py $(DISTNAME)/src
	sed -e "s/^type=.*/type=winsynth/" -e "s/^device_file=.*/device_file=/" -e "s/^synth_number=.*/synth_number=/" -e "s/^commandline=.*/commandline=tools\\\midiplayer.exe %s/" -e "s/^version=.*/version=1.1.3/" default.config > $(DISTNAME)/default.config
	$(MAKE) -C online-docs win32-dist 

	flip -m -b $(DISTNAME)/README
	mv $(DISTNAME)/README $(DISTNAME)/README.txt
	flip -m -b $(DISTNAME)/INSTALL
	mv $(DISTNAME)/INSTALL $(DISTNAME)/INSTALL.txt
	flip -m -b $(DISTNAME)/INSTALL.win32
	mv $(DISTNAME)/INSTALL.win32 $(DISTNAME)/INSTALL.win32.txt
	flip -m -b $(DISTNAME)/FAQ
	mv $(DISTNAME)/FAQ $(DISTNAME)/FAQ.txt
	flip -m -b $(DISTNAME)/COPYING
	mv $(DISTNAME)/COPYING $(DISTNAME)/COPYING.txt
	flip -m -b $(DISTNAME)/AUTHORS
	mv $(DISTNAME)/AUTHORS $(DISTNAME)/AUTHORS.txt
		
	mv $(DISTNAME) $(PACKAGE)-win32-$(VERSION)
	cp --recursive share $(PACKAGE)-win32-$(VERSION)
	rm -f ../$(PACKAGE)-win32-$(VERSION).zip
	zip -9 -r ../$(PACKAGE)-win32-$(VERSION).zip $(PACKAGE)-win32-$(VERSION)
	rm -rf $(PACKAGE)-win32-$(VERSION)

clean-toplevel:
	rm -f *.pyc *.pyo
	rm -rf share
	# remove those subdirs gimp like to spread around
	find -type d|grep .xvpics|xargs rm -rf

clean: clean-toplevel
	$(LOOP)

distclean: clean-toplevel
	$(LOOP)
	rm -f config.status config.cache config.log
	rm -f install-stamp build-stamp

	rm -f Makefile Makefile.inc config.h debian-solfege.py run-solfege.py Variables.make solfege.1 solfege.manpage.xml installer.nsi debian-solfege.py.in
	rm -f config.cache config.log config.status # configure

count_c_lines:
	wc --lines $(wildcard soundcard/*.c)

count_lines:
	wc --lines $(wildcard mpd/*.py soundcard/*.py src/*.py)
	@echo "Counted python files, but not counting gnomeemu/"

export-to-dos:
	rm -rf ../solfege-export-to-dos
	autoconf
	./configure --without-gnome --without-gtkhtml
	$(MAKE)
	$(MAKE) -C soundcard winmidi.pyd
	cd .. && cp -R solfege-stable solfege-export-to-dos
	cd ../solfege-export-to-dos && make export-to-dos-step-two

export-to-dos-step-two:
	for fn in AUTHORS COPYING FAQ README INSTALL INSTALL.win32 ; do \
	  flip -m -b $$fn; \
	  mv $$fn $$fn.txt; \
	done
	mv gnomeemu/gnome gnome
