#!/usr/bin/make -f
# Copyright (C) 2002-2005 Flavio Stanchina
# Copyright (C) 2005-2006 Aric Cyr

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

CFLAGS = -Wall

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(ARCH))
	DEB_BUILD_ARCH := $(ARCH)
else 
ifeq (,$(DEB_BUILD_ARCH))
	DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
endif
endif

# Changelog version (without Debian-specific version)
CVERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2 | cut -d- -f1)
DVERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2)

# Detect X implementation.
# If you want to build for a specific implementation, set XTYPE accordingly.
# Examples: XTYPE=XFree86, XTYPE=X.Org
ifeq (,$(XTYPE))
	XTYPE := $(shell X -version 2>&1 | grep -E "^(XFree86|X Window System) Version " | sed -e "s/^X Window System /X.Org /" | cut -d' ' -f1)
endif
ifeq (,$(XSERVER))
	ifeq ($(XTYPE),XFree86)
		XSERVER := xfree86
	endif
	ifeq ($(XTYPE),X.Org)
		XSERVER := xorg
	endif
endif

# Detect X version.
# If you want to build for a specific version, set XVERSION accordingly.
# Example: XVERSION=4.2.0
ifeq (,$(XVERSION))
	XVERSION := $(shell X -version 2>&1 | grep -E "^(XFree86|X Window System) Version " | sed -e "s/^X Window System /X.Org /" | cut -d' ' -f3 | cut -d. -f1-3)
	ifeq ($(XVERSION),4.2.1)
		# There ain't no driver for 4.2.1, set to 4.2.0
		XVERSION := 4.2.0
	endif
	ifeq ($(basename $(XVERSION)),6.8)
		# The driver for 6.8.0 also works with 6.8.[12], and hopefully later versions
		XVERSION := 6.8.0
	endif
	ifeq ($(basename $(XVERSION)),6.9)
		XVERSION := 6.9.0
	endif
	ifeq ($(basename $(XVERSION)),7.0)
		XVERSION := 7.0.0
	endif
endif

# This is the fglrx version.
# Example: PVERSION=2.9.13
ifeq (,$(PVERSION))
	PVERSION := $(CVERSION)
endif

XMODDIR := usr/X11R6/lib/modules
XMODDIR32 := usr/X11R6/lib32/modules

XVERSIONMAX := $(basename $(XVERSION)).99

KSRCDIR  := usr/src/modules/fglrx

# Package names
PKG_driver      := $(XSERVER)-driver-fglrx
PKG_driver_dev  := $(XSERVER)-driver-fglrx-dev
PKG_kernel_src  := fglrx-kernel-source
PKG_control_qt3 := fglrx-control
PKG_sources     := fglrx-sources

usr/X11R6:
	dh_testdir

	# move licenses away from binary dir
	if [ ! -d usr/share/doc/fglrx ]; then \
		mkdir -p usr/share/doc/fglrx; \
		mv usr/X11R6/bin/LICENSE.* usr/share/doc/fglrx; \
	fi

	# remove exec bit from files that don't deserve it
	find usr/X11R6/include \
		usr/X11R6/lib \
		usr/share usr/src     -type f | xargs chmod -x
	find lib -not -name "*.sh" -type f | xargs chmod -x
	find lib      -name "*.sh" -type f | xargs chmod +x

ifeq ($(DEB_BUILD_ARCH),amd64)
	# remove exec bit from 64-bit libs too
	find usr/X11R6/lib64       -type f | xargs chmod -x
endif

configure:
	echo "Using architecture: $(DEB_BUILD_ARCH)"
	if [ -f $(CURDIR)/debian/control.template ]; then \
		cat $(CURDIR)/debian/control.template > $(CURDIR)/debian/control; \
	fi
	for i in preinst postinst postrm atieventsd.init; do \
	  if [ -f $(CURDIR)/debian/driver.$$i ]; then \
	    sed -e "s/#PKGNAME#/$(PKG_driver)/" \
	        -e "s/#DISTRO#/$(X_NAME)/" $(CURDIR)/debian/driver.$$i > \
	      $(CURDIR)/debian/$(PKG_driver).$$i; \
	  fi; \
	done
	if [ -f $(CURDIR)/debian/10fglrx.template ]; then \
	  sed -e "s|#XMODDIR#|$(XMODDIR)|" -e "s|#XMODDIR32#|$(XMODDIR32)|" \
	    $(CURDIR)/debian/10fglrx.template > $(CURDIR)/debian/10fglrx; \
	fi
	if [ -f $(CURDIR)/debian/fglrx.default ]; then \
	  mv $(CURDIR)/debian/fglrx.default $(CURDIR)/debian/fglrx; \
	fi
	dh_testdir

build: configure usr/X11R6
	dh_testdir

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	-rm -f $(CURDIR)/debian/control
	sed -e 's/#XSERVER#/$(XSERVER)/g' debian/control.template > $(CURDIR)/debian/control

clean:
	dh_testdir
	dh_testroot
	dh_clean

	-rm -fr usr opt lib
	-rm -fr extra_src

# NOTE: rm -i because I don't want to accidentally remove downloaded files
distclean: clean
	-rm -i $(RPMNAME)

binary: build install
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Create the directories to install into
	dh_installdirs -p$(PKG_driver) \
		usr/lib \
		usr/sbin \
		$(XMODDIR) \
		$(XMODDIR)/dri \
		$(XMODDIR)/drivers \
		$(XMODDIR)/linux \
		usr/share/fglrx/diversions \
		etc/acpi \
		etc/acpi/events \
		etc/default

ifeq ($(DEB_BUILD_ARCH),amd64)
	# the amd64 package includes 32bit compatibility libraries
	dh_installdirs -p$(PKG_driver) \
		usr/lib32 \
		$(XMODDIR32) \
		$(XMODDIR32)/dri \
		$(XMODDIR32)/drivers \
		$(XMODDIR32)/linux \
		etc/X11/Xsession.d
endif

	dh_installdirs -A -p$(PKG_driver) \
		usr/bin \
		usr/share \
		usr/share/applnk \
		usr/share/gnome \
		usr/share/gnome/apps \
		usr/share/icons \
		usr/share/pixmaps

	dh_installdirs -p$(PKG_driver_dev) \
		usr/include \
		usr/lib
	dh_installdirs -p$(PKG_kernel_src) \
		$(KSRCDIR) \
		$(KSRCDIR)/debian
	dh_installdirs -p$(PKG_sources) \
		usr/src

	dh_install

	# Driver package
	dh_install -p$(PKG_driver) "usr/X11R6/bin/fgl*"      "usr/bin"
	dh_install -p$(PKG_driver) "usr/X11R6/bin/aticonfig" "usr/bin"
	dh_install -p$(PKG_driver) "usr/sbin/atieventsd"     "usr/sbin"
	dh_installman -p$(PKG_driver) "usr/share/man/man8/atieventsd.8.gz"
ifeq ($(DEB_BUILD_ARCH),amd64)
	# amd64 needs some library redirection
	dh_install -p$(PKG_driver) "usr/X11R6/lib64/*.so*"     "usr/lib"
	dh_install -p$(PKG_driver) "usr/X11R6/lib64/modules/*" "$(XMODDIR)"
	dh_install -p$(PKG_driver) "usr/X11R6/lib/*.so*"       "usr/lib32"
	dh_install -p$(PKG_driver) "usr/X11R6/lib/modules/*"   "$(XMODDIR32)"
ifeq ($(X_NAME),breezy)
	dh_install -p$(PKG_driver) "debian/libdri.a"  "$(XMODDIR)/extensions"
endif
else
	dh_install -p$(PKG_driver) "usr/X11R6/lib/*.so*"     "usr/lib"
	dh_install -p$(PKG_driver) "usr/X11R6/lib/modules/*" "$(XMODDIR)"
endif
	dh_install -p$(PKG_driver) "etc/fglrx*"                "etc"
	dh_install -p$(PKG_driver) "etc/ati"                   "etc"
	dh_install -p$(PKG_driver) "debian/10fglrx"            "etc/X11/Xsession.d"
	dh_install -p$(PKG_driver) "debian/fglrx-powermode.sh" "etc/acpi"
	dh_install -p$(PKG_driver) "debian/fglrx-*-aticonfig"  "etc/acpi/events"
	dh_install -p$(PKG_driver) "debian/fglrx"              "etc/default"
	dh_installinit -p$(PKG_driver) --name="atieventsd"

	# Driver dev package
ifeq ($(DEB_BUILD_ARCH),amd64)
	dh_install -p$(PKG_driver_dev) "usr/X11R6/lib64/*.a" "usr/lib"
else
	dh_install -p$(PKG_driver_dev) "usr/X11R6/lib/*.a"   "usr/lib"
endif
	dh_install -p$(PKG_driver_dev) "usr/X11R6/include/*" "usr/include"
	dh_install -p$(PKG_driver_dev) "usr/include/*"       "usr/include"

	# Kernel source package
	dh_install -p$(PKG_kernel_src) \
		lib/modules/fglrx/build_mod/*.c            \
		lib/modules/fglrx/build_mod/*.h            \
		lib/modules/fglrx/build_mod/*.sh           \
		lib/modules/fglrx/build_mod/lib*           \
		lib/modules/fglrx/build_mod/2.6.x/Makefile \
		$(KSRCDIR)
ifeq ($(CVERSION),$(PVERSION))
	dh_install -p$(PKG_kernel_src) "debian/changelog" "$(KSRCDIR)/debian"
else
	echo -e "fglrx-installer ($(PVERSION)+$(DVERSION)) experimental; urgency=low\n" \
		> debian/$(PKG_kernel_src)/$(KSRCDIR)/debian/changelog
	echo -e "  * Version set by user; real changelog omitted.\n" \
		>> debian/$(PKG_kernel_src)/$(KSRCDIR)/debian/changelog
	echo -e " -- $(USER) <$(EMAIL)>  $(shell date --rfc-822)\n" \
		>> debian/$(PKG_kernel_src)/$(KSRCDIR)/debian/changelog
endif
	dh_install -p$(PKG_kernel_src)  \
		debian/copyright        \
		debian/compat           \
		module/rules            \
		module/control.template \
		module/dirs.template    \
		module/postinst		\
		$(KSRCDIR)/debian
	(cd debian/$(PKG_kernel_src)/usr/src \
	 && chown -R root:src modules \
	 && tar -jcf fglrx.tar.bz2 modules \
	 && rm -rf modules)

	# control panel package
	dh_install -A -p$(PKG_control_qt3) "usr/X11R6/bin/fireglcontrolpanel"  "usr/bin"
	dh_install -A -p$(PKG_control_qt3) "usr/share/icons/ati.xpm"           "usr/share/icons"
	dh_install -A -p$(PKG_control_qt3) "usr/share/icons/ati.xpm"           "usr/share/pixmaps"
	dh_install -A -p$(PKG_control_qt3) "debian/fireglcontrol.desktop"      "usr/share/applications"
	dh_install -A -p$(PKG_control_qt3) "debian/fireglcontrol.kdelnk"       "usr/share/applnk"
	dh_desktop    -p$(PKG_control_qt3)

	# control panel source package
	dh_install -p$(PKG_sources) "usr/src/*" "usr/src"

	dh_installdocs
	dh_installdocs -p$(PKG_driver) usr/share/doc/fglrx/*
	#dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_makeshlibs
	dh_installdeb
	LD_PRELOAD= dh_shlibdeps
ifeq ($(CVERSION),$(PVERSION))
	dh_gencontrol -- -VXVERSION=$(XVERSION) -VXVERSIONMAX=$(XVERSIONMAX) -VXTYPE=$(XTYPE) -VXSERVER=$(XSERVER)
else
	dh_gencontrol -- -VXVERSION=$(XVERSION) -VXVERSIONMAX=$(XVERSIONMAX) -VXTYPE=$(XTYPE) -VXSERVER=$(XSERVER) -v$(PVERSION)+$(DVERSION)
endif
	dh_md5sums
	dh_builddeb

.PHONY: binary configure build install clean distclean

# $Id: rules,v 1.20 2005/06/02 11:09:44 flavio Exp $
