###########################################################################
#	
#	MAKEFILE Distributing the C++ port of NEC
#	
#	Author: Tim Molteno.
#
#
#
#	Copyright (C) 2004-2008  Timothy C.A. Molteno
#	
#	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
#
#

all: dist

###########################################################################################################
#
#	This builds a source code distribution (and a Windows executable) and packages it into
#	an archive. Also uploads it to the server
#

BUILD_DATE=$(shell date +%Y-%m-%d)
BUILDDIR=necpp_$(shell date +%Y%m%d)
DEST=elec@www.physics.otago.ac.nz:electronics/nec
ARCHIVE=${BUILDDIR}
VERSION=$(shell fgrep AM_INIT_AUTOMAKE configure.in | sed -r -e "s/([^0-9]+)([0-9\.]+).+/\2/g")
NEC_VERSION=${VERSION} "[${BUILD_DATE}]"

test:
	echo ${NEC_VERSION}
	
.PHONY : win32
win32:
#
#	Make a zipped source distribution for Windows
	rm -rf ${BUILDDIR}
	mkdir -p ${BUILDDIR}/src
	cp src/*.cpp ${BUILDDIR}/src
	cp src/*.h ${BUILDDIR}/src
	cp README ${BUILDDIR}
	mkdir -p ${BUILDDIR}/test_data
	cp test_data/*.nec ${BUILDDIR}/test_data
	cp -a win32/ ${BUILDDIR}
	cd ${BUILDDIR}; rm -rf `find . -name CVS`
	rm -f ${ARCHIVE}.zip
	zip -vr ${ARCHIVE}.zip ${BUILDDIR}


#
#	Make the Windows executable (nec2++.exe) with MinGW
#
#	To install mingw on debian simply issue the command:
#
#	aptitude install mingw32
#nec_version
win32_exe:
#	make distclean
	./configure --without-lapack --host=i586-mingw32msvc --build=i386-linux CXXFLAGS="-O3 -mtune=athlon-xp -march=pentium -Dnec_build_date='\"${BUILD_DATE}\"' -Dbuild_version='\"${VERSION}\"' " --prefix=/tmp/necpp_w32 
	make
	make install-strip
	mv /tmp/necpp_w32/bin/nec2++.exe .
	zip nec2++.zip nec2++.exe
#
#	If we're using Visual Studio we just copy the executable over
#	scp electron:nec2++.exe .


dist:
	rm -rf ${BUILDDIR}
	mkdir -p ${BUILDDIR}/src
	cp src/*.cpp ${BUILDDIR}/src
	cp src/*.h ${BUILDDIR}/src
	cp makefile.dist Makefile.test ${BUILDDIR}
	cp AUTHORS README INSTALL COPYING ChangeLog TODO ${BUILDDIR}
	cp install-sh ${BUILDDIR}
	cp configure ${BUILDDIR}
	mkdir -p ${BUILDDIR}/test_data
	cp test_data/*.nec ${BUILDDIR}/test_data
	tar -cf ${ARCHIVE}.tar ${BUILDDIR}
	rm -f ${ARCHIVE}.tar.bz2
	bzip2 -9 ${ARCHIVE}.tar
#
#	Make a .tar.bz2 source distribution for Linux
	rm -rf www
	mkdir -p www
	cp ${ARCHIVE}.tar.bz2 www/necpp_latest.tar.bz2
	mv ${ARCHIVE}.tar.bz2 www
#
#	Make a zipped source distribution for Windows
#
	cp -a win32/ ${BUILDDIR}
	cd ${BUILDDIR}; rm -rf `find . -name CVS`
	rm -f ${ARCHIVE}.zip
	zip -vr ${ARCHIVE}.zip ${BUILDDIR}
	mv ${ARCHIVE}.zip www

build_dist:
#		
	- make distclean
	make -f Makefile.cvs
	./configure CXXFLAGS="-O3 -mtune=athlon-xp -march=pentium -Dnec_build_date='\"${BUILD_DATE}\"'  -Dbuild_version='\"${VERSION}\"'"
	make -f Makefile dist
	
	mv necpp-${VERSION}.tar.gz www
#
#	Make a linux executable
#
	make -f Makefile
	mv src/nec2++ www
	strip --strip-all www/nec2++
	cd www; tar -cf nec2++.tar nec2++; gzip -9 nec2++.tar
#
#	The following used to make the Windows executable
#
	make -f makefile.dist win32_exe
	mv nec2++.zip www
	cp docs/*.html www
#	cp docs/users_guide.pdf www
	rsync --verbose --rsh=ssh  --cvs-exclude --recursive www/* ${DEST}

nec2c_src:
	rm -rf nec2c_src
	mkdir -p nec2c_src
	cp  c_src/*.* nec2c_src
	cp  c_src/Makefile nec2c_src
	cp  c_src/README nec2c_src
	cd nec2c_src; rm -rf `find . -name CVS`
	rm -f nec2c_src.tar.gz
	tar -cf nec2c_src.tar nec2c_src
	gzip -9 nec2c_src.tar
	scp nec2c_src.tar.gz ${DEST}
	

