#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif



config.status: config.status-stamp configure
config.status-stamp:
	dh_testdir
	# Make build dir for ngspice
	mkdir -p build/ngspice
	cp -Rl `ls . |grep -v build|grep -v debian` build/ngspice
	cp -f /usr/share/misc/config.sub build/ngspice/config.sub
	cp -f /usr/share/misc/config.guess build/ngspice/config.guess
	cp -f /usr/share/misc/config.sub build/ngspice/doc/config.sub
	cp -f /usr/share/misc/config.guess build/ngspice/doc/config.guess
	# Make build dir for tclspice
	mkdir -p build/tclspice
	cp -Rl `ls . |grep -v build|grep -v debian` build/tclspice
	cp -f /usr/share/misc/config.sub build/tclspice/config.sub
	cp -f /usr/share/misc/config.guess build/tclspice/config.guess
	# Configure ngspice
	(cd build/ngspice;\
	./configure $(CROSS) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--enable-maintainer-mode \
		--enable-capzerobypass \
		--enable-intnoise \
		--enable-xspice \
		--enable-cider \
		--disable-xgraph \
		--enable-debug \
		--enable-numparam=yes \
		--enable-dot-global \
		enable_adms=no \
		CFLAGS="$(CFLAGS)")
	#LDFLAGS="-Wl,-z,defs")
	# Configure tclspice
	(cd build/tclspice;\
	./configure $(CROSS) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--libdir=/usr/lib/tcltk \
		--enable-shared=yes \
		--enable-maintainer-mode \
		--enable-capzerobypass \
		--enable-intnoise \
		--enable-xspice \
		--enable-cider \
		--disable-xgraph \
		--enable-debug \
		--enable-numparam=yes \
		--enable-dot-global \
		--disable-x \
		--with-tcl=/usr/lib/tcl8.4 \
		CFLAGS="$(CFLAGS)")
	touch $@

build: build-stamp

build-stamp:  config.status 
	dh_testdir

	cd build/ngspice && $(MAKE)
	cd build/tclspice && $(MAKE)
	# Build documentation
	cd build/ngspice/doc && lyx --export ps manual.lyx 
	cd build/ngspice/doc && lyx --export html manual.lyx 

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	rm -f config.status-stamp
	rm -rf build
	rm -f src/frontend/parse-bison.c
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Make ngspice and tclspice
	cd build/ngspice && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	cd build/tclspice && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	# The code model files must be stripped
ifneq ("nostrip","$(findstring nostrip,$(DEB_BUILD_OPTIONS))")
	strip $(CURDIR)/debian/tmp/usr/lib/spice/*.cm
	strip $(CURDIR)/debian/tmp/usr/lib/tcltk/spice/*.cm
endif

	# Remove empty dirs:
	find $(CURDIR)/debian/tclspice -type d -empty -delete

	# Override file for ngspice
	##mkdir -p $(CURDIR)/debian/ngspice/usr/share/lintian/overrides/
	##install -p -o root -g root -m 644 $(CURDIR)/debian/ngspice.overrides \
	##	$(CURDIR)/debian/ngspice/usr/share/lintian/overrides/ngspice
	# Override file for tclspice
	##mkdir -p $(CURDIR)/debian/tclspice/usr/share/lintian/overrides/
	##install -p -o root -g root -m 644 $(CURDIR)/debian/tclspice.overrides \
	##	$(CURDIR)/debian/tclspice/usr/share/lintian/overrides/tclspice
	# Documentation for ngspice, the same as for tclspice
	mkdir -p $(CURDIR)/debian/tmp/usr/share/doc/ngspice-doc/html
	cp -a build/ngspice/doc/manual.html.LyXconv/* \
		$(CURDIR)/debian/tmp/usr/share/doc/ngspice-doc/html
	install -o root -g root -m 644 build/ngspice/doc/manual.ps \
		$(CURDIR)/debian/tmp/usr/share/doc/ngspice-doc/ngspice.ps
	# Removing /usr/share/info/dir file
	rm -f $(CURDIR)/debian/tmp/usr/share/info/dir

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_install -i --sourcedir=debian/tmp
	dh_installinfo -i
	dh_installman -i
	dh_installexamples -i -pngspice-doc examples/* -Xtclspice
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs -pngspice contrib -XCOPYING
	dh_installdocs
	dh_installexamples -ptclspice examples/tclspice/*
	dh_install --sourcedir=debian/tmp
	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

#DEB_UPSTREAM_VERSION := $(shell dpkg-parsechangelog | grep '^Version:' | sed -e 's/Version: \(.*\)-\(.*\)\.*/\1/')
get-orig-source:
	VER=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-+~]+).*,\1,p'); \
	BASENAME=ng-spice-rework; \
	set -e;\
	wget -q http://sourceforge.net/projects/ngspice/files/$$BASENAME/$$VER/$$BASENAME-$$VER.tar.gz/download; \
	tar xz --exclude=CVS --exclude=xgraph -f $$BASENAME-$$VER.tar.gz; \
	mv -f ngspice-$$VER $$BASENAME-$$VER; \
	rm -f $$BASENAME-$$VER/config.sub ;\
 	rm -f $$BASENAME-$$VER/config.guess ;\
	tar czf ng-spice-rework_$$VER.orig.tar.gz $$BASENAME-$$VER; \
	rm -f $$BASENAME-$$VER.tar.gz; \
	rm -rf $$BASENAME-$$VER; \
	wget -q http://sourceforge.net/projects/ngspice/files/$$BASENAME/$$VER/ngspice-doc-$$VER.tar.gz/download; \
	tar xz --exclude=pdf -f ngspice-doc-$$VER.tar.gz; \
	tar czf ng-spice-rework_$$VER.orig-doc.tar.gz ngspice-doc-$$VER; \
	rm -f ngspice-doc-$$VER.tar.gz; \
	rm -rf ngspice-doc-$$VER
# 	find ${DIRNAME} -name .cvsignore -exec rm -f {} \; ;\

.PHONY: build clean binary-indep binary-arch binary install 
