#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

package=mysql
CHARSET=ujis
SYSNAME=

TEMPINST=build

# CFLAGS="-O6 -mpentium -mstack-align-double -fomit-frame-pointer" CXX=gcc CXXFLAGS="-O6 -mpentium -mstack-align-double -fomit-frame-pointer -felide-constructors -fno-exceptions -fno-rtti" ./configure --prefix=/usr/local/mysql --enable-assembler --with-mysqld-ldflags=-all-static

CC=gcc
CFLAGS=-O6 -fomit-frame-pointer
CXX=gcc
CXXFLAGS=-O6 -fomit-frame-pointer -felide-constructors  -fno-exceptions -fno-rtti
# CXXFLAGS=-O6 -fomit-frame-pointer -felide-constructors -fno-rtti

COMMONCONF= --prefix=/usr --libexecdir=/usr/sbin \
		--localstatedir=/var/mysql/data \
		--enable-shared \
		--without-perl --without-readline \
		--without-docs --without-bench \
		--with-mysqld-user=mysql \
		--with-extra-charsets=all

SERVERCONF=$(COMMONCONF) --enable-assembler \
		--with-raid

MYSQLMAXCONF=	--with-server-suffix=-Max \
		--with-innodb \
		--with-berkeley-db

#		--with-gemini \

#		--with-berkeley-db-includes=/usr/include/db3 \
#		--with-berkeley-db-libs=/usr/lib

STATICCONF=--with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static

CLIENTCONF=$(COMMONCONF) --without-server

##################################################
patches debian/stamp-patches:
	-test -e debian/stamp-patches || \
	for i in `find debian/patches -type f -print` ;	do \
	  echo "==== $$i ====" ; \
	  patch -p1 < $$i ; \
	done
	touch debian/stamp-patches

##################################################
premkdir debian/stamp-premkdir:
	$(checkdir)
	-rm -rf debian/tmp debian/$(TEMPINST)*
	dh_installdirs

	for i in '' -shared -debug -max -max-shared -max-debug ; do \
	 install -d debian/$(TEMPINST)$${i}/usr/{bin,sbin,share,man,include,info} ; \
	done

#	-install -d debian/$(TEMPINST)$${i}/usr/{bin,sbin,share,man,include,info}
#	-install -d debian/$(TEMPINST)-shared/usr/{bin,sbin,share,man,include,info}
#	-install -d debian/$(TEMPINST)-debug/usr/{bin,sbin,share,man,include,info}

	touch debian/stamp-premkdir

##################################################
config debian/stamp-config: debian/stamp-premkdir debian/stamp-patches
	CC=$(CC) CFLAGS="$(CFLAGS)" CXX=$(CXX) CXXFLAGS="$(CXXFLAGS)" \
	LDFLAGS="-static" \
	./configure $(SERVERCONF) $(STATICCONF) \
		--with-charset=$(CHARSET) \
		--with-bench \
		$(SYSNAME)

#	sed 's/-fno-implicit-templates//g' sql/Makefile > .m
#	mv .m sql/Makefile
	touch debian/stamp-config

##################################################
build: debian/stamp-config
	make LDFLAGS="-static"
	make install DESTDIR=`pwd`/debian/$(TEMPINST)
	cp include/m_ctype.h `pwd`/debian/$(TEMPINST)/usr/include/mysql/
	touch build

##################################################
build-shared debian/stamp-build-shared: debian/stamp-patches
	-make distclean
	CC=$(CC) CFLAGS="$(CFLAGS)" CXX=$(CXX) CXXFLAGS="$(CXXFLAGS)" \
	./configure $(SERVERCONF) \
		--with-charset=$(CHARSET) \
		$(SYSNAME)

#	./configure $(CLIENTCONF)

	make
	make install DESTDIR=`pwd`/debian/$(TEMPINST)-shared

	touch debian/stamp-build-shared

##################################################
build-debug debian/stamp-build-debug: debian/stamp-patches
	-make distclean
	CC=$(CC) CFLAGS="$(CFLAGS)" CXX=$(CXX) CXXFLAGS="$(CXXFLAGS)" \
	./configure $(SERVERCONF) \
		--with-charset=$(CHARSET) \
		--with-debug \
		$(SYSNAME)

#	./configure $(CLIENTCONF)

	make
	make install DESTDIR=`pwd`/debian/$(TEMPINST)-debug

	touch debian/stamp-build-debug
	
##################################################
debian/stamp-mysql-max: debian/stamp-mysql-max-static debian/stamp-mysql-max-shared debian/stamp-mysql-max-debug

debian/stamp-mysql-max-static: debian/stamp-premkdir debian/stamp-patches
	-make distclean
	CC=$(CC) CFLAGS="$(CFLAGS)" CXX=$(CXX) CXXFLAGS="$(CXXFLAGS)" \
	./configure $(SERVERCONF) $(STATICCONF) \
		$(MYSQLMAXCONF) \
		--with-charset=$(CHARSET) \
		$(SYSNAME)

	make LDFLAGS="-static"
	make install DESTDIR=`pwd`/debian/$(TEMPINST)-max

	touch debian/stamp-mysql-max-static

debian/stamp-mysql-max-shared: debian/stamp-premkdir debian/stamp-patches
	-make distclean
	CC=$(CC) CFLAGS="$(CFLAGS)" CXX=$(CXX) CXXFLAGS="$(CXXFLAGS)" \
	./configure $(SERVERCONF) \
		$(MYSQLMAXCONF) \
		--with-charset=$(CHARSET) \
		$(SYSNAME)

	make
	make install DESTDIR=`pwd`/debian/$(TEMPINST)-max-shared

	touch debian/stamp-mysql-max-shared


debian/stamp-mysql-max-debug: debian/stamp-premkdir debian/stamp-patches
	-make distclean
	CC=$(CC) CFLAGS="$(CFLAGS)" CXX=$(CXX) CXXFLAGS="$(CXXFLAGS)" \
	./configure $(SERVERCONF) \
		$(MYSQLMAXCONF) \
		--with-charset=$(CHARSET) \
		--with-debug \
		$(SYSNAME)

	make
	make install DESTDIR=`pwd`/debian/$(TEMPINST)-max-debug

	touch debian/stamp-mysql-max-debug


##################################################
clean:
	$(checkdir)
	-make distclean
	-test -e debian/stamp-patches && \
	for i in `find debian/patches -type f -print` ;	do \
	  patch -R -p1 < $$i ; \
	done
	-rm -rf build debian/stamp-* debian/$(TEMPINST)*
	-dh_clean
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/files* core
	-rm -f debian/*substvars
	-rm -f `cat debian/gomi`

##################################################
binary-indep:	checkroot build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch:	checkroot build debian/stamp-build-shared debian/stamp-build-debug debian/stamp-mysql-max
	sh debian/move

### init, post*
	dh_installdeb
	cp debian/shlibs debian/libmysqlclient10/DEBIAN/

	cp debian/my.cnf debian/mysql-server/etc/
	cp support-files/mysql.server debian/mysql-server/etc/init.d/mysql ; chmod +x debian/mysql-server/etc/init.d/mysql

### dpkg-xxx
	dh_compress

	dh_fixperms

	dh_strip

	dh_shlibdeps
	dh_gencontrol

	dpkg --build debian/libmysqlclient10 ..
	dpkg --build debian/mysql-client ..
	dpkg --build debian/mysql-server ..
	dpkg --build debian/mysql-server-shared ..
	dpkg --build debian/mysql-server-debug ..
	dpkg --build debian/mysql-dev ..
	dpkg --build debian/mysql-bench ..
	dpkg --build debian/mysql-doc ..
	dpkg --build debian/mysql-max ..
	dpkg --build debian/mysql-max-shared ..
	dpkg --build debian/mysql-max-debug ..

define checkdir
	test -f debian/rules
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

##################################################
checkroot:
	$(checkdir)
	test root = "`whoami`"

##################################################
.PHONY: binary binary-arch binary-indep clean checkroot
