#!/usr/bin/make -f
# Copyright (C) 2002-2005 Flavio Stanchina
# Copyright (C) 2005-2006 Aric Cyr
# Copyright (C) 2007-2010 Mario Limonciello
# Copyright (C) 2009-2011 Canonical Ltd.

# Figure out some magic versioning
CVERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2 | cut -d- -f1 | cut -d\: -f2)
DVERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2 | cut -d\: -f2)
DISTRO   := $(shell dpkg-parsechangelog | grep '^Distribution:' | cut -d' ' -f2 | cut -d\: -f2)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
# If the host is i386 set OTHER_ARCH to x86_64 and vice versa
OTHER_ARCH := $(shell (echo "$(DEB_HOST_MULTIARCH)" | grep i386 >/dev/null) && \
		echo "$(DEB_HOST_MULTIARCH)" | sed s/i386/x86_64/ || \
		echo "$(DEB_HOST_MULTIARCH)" | sed s/x86_64/i386/)

# Fallback for local debuild
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Hack for pbuild which would otherwise fail to call execstack
PATH := $(PATH):/usr/sbin

# Package names
PKG_flavour     := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p' | sed 's/fglrx\-installer\-//g')
# If it's the standard flavour then we set the flavour to ""
ifeq ("$(PKG_flavour)", "fglrx-installer")
	PKG_flavour := ""
else
	PKG_flavour := "-$(PKG_flavour)"
endif
PKG_driver      := fglrx$(PKG_flavour)
PKG_driver_dev  := $(PKG_driver)-dev
PKG_driver_core := $(PKG_driver)-core
PKG_control     := fglrx-amdcccle$(PKG_flavour)
PKG_source      := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
PKG_module      := $(shell echo "$(PKG_driver)" | sed s/\-/_/g)

# Priority of the alternatives
alt_priority         := 1000
PX_alt_priority      := 900
CORE_priority        := $(alt_priority)
UNBLKLST_CORE_priority  := $(PX_alt_priority)

#Directory naming schemes
PKG_dirname          := fglrx
bindir               := usr/bin
sbindir              := usr/sbin
datadir              := usr/share
PKG_datadir          := $(datadir)/$(PKG_dirname)
libdir               := usr/lib
libdir32             := usr/lib32
includedir           := usr/include
sysconfdir           := etc
PKG_libdir           := $(libdir)/$(PKG_dirname)
PKG_libdir32         := $(libdir32)/$(PKG_dirname)
xorg_libdir          := $(libdir)/xorg
xorg_extra_modules   := $(libdir)/$(DEB_HOST_MULTIARCH)/xorg/extra-modules
xorg_extensionsdir   := $(xorg_libdir)/modules/extensions
PKG_driversdir       := $(PKG_libdir)/xorg
PKG_extensionsdir    := $(PKG_driversdir)
PKG_bindir           := $(PKG_libdir)/bin
PKG_deskdir          := $(PKG_datadir)
PKG_configdir        := $(PKG_libdir)
ld_so_conf_dir       := $(PKG_configdir)
ld_so_conf_file      := ld.so.conf
alt_ld_so_conf_file  := alt_ld.so.conf
ld_so_conf_path      := $(ld_so_conf_dir)/$(ld_so_conf_file)
alt_ld_so_conf_path  := $(ld_so_conf_dir)/$(alt_ld_so_conf_file)
mandir               := usr/share/man/man1
PKG_bugdir           := usr/share/bug/$(PKG_dirname)
autostartdir         := etc/xdg/autostart
PKG_autostartdir     := $(PKG_configdir)

PKG_xmoddir  := $(PKG_libdir)/xorg/modules

PX_dirname := pxpress
PX_dir := $(libdir)/$(PX_dirname)
PX_dir32 := $(libdir32)/$(PX_dirname)
PX_xmoddir := $(PX_dir)/xorg/modules
PX_libdir := $(PX_dir)/lib
PX_libdir32 := $(PX_dir32)/lib
PX_ld_so_conf := $(PX_dir)/$(ld_so_conf_file)
PX_ld_so_conf_alt := $(PX_dir)/$(alt_ld_so_conf_file)

CORE_dirname := fglrx-core
CORE_dir     := $(libdir)/$(CORE_dirname)
CORE_dir32     := $(libdir32)/$(CORE_dirname)
CORE_ld_so_conf := $(CORE_dir)/$(ld_so_conf_file)
CORE_libdir := $(CORE_dir)/lib
CORE_libdir32 := $(CORE_dir32)/lib

PKG_driver_core_bck_dir := usr/share/fglrx-core-backup
PKG_driver_bck_dir := usr/share/fglrx-backup
PKG_control_bck_dir := usr/share/fglrx-amdcccle-backup


# This is remove fglrx from the blacklist
UNBLKLST_CORE_ld_so_conf_file  := unblacklist_ld.so.conf
UNBLKLST_CORE_ld_so_conf := $(CORE_dir)/$(UNBLKLST_CORE_ld_so_conf_file)

# This is a hack to make sure that
# the blacklist file is available
# early in the boot process when
# /usr is on a separate partition.
# See LP: #538071.
PKG_libconfdir := lib/$(PKG_dirname)

# Paths to the dirs from the installer
ifeq ($(DEB_HOST_ARCH),amd64)
	SRC_libdir  := lib64
	SRC_arch    := x86_64
	SRC_other_arch := x86
	SRC_xarch   := xpic_64a
else
	SRC_libdir   := lib
	SRC_arch     := x86
	SRC_other_arch := x86_64
	SRC_xarch    := xpic
endif

#These "define" bits are needed for the blacklist file
define newline


endef

define blacklist_file
# This file was installed by $(PKG_driver)
# Do not edit this file manually

blacklist radeon
alias fglrx $(PKG_module)
alias radeon off
alias lbm-radeon off

blacklist amdgpu
alias amdgpu off
alias lbm-amdgpu off
endef

# DEB_DH_SHLIBDEPS_ARGS_ALL="-Xlib32"

refresh_copyright:
	# refresh copyright file
	cat debian/copyright_stub_0 > debian/copyright
	cat usr/share/doc/fglrx/LICENSE.TXT >> debian/copyright
	cat debian/copyright_stub_1 >> debian/copyright

clean: refresh_copyright regen-from-templates
	# remove any file generated from a template
	for file in *.in; do \
		# strip only the last .in from the name \
		filename=`echo $$file | sed -e "s|.in|$1|"` \
		rm -f $(filename); \
	done
	rm -f debian/$(PKG_driver).substvars

	dh clean

.PHONY: regen-from-templates
regen-from-templates:
	#Create important strings
	for i in 10fglrx \
			 control \
			 dkms.conf \
			 $(PKG_driver).install \
			 $(PKG_driver_dev).install \
			 $(PKG_driver_dev).links \
			 $(PKG_driver).dirs \
			 $(PKG_driver).links \
			 $(PKG_driver).postinst \
			 $(PKG_driver).postrm \
			 $(PKG_driver).preinst \
			 $(PKG_driver).prerm \
			 $(PKG_control).install \
			 $(PKG_control).dirs \
			 $(PKG_driver_core).install \
			 $(PKG_driver_core).links \
			 $(PKG_driver_core).dirs \
			 $(PKG_driver_core).postinst \
			 $(PKG_driver_core).postrm \
			 $(PKG_driver_core).preinst \
			 $(PKG_driver_core).prerm \
			 overrides/$(PKG_driver) \
			 overrides/$(PKG_driver_dev) \
			 overrides/$(PKG_driver_core) \
			 overrides/$(PKG_control); do \
		sed -e "s|#PKGXMODDIR#|$(PKG_xmoddir)|g" \
			-e "s|#LIBDIR#|$(libdir)|g" \
			-e "s|#LIBDIR32#|$(libdir32)|g" \
			-e "s|#BINDIR#|$(bindir)|g" \
			-e "s|#SBINDIR#|$(sbindir)|g" \
			-e "s|#SYSCONFDIR#|$(sysconfdir)|g" \
			-e "s|#MANDIR#|$(mandir)|g" \
			-e "s|#LDSOCONF#|$(ld_so_conf_path)|g" \
			-e "s|#ALTLDSOCONF#|$(alt_ld_so_conf_path)|g" \
			-e "s|#ALTPRIORITY#|$(alt_priority)|g" \
			-e "s|#PXALTPRIORITY#|$(PX_alt_priority)|g" \
			-e "s|#AUTOSTARTDIR#|$(autostartdir)|g" \
			-e "s|#DATADIR#|$(datadir)|g" \
			-e "s|#PKGDESKDIR#|$(PKG_deskdir)|g" \
			-e "s|#PKGDATADIR#|$(PKG_datadir)|g" \
			-e "s|#PKGCONFIGDIR#|$(PKG_configdir)|g" \
			-e "s|#PKGBINDIR#|$(PKG_bindir)|g" \
			-e "s|#PKGLIBDIR#|$(PKG_libdir)|g" \
			-e "s|#PKGLIBDIR32#|$(PKG_libdir32)|g" \
			-e "s|#PKGDRIVERSDIR#|$(PKG_driversdir)|g" \
			-e "s|#XORGEXTRA#|$(xorg_extra_modules)|g" \
			-e "s|#PKGEXTENSIONDIR#|$(PKG_extensionsdir)|g" \
			-e "s|#XORGEXTENSIONSDIR#|$(xorg_extensionsdir)|g" \
			-e "s|#DRIVERNAME#|$(PKG_driver)|g" \
			-e "s|#MODULENAME#|$(PKG_module)|g" \
			-e "s|#DRIVERDIRNAME#|$(PKG_dirname)|g" \
			-e "s|#DRIVERDEVNAME#|$(PKG_driver_dev)|g" \
			-e "s|#DRIVERSRCNAME#|$(PKG_source)|g" \
			-e "s|#DRIVERCORENAME#|$(PKG_driver_core)|g" \
			-e "s|#FLAVOUR#|$(PKG_flavour)|g" \
			-e "s|#DRIVERCTRLNAME#|$(PKG_control)|g" \
			-e "s|#INCLUDEDIR#|$(includedir)|g" \
			-e "s|#PKGLIBCONFDIR#|$(PKG_libconfdir)|g" \
			-e "s|#PKGXMODDIR#|$(PKG_xmoddir)|g" \
			-e "s|#PXDIR#|$(PX_dir)|g" \
			-e "s|#PXDIR32#|$(PX_dir32)|g" \
			-e "s|#PXXMODDIR#|$(PX_xmoddir)|g" \
			-e "s|#PXDIRNAME#|$(PX_dirname)|g" \
			-e "s|#PXLIBDIR#|$(PX_libdir)|g" \
			-e "s|#PXLIBDIR32#|$(PX_libdir32)|g" \
			-e "s|#PXLDSOCONF#|$(PX_ld_so_conf)|g" \
			-e "s|#ALTPXLDSOCONF#|$(PX_ld_so_conf_alt)|g" \
			-e "s|#COREDIRNAME#|$(CORE_dirname)|g" \
			-e "s|#COREDIR#|$(CORE_dir)|g" \
			-e "s|#COREDIR32#|$(CORE_dir32)|g" \
			-e "s|#CORELDSOCONF#|$(CORE_ld_so_conf)|g" \
			-e "s|#COREPRIORITY#|$(CORE_priority)|g" \
			-e "s|#UNBLKCOREPRIORITY#|$(UNBLKLST_CORE_priority)|g" \
			-e "s|#UNBLKCORELDSOCONF#|$(UNBLKLST_CORE_ld_so_conf)|g" \
			-e "s|#CVERSION#|$(CVERSION)|g" \
			-e "s|#SRCXARCH#|$(SRC_xarch)|g" \
			-e "s|#SRCARCH#|$(SRC_arch)|g" \
			-e "s|#SRCOTHERARCH#|$(SRC_other_arch)|g" \
			-e "s|#SRCLIBDIR#|$(SRC_libdir)|g" \
			-e "s|#DEB_HOST_MULTIARCH#|$(DEB_HOST_MULTIARCH)|g" \
			-e "s|#OTHER_ARCH#|$(OTHER_ARCH)|g" \
			-e "s|#CORERESTOREDIR#|$(PKG_driver_core_bck_dir)|g" \
			-e "s|#RESTOREDIR#|$(PKG_driver_bck_dir)|g" \
			-e "s|#AMDCCCLERESTOREDIR#|$(PKG_control_bck_dir)|g" \
			debian/$$i.in > debian/$$i;      \
	done

build: build-arch
build-arch: regen-from-templates
	# remove exec bit on everything
	find arch \
		etc \
		lib \
		module \
		usr \
		$(SRC_xarch)     -type f | xargs chmod -x

	# set executable on user apps
	find arch/$(SRC_arch)/usr/sbin \
		arch/$(SRC_arch)/usr/X11R6/bin \
		usr/sbin/ -type f | xargs chmod a+x

	# set exec bit on scripts
	find lib etc debian -name "*.sh" -type f | xargs chmod +x

	# set the permissions on the pxpress scripts
	chmod 744 debian/pxpress/switch*

	dh build

.PHONY: gen-abi-substvars
gen-abi-substvars:
	# Generate the xserver ABI dependencies
	cat debian/substvars >> debian/$(PKG_driver).substvars

pre-binary-arch: refresh_copyright gen-abi-substvars
	#Steps that we can't easily represent in debhelper files or .in files yet

	# Remove any libraries that may be caught by shell expansion
	find . -name libGLE* | xargs rm -f
	find . -name libEGL* | xargs rm -f

	# Code for the CORE package
ifeq ($(DEB_HOST_ARCH),amd64)
	dh_install -p$(PKG_driver_core) "arch/x86/usr/lib/*.so*"                 "$(libdir32)"
	dh_install -p$(PKG_driver_core) "arch/x86/usr/X11R6/lib/libatiadlxx.so"  "$(libdir32)"
	dh_install -p$(PKG_driver_core) "arch/x86/usr/lib/*.so*"                 "$(libdir32)"

	#they don't provide the symlink for us (starting at 8.699)
	dh_link -p$(PKG_driver_core) $(libdir32)/libatiuki.so.1.0 $(libdir32)/libatiuki.so.1
endif
	dh_installdirs -p$(PKG_driver_core)

	if [ -f "arch/$(SRC_arch)/usr/X11R6/bin/amd-console-helper" ]; then \
		dh_install -p$(PKG_driver_core) "arch/$(SRC_arch)/usr/X11R6/bin/amd-console-helper" $(bindir); \
		chmod ug+s debian/$(PKG_driver_core)/$(bindir)/amd-console-helper; \
	fi;

	# Blacklist any other driver that udev may want to load instead of fglrx
	# and create an alias for the module so that it can be used as fglrx
	printf '$(subst $(newline),\n,${blacklist_file})' > $(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libconfdir)/core-modprobe.conf

	# This is empty, just for the alternative that blacklists the driver
	echo "" > "$(CURDIR)/debian/$(PKG_driver_core)/$(CORE_ld_so_conf)"

	# This is empty, just for the alternative that unblacklists the driver
	echo "" > "$(CURDIR)/debian/$(PKG_driver_core)/$(UNBLKLST_CORE_ld_so_conf)"

	dh_install -p$(PKG_driver_core)

	# Make sure that every binary in bin is executable
	find debian/$(PKG_driver_core)/$(bindir)/ \
		-type f | xargs chmod a+x

	# Make sure that every binary in sbin is executable
	find debian/$(PKG_driver_core)/usr/sbin/ \
		-type f | xargs chmod a+x

	# End of code for the CODE package

ifeq ($(DEB_HOST_ARCH),amd64)
	dh_install -p$(PKG_driver)    "arch/x86/usr/X11R6/lib/libAMD*.so*"           "$(libdir32)"
	dh_install -p$(PKG_driver) "arch/x86/usr/X11R6/lib/*.*"           "$(PKG_libdir32)"
	dh_install -p$(PKG_driver) "arch/x86/usr/X11R6/lib/fglrx/*.so*"     "$(PKG_libdir32)"
	dh_installdirs -p$(PKG_driver) "$(PKG_libdir32)"
	dh_install -p$(PKG_driver) "arch/x86/usr/X11R6/lib/modules/dri"     "$(PKG_libdir32)"
	dh_install -p$(PKG_driver) "arch/x86/usr/lib/*.so*"                 "$(PKG_libdir32)"

	# Install the QT libraries
	dh_install -p$(PKG_driver) "arch/x86_64/usr/share/ati/lib64" "$(datadir)/ati"
else
	dh_installdirs -p$(PKG_driver)

	# Install the QT libraries
	dh_install -p$(PKG_driver) "arch/x86/usr/share/ati/lib" "$(datadir)/ati"
endif
	dh_installdirs -p$(PKG_driver_dev)
	dh_installdocs -p$(PKG_driver) usr/share/doc/fglrx/* --exclude LICENSE.TXT
	dh_installdocs -p$(PKG_driver) debian/AUTHORS
	dh_installdocs -p$(PKG_driver) debian/copyright

	dh_installinit -p$(PKG_driver) -n --name="atieventsd" --no-start --update-rcd-params="defaults 31"

	dh_install -p$(PKG_control)
	dh_install -p$(PKG_driver)
	dh_install -p$(PKG_driver_dev)

ifeq ($(DEB_HOST_ARCH),amd64)
	# Create links for 32 bit libGL
	dh_link -p$(PKG_driver) $(PKG_libdir32)/libGL.so.1.2 $(PKG_libdir32)/libGL.so.1
	dh_link -p$(PKG_driver_dev) $(PKG_libdir32)/libGL.so.1 $(PKG_libdir32)/libGL.so
	dh_link -p$(PKG_driver_dev) $(PKG_libdir32)/libAMDXvBA.so.1 $(PKG_libdir32)/libAMDXvBA.so
	dh_link -p$(PKG_driver_dev) $(PKG_libdir32)/libXvBAW.so.1 $(PKG_libdir32)/libXvBAW.so
endif

	# Make some additional scripts executable
	find debian/$(PKG_control)/$(bindir)/ \
		-type f | xargs chmod a+x

	# Make sure that every binary in bin is executable
	find debian/$(PKG_driver)/$(bindir)/ \
		-type f | xargs chmod a+x

	# Rename libraries which are supposed to have fglrx-libGL as a prefix
	for lib in `find debian/$(PKG_driver)/ -name 'fglrx-libGL*'`; \
		do \
			file_name=`echo $$lib | awk -F/ '{print $$NF}'`; \
			path=`echo $$lib | sed -e "s|\/$$file_name|\/|"`; \
			# Remove fglrx prefix \
			new_name=`echo $$file_name | sed -e "s|fglrx\-||"`; \
			full_path=`echo "$$path$$new_name"`; \
			mv -f $$lib $$full_path; \
	done

	# Rename libraries which are supposed to have fglrx-libglx as a prefix
	for lib in `find debian/$(PKG_driver)/ -name 'fglrx-libglx*'`; \
		do \
			file_name=`echo $$lib | awk -F/ '{print $$NF}'`; \
			path=`echo $$lib | sed -e "s|\/$$file_name|\/|"`; \
			new_path=`echo $$path | sed -e 's/fglrx\/$$//'`; \
			# Remove fglrx prefix \
			new_name=`echo $$file_name | sed -e "s|fglrx\-||"`; \
			full_path=`echo "$$new_path$$new_name"`; \
			mv -f $$lib $$full_path; \
	done

	# Create links for PowerXpress X modules (except for extensions)
	for i in \
		debian/$(PKG_driver)/$(PKG_xmoddir)/* \
		; do \
			orig_name=`echo $$i  | sed -e "s|debian/$(PKG_driver)/||"`; \
			if [ `echo $$orig_name  | sed -e "s|$(PKG_xmoddir)/||"` != "extensions" ]; then \
				link_name=$$orig_name ; \
				link_name=`echo $$orig_name  | sed -e "s|$(PKG_xmoddir)|$(PX_xmoddir)|"`; \
				dh_link -p$(PKG_driver) $$orig_name $$link_name ; \
			fi \
		done

	# Blacklist any other driver that udev may want to load instead of fglrx
	# and create an alias for the module so that it can be used as fglrx
	#printf '$(subst $(newline),\n,${blacklist_file})' > $(CURDIR)/debian/$(PKG_driver)/$(PKG_libconfdir)/modprobe.conf

	#ld.so.conf
	echo "/$(PKG_libdir)" >	"$(CURDIR)/debian/$(PKG_driver)/$(ld_so_conf_path)"
ifeq ($(DEB_HOST_ARCH),amd64)
	echo "/$(PKG_libdir32)" >>	"$(CURDIR)/debian/$(PKG_driver)/$(ld_so_conf_path)"
endif

	# ld.so.conf for PowerXpress
	echo "/usr/lib/$(DEB_HOST_MULTIARCH)/mesa" >		"$(CURDIR)/debian/$(PKG_driver)/$(PX_ld_so_conf)"
	#echo "/$(PX_libdir)" >>	"$(CURDIR)/debian/$(PKG_driver)/$(PX_ld_so_conf)"
ifeq ($(DEB_HOST_ARCH),amd64)
	echo "/usr/lib/$(OTHER_ARCH)/mesa" >>   "$(CURDIR)/debian/$(PKG_driver)/$(PX_ld_so_conf)"
	#echo "/$(PX_libdir32)" >>	"$(CURDIR)/debian/$(PKG_driver)/$(PX_ld_so_conf)"
endif

	# empty ld.so.conf for the fake multi-arch alternative
	echo "" > "$(CURDIR)/debian/$(PKG_driver)/$(alt_ld_so_conf_path)"

	# empty ld.so.conf for the fake multi-arch alternative for PXpress
	echo "" > "$(CURDIR)/debian/$(PKG_driver)/$(PX_ld_so_conf_alt)"

	# Generate modaliases
	sh -e debian/modaliases/fglrx_supported \
		lib/modules/fglrx/build_mod/fglrxko_pci_ids.h $(PKG_module) $(PKG_driver) > \
		debian/$(PKG_driver).modaliases
	dh_modaliases
	rm debian/$(PKG_driver).modaliases

	# Remove all the binaries and shell scripts that already are in the
	# core package from the x driver package
	# PKG_driver_core always wins against PKG_driver
	for i in $$(find debian/$(PKG_driver_core)/ -exec file {} \; | grep -e "shell" -e "ELF" | cut -d: -f1); do \
		i_name="$${i##*/}"; \
		for j in $$(find debian/$(PKG_driver)/ -exec file {} \; | grep -e "shell" -e "ELF" | cut -d: -f1); do \
			j_name="$${j##*/}"; \
			# Leave the DEBIAN directory and copyright files alone \
			if [ -z "`echo $$i | grep -e \"DEBIAN\" -e \"copyright\"`" ] && [ "$$i_name" = "$$j_name" ]; then \
				echo "$$i_name found in both $(PKG_driver_core) and $(PKG_driver)"; \
				#echo "orig: $$i"; \
				#echo "dupe: $$j"; \
				echo "removing duplicate in $(PKG_driver): $$j"; \
				rm -f "$$j"; \
			fi; \
		done \
	done

	# Disable the stack markings on all binaries in PKG_driver_core
	for i in $$(find debian/$(PKG_driver_core)/ -exec file {} \; | grep -e "ELF" | cut -d: -f1); do \
		execstack -q $$i && execstack -c $$i || true; \
	done

	# Disable the stack markings on all binaries in PKG_driver
	for i in $$(find debian/$(PKG_driver)/ -exec file {} \; | grep -e "ELF" | cut -d: -f1); do \
		execstack -q $$i && execstack -c $$i || true; \
	done

	# Disable the stack markings on all binaries in PKG_control
	for i in $$(find debian/$(PKG_control)/ -exec file {} \; | grep -e "ELF" | cut -d: -f1); do \
		execstack -q $$i && execstack -c $$i || true; \
	done

	# Create a backup directory for fglrx
	mkdir -p $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(bindir) \
	         $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir)/dri

	cp -a debian/$(PKG_driver)/$(bindir)/amdconfig $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(bindir)
	cp -a debian/$(PKG_driver)/$(bindir)/aticonfig $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(bindir)
	cp -a debian/$(PKG_driver)/$(bindir)/atiode $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(bindir)
	cp -a debian/$(PKG_driver)/$(bindir)/fglrxinfo $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(bindir)
	cp -a debian/$(PKG_driver)/$(bindir)/fgl_glxgears $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(bindir)

	cp -a debian/$(PKG_driver)/$(libdir)/libAMDXvBA.cap $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir)
ifeq ($(DEB_HOST_ARCH),amd64)
	mkdir -p $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir32)/fglrx
	cp -a debian/$(PKG_driver)/$(libdir32)/fglrx/libAMDXvBA.cap $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir32)/fglrx
endif

	cp -a debian/$(PKG_driver)/$(libdir)/dri/fglrx_dri.so $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir)/dri
ifeq ($(DEB_HOST_ARCH),amd64)
	mkdir -p $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir32)/fglrx/dri
	cp -a debian/$(PKG_driver)/$(libdir32)/fglrx/dri/fglrx_dri.so $(CURDIR)/debian/$(PKG_driver)/$(PKG_driver_bck_dir)/$(libdir32)/fglrx/dri
endif

	# Create a backup directory for fglrx-amdcccle
	mkdir -p $(CURDIR)/debian/$(PKG_control)/$(PKG_control_bck_dir)/$(bindir)

	cp -a debian/$(PKG_control)/$(bindir)/amdupdaterandrconfig $(CURDIR)/debian/$(PKG_control)/$(PKG_control_bck_dir)/$(bindir)
	cp -a debian/$(PKG_control)/$(bindir)/amdxdg-su $(CURDIR)/debian/$(PKG_control)/$(PKG_control_bck_dir)/$(bindir)
	cp -a debian/$(PKG_control)/$(bindir)/amdcccle $(CURDIR)/debian/$(PKG_control)/$(PKG_control_bck_dir)/$(bindir)

binary-arch: pre-binary-arch
	#Run the normal stuff
	dh binary-arch

binary: binary-arch binary-indep gen-abi-substvars ;
	#Run the normal stuff
	dh binary

binary-no-x: build-arch pre-binary-arch ;
	dh binary -p$(PKG_driver_core)
	dh binary -p$(PKG_driver_dev)

override_dh_shlibdeps:
ifeq ($(DEB_HOST_ARCH),amd64)
	dh_shlibdeps -l$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir):$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir)/bin:$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir)/sbin:$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir32):$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir):$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir)/bin:$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir)/sbin:$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir32) -Xlib32
else
	dh_shlibdeps -l$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir):$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir)/bin:$(CURDIR)/debian/$(PKG_driver)/$(PKG_libdir)/sbin:$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir):$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir)/bin:$(CURDIR)/debian/$(PKG_driver_core)/$(PKG_libdir)/sbin
endif

override_dh_fixperms:
	dh_fixperms -Xamd-console-helper

%:
	dh $@
