#!/usr/bin/make -f

# determine whether we are cross-compiling
DEB_BUILD_GNU_TYPE:=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE:=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq (${DEB_BUILD_GNU_TYPE},${DEB_HOST_GNU_TYPE})
CROSS:=0
else
CROSS:=1
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  DEBUG =DEBUG=1
else
  DEBUG =
endif

ARCH ?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(ARCH),amd64)
  ARCH =x86_64
else
ifeq ($(ARCH),powerpc)
  ARCH =ppc
else
ifeq ($(ARCH),hppa)
  ARCH =parisc
else
ifeq ($(ARCH),mipsel)
  ARCH =mips
else
ifneq (,$(findstring arm,$(ARCH)))
  ARCH =arm
  DEBUG =DEBUG=1
endif
endif
endif
endif
endif

DYN =0
ifneq ($(ARCH),i386)
  DYN =0
endif

DIR =$(shell pwd)/debian/dietlibc
VERSION =$(shell head -n1 debian/changelog |sed -e 's/.*(\(.*\)).*/\1/')

patch: deb-checkdir patch-stamp
patch-stamp:
	for i in `ls -1 debian/diff/*.diff || :`; do \
	  patch -p1 <$$i || exit 1; \
	done
	touch patch-stamp

build: deb-checkdir build-stamp
build-stamp: patch-stamp
	rm -f parisc/strstr.S
	$(MAKE) prefix=/usr/lib/diet \
	  MYARCH='$(ARCH)' VERSION='$(VERSION)' $(DEBUG)
ifeq (${CROSS},0)
	bin-'$(ARCH)'/diet -v -Os gcc -nostdinc -o ttt test/select.c
	./ttt
	rm -f ttt
	bin-'$(ARCH)'/diet -v -Os gcc -nostdinc -static -o ttt debian/tc523086.c
	./ttt
	rm -f ttt
endif
	test '$(DYN)' -eq 0 || \
	  $(MAKE) dyn prefix=/usr/lib/diet \
	    MYARCH='$(ARCH)' VERSION='$(VERSION)' $(DEBUG)
	touch build-stamp

clean: deb-checkdir deb-checkuid
	$(MAKE) clean MYARCH='$(ARCH)'
	test ! -e patch-stamp || \
	  for i in `ls -1r debian/diff/*.diff || :`; do patch -p1 -R <$$i; done
	rm -f build-stamp install-stamp patch-stamp
	rm -f dynlinker.README libdl.README libpthread.CHANGES \
	  libpthread.README profiling.PORTING profiling.README \
	  sparc64.README ia64.README changelog
	rm -rf '$(DIR)' '$(DIR)'-dev '$(DIR)'-doc
	rm -f debian/files debian/substvars debian/dietlibc-dev.postinst \
	  debian/dietlibc-dev.prerm

install: deb-checkdir deb-checkuid install-stamp
install-stamp: build-stamp
	rm -rf '$(DIR)' '$(DIR)'-dev '$(DIR)'-doc
	install -d -m0755 '$(DIR)'/etc
	$(MAKE) install prefix=/usr/lib/diet DESTDIR='$(DIR)' \
	  MYARCH='$(ARCH)' VERSION='$(VERSION)' $(DEBUG)
	# dietlibc dietlibc-dev dietlibc-doc
	echo '/usr/lib/diet/lib' >'$(DIR)'/etc/diet.ld.conf
	install -d -m0755 '$(DIR)'-dev/usr/include
	mv '$(DIR)'/usr/lib/diet/include '$(DIR)'-dev/usr/include/diet
	install -d -m0755 '$(DIR)'-dev/usr/lib/diet
	ln -s ../../include/diet '$(DIR)'-dev/usr/lib/diet/include
	mv '$(DIR)'/usr/lib/diet/lib-'$(ARCH)' '$(DIR)'/usr/lib/diet/lib
	cp -pr '$(DIR)'/usr/lib/diet/lib '$(DIR)'-dev/usr/lib/diet/
	rm -f '$(DIR)'/usr/lib/diet/lib/*.[ao]
	rm -f '$(DIR)'-dev/usr/lib/diet/lib/*.so
	#ln -s lib '$(DIR)'-dev/usr/lib/diet/lib-'$(ARCH)'
	mv '$(DIR)'/usr/lib/diet/bin '$(DIR)'-dev/usr/lib/diet/
	install -d -m0755 '$(DIR)'-dev/usr/bin
	mv '$(DIR)'-dev/usr/lib/diet/bin/diet* '$(DIR)'-dev/usr/bin/
	ln -s ../../../bin/diet '$(DIR)'-dev/usr/lib/diet/bin/
	if test '$(DYN)' -eq 1; then \
	  ln -s ../../../bin/diet-dyn '$(DIR)'-dev/usr/lib/diet/bin/ && \
	  chmod 0644 '$(DIR)'/usr/lib/diet/lib/*.so && \
	  chmod 0755 '$(DIR)'/usr/lib/diet/lib/libdl.so; \
	fi
	install -d -m0755 $(DIR)-dev/usr/share/man
	mv $(DIR)/usr/lib/diet/man/man? $(DIR)-dev/usr/share/man/
	gzip -9 $(DIR)-dev/usr/share/man/man?/*
	rmdir $(DIR)/usr/lib/diet/man
	install -d -m0755 $(DIR)-doc
	# .in
	sed -e 's/|ARCH|/$(ARCH)/g' <debian/dietlibc-dev.postinst.in \
	  >debian/dietlibc-dev.postinst
	sed -e 's/|ARCH|/$(ARCH)/g' <debian/dietlibc-dev.prerm.in \
	  >debian/dietlibc-dev.prerm
	# lintian overrides
	install -d -m0755 '$(DIR)'-dev/usr/share/lintian/overrides
	install -m0644 debian/dietlibc-dev.lintian \
	  '$(DIR)'-dev/usr/share/lintian/overrides/dietlibc-dev
	# doc
	rm -f dynlinker.README libdl.README libpthread.CHANGES \
	  libpthread.README profiling.PORTING profiling.README \
	  sparc64.README ia64.README changelog
	ln -s dynlinker/README dynlinker.README
	ln -s libdl/README libdl.README
	ln -s libpthread/CHANGES libpthread.CHANGES
	ln -s libpthread/README libpthread.README
	ln -s profiling/PORTING profiling.PORTING
	ln -s profiling/README profiling.README
	ln -s sparc64/README sparc64.README
	ln -s ia64/README ia64.README
	ln -s CHANGES changelog
	touch install-stamp

binary-indep: deb-checkdir deb-checkuid install dietlibc-doc.deb
	dpkg-gencontrol -isp -pdietlibc-doc -P'$(DIR)'-doc
	dpkg -b '$(DIR)'-doc ..

binary-arch: deb-checkdir deb-checkuid install dietlibc-dev.deb dietlibc.deb
	dpkg-gencontrol -isp -pdietlibc-dev -P'$(DIR)'-dev
	dpkg -b '$(DIR)'-dev ..
	if test '$(DYN)' -eq 1; then \
	  dpkg-gencontrol -isp -pdietlibc -P'$(DIR)'; \
	  dpkg -b '$(DIR)' ..; \
	fi

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

include debian/implicit
