######################################################################
#    Copyright (C) 2004,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.
######################################################################

NAME := pmag-kds-py
# VERSION_1 is the previous version
VERSION_1:=${shell python -c "from pmag_kds_version import VERSION;print VERSION.split('.')[0]+'.'+str(int(VERSION.split('.')[1])- 1)"}

VERSION:=${shell python -c "import pmag_kds_version;print pmag_kds_version.VERSION"}
DATE:=${shell python -c "import pmag_kds_version;print pmag_kds_version.__date__"}


default:
	@echo
	@echo "Welcome to the pmag-kds-py, Kurt's python paleomagnetic software package"
	@echo
	@echo "   Version ${VERSION}  (${DATE})"
	@echo
	@echo "docs          - Use pydoc to make html pages"
	@echo "docs-install  - Put html on the web"
	@echo "check         - Search for marked trouble spots"
	@echo "clean         - Remove temp files"
	@echo "dist-clean    - Get rid of the setup.py build stuff"
	@echo
	@echo "help          - Send you to a helpful web page"
	@echo
	@echo "sdist         - Build a tar.bz2 image of the package"
	@echo "sdist-install - put the tar.bz2 image on the web server"

# Programs with optparse interfaces
BINS := agfm.py
BINS += pdfposter.py
BINS += units.py
BINS += xrd.py
BINS += fabricsim.py
#BINS += 

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?... or how to keep it in?

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

HEADER.html: HEADER.html.in Makefile
	perl -p -e "s/\@VERSION\@/${VERSION}/g" $< > header.tmp
	perl -p -e "s/\@DATE\@/${DATE}/g" header.tmp > $@
	rm -f header.tmp

BASE_URL := http://schwehr.org/software/pmag-kds-py
BASE_SCP := kds.ucsd.edu:www/software/pmag-kds-py
docs-install: docs html HEADER.html
	scp HEADER.html ${BASE_SCP}/
	scp ${PYTHON_MOD_DOCS} Makefile-pmag-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:
	pychecker *.py
	grep -n FIX *.py

clean:
	rm -f *.pyc *.html mypost* *~ *.1

# dist is build 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 *.tar.{gz,bz2} foo*
	rm -rf *.{svg,gif,gp,pdf,png,txt}

##############################
# Fink package file

pmag-kds-py.info: pmag-kds-py.info.in Makefile
	perl -p -e "s/\@VERSION\@/${VERSION}/g" $< > fink.tmp
	MD5=`md5 dist/pmag-py-${VERSION}.tar.bz2| awk '{print $$4}'` \
	      && perl -p -e "s/\@MD5\@/$$MD5/g" fink.tmp > $@
	rm -f fink.tmp

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

sdist-install: sdist pmag-kds-py.info
	scp dist/* ${BASE_SCP}/downloads/
	scp pmag-kds-py.info ${BASE_SCP}/downloads/pmag-kds-py-${VERSION}.info

${NAME}-${VERSION_1}.tar.bz2: 
	@echo "Current  version: ${VERSION}"
	@echo "Fetching version: ${VERSION_1}"
	@echo
	scp ${BASE_SCP}/downloads/${NAME}-${VERSION_1}.tar.bz2 .

diff-setup:  sdist ${NAME}-${VERSION_1}.tar.bz2
	cd dist && tar xf ../${NAME}-${VERSION_1}.tar.bz2
	cd dist && tar xf ${NAME}-${VERSION}.tar.bz2

diff:
	cd dist && diff -c -r ${NAME}-${VERSION_1} ${NAME}-${VERSION}

##############################
# 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}


syntax highlighted by Code2HTML, v. 0.9.1