#!/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)
        ifeq ($(XVERSION),6.8.99)
            XVERSIONMAX := 7.0.99
        else
		# The driver for 6.8.0 also works with 6.8.[12], and hopefully later versions
	    	XVERSION := 6.8.0
        endif
	endif
endif

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

ifeq (,$(XVERSIONMAX))
    XVERSIONMAX := $(basename $(XVERSION)).99
endif

KSRCDIR  := usr/src/modules/fglrx

# Package names
PKG_driver      := fglrx-driver
PKG_driver_dev  := fglrx-driver-dev
PKG_kernel_src  := fglrx-kernel-src
PKG_control_qt3 := fglrx-control
PKG_sources     := fglrx-sources

build: usr/X11R6
	dh_testdir

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

	# set proper permissions on /etc files
	test -f etc/fglrxrc && chmod 644 etc/fglrxrc
	test -f etc/fglrxprofiles.csv && chmod 644 etc/fglrxprofiles.csv

	if [ -f debian/fglrx.default ]; then \
	  mv -v debian/fglrx.default debian/fglrx; \
	fi
ifeq ($(DEB_BUILD_ARCH),amd64)
	# remove exec bit from 64-bit libs too
	find usr/X11R6/lib64       -type f | xargs chmod -x
endif

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
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Create the directories to install into
	dh_installdirs -p$(PKG_driver) \
		usr/X11R6 \
		usr/X11R6/lib \
		usr/X11R6/lib/modules \
		usr/bin \
		usr/sbin \
		etc/acpi \
		etc/acpi/events \
		etc/default

ifeq ($(DEB_BUILD_ARCH),amd64)
	# the amd64 package includes 32bit compatibility libraries
	dh_installdirs -p$(PKG_driver) \
		emul/ia32-linux/usr/X11R6/lib \
		emul/ia32-linux/usr/X11R6/lib/modules \
		etc/X11/Xsession.d
endif

	dh_installdirs -p$(PKG_driver_dev) \
		usr/X11R6 \
		usr/X11R6/include \
		usr/X11R6/lib \
		usr/include
	dh_installdirs -p$(PKG_kernel_src) \
		$(KSRCDIR) \
		$(KSRCDIR)/debian
	dh_installdirs -A -p$(PKG_control_qt3) \
		usr/X11R6 \
		usr/X11R6/bin \
		usr/share \
		usr/share/applnk \
		usr/share/gnome \
		usr/share/icons \
		usr/share/pixmaps
	dh_installdirs -p$(PKG_sources) \
		usr/src

	dh_install

	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"
	# amd64 needs some library redirection
ifeq ($(DEB_BUILD_ARCH),amd64)
	dh_install -p$(PKG_driver) "usr/X11R6/lib64/*.so*"     "usr/X11R6/lib"
	dh_install -p$(PKG_driver) "usr/X11R6/lib64/modules/*" "usr/X11R6/lib/modules"
	dh_install -p$(PKG_driver) "usr/X11R6/lib/*.so*"     "emul/ia32-linux/usr/X11R6/lib"
	dh_install -p$(PKG_driver) "usr/X11R6/lib/modules/*" "emul/ia32-linux/usr/X11R6/lib/modules"
	dh_install -p$(PKG_driver) "debian/70fglrx" 	     "etc/X11/Xsession.d"
else
	dh_install -p$(PKG_driver) "usr/X11R6/lib/*.so*"     "usr/X11R6/lib"
	dh_install -p$(PKG_driver) "usr/X11R6/lib/modules/*" "usr/X11R6/lib/modules"
endif
	dh_install -p$(PKG_driver) "etc/fglrx*"                "etc"
	dh_install -p$(PKG_driver) "etc/ati"                   "etc"
	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"

	# create symlinks to shared libs; fixes a lintian error
	/sbin/ldconfig -n debian/$(PKG_driver)/usr/X11R6/lib
ifeq ($(DEB_BUILD_ARCH),amd64)
	/sbin/ldconfig -n debian/$(PKG_driver)/emul/ia32-linux/usr/X11R6/lib
endif

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

	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 -c modules | bzip2 > fglrx.tar.bz2 \
	 && rm -rf modules)
	# install panel files
	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) "usr/share/gnome/apps/fireglcontrol.desktop" "usr/share/applications"
	dh_install -A -p$(PKG_control_qt3) "usr/share/applnk/fireglcontrol.kdelnk" "usr/share/applnk"
	dh_desktop    -p$(PKG_control_qt3)

	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 --exclude=emul
ifeq ($(CVERSION),$(PVERSION))
	dh_gencontrol -- -VXVERSION=$(XVERSION) -VXVERSIONMAX=$(XVERSIONMAX) -VXTYPE=$(XTYPE) -VXSERVER=$(XSERVER) -VVERSION=$(PVERSION)
else
	dh_gencontrol -- -VXVERSION=$(XVERSION) -VXVERSIONMAX=$(XVERSIONMAX) -VXTYPE=$(XTYPE) -VXSERVER=$(XSERVER) -VVERSION=$(PVERSION) -v$(PVERSION)+$(DVERSION)
endif
	dh_md5sums
	dh_builddeb

.PHONY: binary build clean distclean usr/X11R6

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