######################################################################
#    Copyright (C) 2005 Kurt Schwehr
#
#    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, 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.
######################################################################

default:
	@echo
	@echo "Welcome to the segy-py (The python SEGY seismic data reader)"
	@echo
	@echo "  docs          - use pydoc to make html pages"
	@echo "  docs-install  - put html on the web"
	@echo
	@echo "  clean         - Remove temp files"
	@echo "  dist-clean    - Get rid of the setup.py build stuff"
	@echo
	@echo "  test          - Run the unittests in the python files"
	@echo "  check         - Run everything through the ringer (includes test)"
	@echo
	@echo "  sdist         - Build a tar.bz2 image of the package"
	@echo "  sdist-install - put the tar.bz2 image on the web server"
	@echo
	@echo "It is up to you to install the package with ./setup.py"

BINS := makepltsegy.py
BINS += segydump.py
BINS += segysql.py
BINS += segysqldist.py
BINS += segysqldump.py
BINS += segysqlgmt.py
BINS += segysqlshiptrack.py
BINS += segyslice.py
BINS += segysql-sioseis-plt.py
BINS += segywaterbottom.py
BINS += gnis.py
BINS += segystats.py
BINS += offset.py
BINS += decimate.py

MAN  := ${BINS:.py=.py.1}
HTML := ${BINS:.py=.py.1.html}

PYTHON_FILES:=${wildcard *.py}
PYTHON_MODS:=${PYTHON_FILES:.py=}
PYTHON_MOD_DOCS:=${PYTHON_FILES:.py=.html}
# FIX: how do I remove setup from the above lists?

docs: 
	@echo "${PYTHON_MODS}"
	pydoc -w ${PYTHON_MODS}
	code2html Makefile > Makefile-segy-py.html

BASE_URL:=http://schwehr.org/software/segy-py
BASE_SCP:=schwehr.org:www/software/segy-py
docs-install: docs html
	scp HEADER.html ${BASE_SCP}/
	scp ${PYTHON_MOD_DOCS}  Makefile-segy-py.html ${BASE_SCP}/docs
	scp ${HTML} ChangeLog ${BASE_SCP}/docs
	open ${BASE_URL}/docs

help:
	open ${BASE_URL}/docs

# Find all the known rough edges and pot holes
check: test
	pychecker *.py
	grep -n FIX *.py

# Call the unittest methods for those with tests written
test:
	./segy.py -v
	./segy_drv_xstar.py -v

# For html, skip removing HEADER.html
clean:
	rm -f *.pyc [^H]*.html *~ [0-9].dat [0-9][0-9][0-9].dat
	rm -f *.py.1 

# a dist is built by "python setup.py sdist"
# and MANIFEST gets built in the process based on MANIFEST.in
dist-clean: clean
	rm -rf dist *.db
	rm -rf MANIFEST

# python packaging - I like bz2 over gz for software tars
sdist: dist-clean
	./setup.py sdist --formats=bztar

sdist-install: sdist segy-py.info
	scp dist/* ${BASE_SCP}/
	scp segy-py.info ${BASE_SCP}/

##############################
# Build web man pages
HELP2MAN_OPTS:=-SSchwehr -m "Python Commands" --no-info  

%.py.1: %.py
	export PATH=.:$$PATH && help2man ${HELP2MAN_OPTS} -I $<.help2man $< > $@
%.py.1.html: %.py.1
	groff -Tascii -man $< | man2html > $@

html: ${HTML}
man: ${MAN}

#	-mkdir -p doc/man/man1

##############################
# Fink package file
VERSION:=${shell python -c "import segy_version;print segy_version.VERSION"}
segy-py.info: segy-py.info.in Makefile
	perl -p -e "s/\@VERSION\@/${VERSION}/g" $< > fink.tmp
	MD5=`md5 dist/segy-py-${VERSION}.tar.bz2| awk '{print $$4}'` \
	      && perl -p -e "s/\@MD5\@/$$MD5/g" fink.tmp > $@
	rm -f fink.tmp


syntax highlighted by Code2HTML, v. 0.9.1