include ../config

NAME = xfree86-4.2.0

PWD := $(shell pwd)

LSRCDIR= $(SRCDIR)/$(NAME)
LBUILDDIR = $(BUILDDIR)/$(NAME)
#LTMPDIR = $(BUILDDIR)/$(NAME)

all: install

dirs: stamp-dirs
stamp-dirs: 
	mkdir -p $(LBUILDDIR) $(LSRCDIR)
	touch stamp-dirs

untar: stamp-untar
stamp-untar: stamp-dirs
	cd $(LSRCDIR) && tar xfz $(DEBSRCDIR)/X420src-1.tgz
	touch stamp-untar

upstream-patch: stamp-upstream-patch
stamp-upstream-patch: stamp-untar
	cd $(LSRCDIR)/xc && zcat $(DEBSRCDIR)/xfree86-2002-02-22.diff.gz | patch -p1
	touch stamp-upstream-patch

custom_patches = xf86-param-dirs

custom-patch: stamp-custom-patch
stamp-custom-patch: stamp-upstream-patch
	set -e; \
	for f in $(custom_patches); do \
	  echo "custom patch $$f:"; \
	  patch -d $(LSRCDIR) -p1 <$(PWD)/$$f.patch; \
	done
	touch stamp-custom-patch

fix-paths: stamp-fix-paths
stamp-fix-paths: stamp-custom-patch
	cd $(LSRCDIR)/xc/config/cf/ && mv Agenda.cf Agenda.cf.in
	cd $(LSRCDIR)/xc/config/cf/ && \
	    sed -e 's%@INSTALLDIR@%$(INSTALLDIR)%g' \
	        -e 's%@GCCINCDIR@%$(INSTALLDIR)/lib/gcc-lib/mipsel-linux/2.95.4/include%g' \
	        <Agenda.cf.in >Agenda.cf
	touch stamp-fix-paths

build-libs-upstream: stamp-build-libs-upstream
stamp-build-libs-upstream: stamp-fix-paths
	cd $(LSRCDIR)/xc && PATH=$(INSTALLDIR)/bin:$$PATH \
		/usr/bin/time make World
	touch stamp-build-libs-upstream

libs=libICE libSM libX11 libXext libXpm libXt libXtst

build-libs: stamp-build-libs
stamp-build-libs: stamp-build-libs-upstream
	cp $(LSRCDIR)/xc/exports/lib/*.a $(INSTALLDIR)/mipsel-linux/lib/soft-float/snow-model/
	cp $(LSRCDIR)/xc/exports/lib/*.a $(LBUILDDIR)
	set -e; for lib in $(libs); do \
		cp $(PWD)/$$lib.spec $(INSTALLDIR)/lib/snow/lib-specs/ ; \
	done
	set -e; for lib in $(libs); do \
		cd $(LBUILDDIR); \
		mksnow $$lib; \
		cp $$lib.1.sso $$lib.sa $(INSTALLDIR)/lib/snow/; \
		(cd $(INSTALLDIR)/lib/snow/; \
			rm -f $$lib.a; \
			ln -s $$lib.sa $$lib.a); \
	done
	touch stamp-build-libs

build-progs: stamp-build-progs
stamp-build-progs: stamp-build-libs
	cd $(LSRCDIR)/xc/programs && make clean
	cd $(LSRCDIR)/xc/programs && make -k
	touch stamp-build-progs

# Mostly, programs that could vaguely make sense on a PDA and got linked fully shared.

# twm would like Xmu

keep_progs = Xserver/Xfbdev appres/appres oclock/oclock showfont/showfont \
  twm/twm xauth/xauth xdpyinfo/xdpyinfo xev/xev xeyes/xeyes xhost/xhost \
  xinit/xinit xkill/xkill xlsclients/xlsclients xlsfonts/xlsfonts \
  xmodmap/xmodmap xprop/xprop xrandr/xrandr xrdb/xrdb xrefresh/xrefresh \
  xset/xset xsetroot/xsetroot xwd/xwd xwininfo/xwininfo xwud/xwud

install-progs: stamp-install-progs
stamp-install-progs: stamp-build-progs
	mkdir -p $(LBUILDDIR)/bin
	cd $(LSRCDIR)/xc/programs && cp $(keep_progs) $(LBUILDDIR)/bin/
	mipsel-linux-snow-stripall $(LBUILDDIR)/bin/*
	cp $(LBUILDDIR)/bin/* $(INSTALLDIR)/mipsel-linux/bin/
	touch stamp-install-progs

# Depends on build-progs to make sure the tree is in a stable state.

install-includes: stamp-install-includes
stamp-install-includes: stamp-build-progs
	cd $(LSRCDIR)/xc/exports/include && find X11 -print | cpio -pumdL $(INSTALLDIR)/mipsel-linux/include
	cd $(LSRCDIR)/xc/exports/include && find freetype2 -print | cpio -pumdL $(INSTALLDIR)/mipsel-linux/include
	touch stamp-install-includes

install: stamp-install
stamp-install: stamp-build-libs stamp-install-progs stamp-install-includes
	touch stamp-install
