#!/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/)

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

# Package names
PKG_driver      := fglrx-updates
PKG_driver_dev  := fglrx-updates-dev
PKG_control     := fglrx-amdcccle-updates
PKG_module      := $(shell echo "$(PKG_driver)" | sed s/\-/_/g)

# Priority of the alternatives
alt_priority         := 1001
PX_alt_priority      := 901

#Directory naming schemes
PKG_dirname          := fglrx
bindir               := usr/bin
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)
grub_blacklistdir    := usr/share/grub-gfxpayload-lists/blacklist

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)

# 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
SRC_libdir   := lib
SRC_arch     := x86
SRC_other_arch := x86_64
SRC_xarch    := xpic

ifeq ($(DEB_HOST_ARCH),amd64)
	SRC_libdir  := lib64
	SRC_arch    := x86_64
	SRC_other_arch := x86
	SRC_xarch   := $(SRC_xarch)_64a
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
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
	# 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


build:
	#Create important strings
	for i in 10fglrx \
			 dkms.conf \
			 $(PKG_driver).install \
			 $(PKG_driver_dev).install \
			 $(PKG_driver_dev).links \
			 $(PKG_control).install \
			 $(PKG_driver).grub-gfxpayload \
			 $(PKG_driver).dirs \
			 $(PKG_driver).links \
			 $(PKG_driver).postinst \
			 $(PKG_driver).postrm \
			 $(PKG_driver).preinst \
			 $(PKG_driver).prerm \
			 overrides/$(PKG_driver); 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|#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|#DRIVERCTRLNAME#|$(PKG_control)|g" \
			-e "s|#INCLUDEDIR#|$(includedir)|g" \
			-e "s|#PKGLIBCONFDIR#|$(PKG_libconfdir)|g" \
			-e "s|#GRUBBLKLISTDIR#|$(grub_blacklistdir)|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|#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" \
			debian/$$i.in > debian/$$i;      \
	done

	# 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

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

ifeq ($(DEB_HOST_ARCH),amd64)
	dh_install -p$(PKG_driver)    "arch/x86/usr/X11R6/lib/libAMD*.so*"           "$(PKG_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"

	for i in \
			debian/$(PKG_driver)/$(PKG_libdir32)/dri/fglrx_dri.so \
			debian/$(PKG_driver)/$(PKG_libdir32)/*libGL.so.* \
			; do execstack -q $$i; execstack -c $$i; done

	#they don't provide the symlink for us (starting at 8.699)
	dh_link -p$(PKG_driver) $(PKG_libdir32)/libatiuki.so.1.0 $(PKG_libdir32)/libatiuki.so.1
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"

	#remove executable bits from stack
	dh_install -p$(PKG_control)
	execstack -c debian/$(PKG_control)/$(PKG_libdir)/bin/amdcccle
	dh_install -p$(PKG_driver)
	for i in \
			debian/$(PKG_driver)/$(PKG_libdir)/bin/clinfo \
			debian/$(PKG_driver)/$(PKG_libdir)/bin/atiode \
			debian/$(PKG_driver)/$(PKG_libdir)/bin/amdnotifyui \
			debian/$(PKG_driver)/$(PKG_libdir)/dri/fglrx_dri.so \
			debian/$(PKG_driver)/$(PKG_libdir)/*libGL.so.*.* \
			debian/$(PKG_driver)/$(PKG_libdir)/*libOpenCL.so.* \
			; do execstack -q $$i; execstack -c $$i; done

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

	# Make sure that every binary in bin is executable
	find debian/$(PKG_driver)/$(PKG_libdir)/bin/ \
		-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

	# Create links for PowerXpress libs (except for libGL)
	for i in \
		debian/$(PKG_driver)/$(PKG_libdir)/* \
		; do \
			orig_name=`echo $$i  | sed -e "s|debian/$(PKG_driver)/||"`; \
			# Copy each file except for libGL* \
			if [ -f $$i ]; then \
				if [ ! `echo $$orig_name  | grep libGL` ]; then \
					link_name=$$orig_name ; \
					link_name=`echo $$orig_name  | sed -e "s|$(PKG_libdir)|$(PX_libdir)|"`; \
					dh_link -p$(PKG_driver) $$orig_name $$link_name ; \
				fi \
			else \
				# Here we only accept the dri directory \
				dir_name=`echo $$orig_name | awk -F/ '{print $$NF}'`; \
				if [ "$$dir_name" = "dri" ]; then \
					link_name=$$orig_name ; \
					link_name=`echo $$orig_name  | sed -e "s|$(PKG_libdir)|$(PX_libdir)|"`; \
					dh_link -p$(PKG_driver) $$orig_name $$link_name ; \
				fi \
			fi \
		done

ifeq ($(DEB_HOST_ARCH),amd64)
	# Create links for PowerXpress 32bit libs (except for libGL)
	for i in \
		debian/$(PKG_driver)/$(PKG_libdir32)/* \
		; do \
			orig_name=`echo $$i  | sed -e "s|debian/$(PKG_driver)/||"`; \
			# Copy each file except for libGL* \
			if [ -f $$i ]; then \
				if [ ! `echo $$orig_name  | grep libGL` ]; then \
					link_name=$$orig_name ; \
					link_name=`echo $$orig_name  | sed -e "s|$(PKG_libdir32)|$(PX_libdir32)|"`; \
					dh_link -p$(PKG_driver) $$orig_name $$link_name ; \
				fi \
			else \
				# Here we only accept the dri directory \
				dir_name=`echo $$orig_name | awk -F/ '{print $$NF}'`; \
				if [ "$$dir_name" = "dri" ]; then \
					link_name=$$orig_name ; \
					link_name=`echo $$orig_name  | sed -e "s|$(PKG_libdir32)|$(PX_libdir32)|"`; \
					dh_link -p$(PKG_driver) $$orig_name $$link_name ; \
				fi \
			fi \
		done
endif

	# 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/$(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

	#Run the normal stuff
	dh binary-arch

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

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) -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
endif

%:
	dh $@
