#!/bin/sh
# Copyright (C) 2007 Mario Limonciello
# Copyright (C) 2009-2011 Canonical Ltd.

set -e

PACKAGE_NAME=#DRIVERNAME#
CVERSION=`dpkg-query -W -f='${Version}' $PACKAGE_NAME | awk -F "-" '{print $1}' | cut -d\: -f2`

ARCH=`dpkg --print-architecture`

INIT_SCRIPT="/etc/init/build-$PACKAGE_NAME.conf"
TMP_FILE="/tmp/do_not_build_dkms_module"

# Check the existence of a kernel named as $1
_is_kernel_name_correct() {
    CORRECT="no"
    KERNEL_NAME=$1

    for kernel in /boot/config-*; do
        KERNEL=${kernel#*-}
        if [ "${KERNEL}" = "${KERNEL_NAME}" ]; then
            CORRECT="yes"
            break
        fi
    done

    echo $CORRECT
}

# Get the most recent kernel on Debian based systems. This keeps
# into account both the version and the ABI. If the current kernel
# is the most recent kernel then the function will print a null string.
_get_newest_kernel_debian() {
    NEWEST_KERNEL=
    NEWEST_VERSION=
    NEWEST_ABI=

    for kernel in /boot/config-*; do
        KERNEL=${kernel#*-}
        KERNEL_VERSION=${KERNEL%%-*}
        ABI=${KERNEL#*-}
        ABI=${ABI%%-*}

        if [ -z "$NEWEST_KERNEL" ]; then
            # The 1st time get a version which is bigger than $1
            COMPARE_TO=$1
        else
            # Get the biggest version
            COMPARE_TO="$NEWEST_VERSION-$NEWEST_ABI"
        fi

        # if $kernel is greater than $COMPARE_TO
        if [ `dpkg --compare-versions "$KERNEL_VERSION-$ABI" gt "$COMPARE_TO" && echo "yes" || \
              echo "no"` = "yes" ]; then
            NEWEST_KERNEL=$KERNEL
            NEWEST_VERSION=$KERNEL_VERSION
            NEWEST_ABI=$ABI
        fi
    done

    echo "$NEWEST_KERNEL"
}


get_newest_kernel() {
    NEWEST_KERNEL=
    # Try Debian first as rpm can be installed in Debian based distros
    if [ -e /usr/bin/dpkg ]; then
        # If DEB based
        CURRENT_KERNEL=$1
        CURRENT_VERSION=${CURRENT_KERNEL%%-*}
        CURRENT_ABI=${CURRENT_KERNEL#*-}
        CURRENT_FLAVOUR=${CURRENT_ABI#*-}
        CURRENT_ABI=${CURRENT_ABI%%-*}
        NEWEST_KERNEL=$(_get_newest_kernel_debian "$CURRENT_VERSION-$CURRENT_ABI")

    elif [ `which rpm &>/dev/null` ]; then
        # If RPM based
        NEWEST_KERNEL=$(_get_newest_kernel_rhel)
    fi

    # Make sure that kernel name that we extracted corresponds to an installed
    # kernel
    if [ -n "$NEWEST_KERNEL" ] && [ `_is_kernel_name_correct $NEWEST_KERNEL` = "no" ]; then
        NEWEST_KERNEL=
    fi

    echo $NEWEST_KERNEL
}


if [ "$1" = "configure" ]; then

    #check whether libglx.so got installed.  some releases earlier than x740
    #will not actually have this libglx.so
    if [ ! -f /usr/lib/xorg/modules/extensions/libglx.so ]; then
        dpkg-divert --remove --rename --package xorg-driver-fglrx --divert /usr/lib/fglrx/libglx.so.xlibmesa /usr/lib/xorg/modules/extensions/libglx.so > /dev/null
    fi

    # Clean up any previous non-multi-arch alternatives
    if [ -n "$(update-alternatives --list gl_conf 2>/dev/null)" ]; then
        set -a $(update-alternatives --list gl_conf 2>/dev/null)
        while [ $# -ge 1 ] && [ "$#" != "configure" ]; do
            ALTERNATIVE=${1}
            update-alternatives --remove gl_conf $ALTERNATIVE
            shift
        done
    fi

    update-alternatives --force \
        --install /#SYSCONFDIR#/ld.so.conf.d/#DEB_HOST_MULTIARCH#_GL.conf #DEB_HOST_MULTIARCH#_gl_conf /#LDSOCONF# #ALTPRIORITY# \
        --slave /#BINDIR#/aticonfig #DEB_HOST_MULTIARCH#_aticonfig /#PKGBINDIR#/aticonfig \
        --slave /#BINDIR#/amdconfig #DEB_HOST_MULTIARCH#_amdconfig /#PKGBINDIR#/amdconfig \
        --slave /#BINDIR#/atiode #DEB_HOST_MULTIARCH#_atiode /#PKGBINDIR#/atiode \
        --slave /#BINDIR#/amdnotifyui #DEB_HOST_MULTIARCH#_amdnotifyui /#PKGBINDIR#/amdnotifyui \
        --slave /#BINDIR#/amdcccle #DEB_HOST_MULTIARCH#_amdcccle /#PKGBINDIR#/amdcccle \
        --slave /#BINDIR#/amdxdg-su #DEB_HOST_MULTIARCH#_amdxdg_su /#PKGBINDIR#/amdxdg-su \
        --slave /#BINDIR#/amdupdaterandrconfig #DEB_HOST_MULTIARCH#_amdupdaterandrconfig /#PKGBINDIR#/amdupdaterandrconfig \
        --slave /#DATADIR#/applications/ubuntu-amdcccle.desktop #DEB_HOST_MULTIARCH#_amdcccle_desktop /#PKGDATADIR#/amdcccle.desktop \
        --slave /#DATADIR#/applications/ubuntu-amdccclesu.desktop #DEB_HOST_MULTIARCH#_amdccclesu_desktop /#PKGDATADIR#/amdccclesu.desktop \
        --slave /#BINDIR#/fgl_glxgears #DEB_HOST_MULTIARCH#_fgl_glxgears /#PKGBINDIR#/fgl_glxgears \
        --slave /#BINDIR#/fglrxinfo #DEB_HOST_MULTIARCH#_fglrxinfo /#PKGBINDIR#/fglrxinfo \
        --slave /#BINDIR#/atiodcli #DEB_HOST_MULTIARCH#_atiodcli  /#PKGBINDIR#/atiodcli \
        --slave /#BINDIR#/clinfo #DEB_HOST_MULTIARCH#_clinfo  /#PKGBINDIR#/clinfo \
        --slave /#BINDIR#/atieventsd #DEB_HOST_MULTIARCH#_atieventsd  /#PKGBINDIR#/atieventsd \
        --slave /#LIBDIR#/xorg/modules/drivers/fglrx_drv.so #DEB_HOST_MULTIARCH#_fglrx_drv /#PKGLIBDIR#/xorg/modules/drivers/fglrx_drv.so \
        --slave /#LIBDIR#/dri/fglrx_dri.so #DEB_HOST_MULTIARCH#_fglrx_dri /#PKGLIBDIR#/dri/fglrx_dri.so \
        --slave /#LIBDIR#/libAMDXvBA.cap #DEB_HOST_MULTIARCH#_libAMDXvBA_cap /#PKGLIBDIR#/libAMDXvBA.cap \
        --slave /#SYSCONFDIR#/modprobe.d/fglrx.conf #DEB_HOST_MULTIARCH#_fglrx_modconf /lib/fglrx/modprobe.conf \
        --slave /#SYSCONFDIR#/X11/Xsession.d/10fglrx #DEB_HOST_MULTIARCH#_10fglrx /#PKGLIBDIR#/10fglrx \
        --slave /#SYSCONFDIR#/ati #DEB_HOST_MULTIARCH#_ati_conf /#PKGLIBDIR#/#SYSCONFDIR#/ati \
        --slave /#SYSCONFDIR#/OpenCL/vendors/amdocl32.icd #DEB_HOST_MULTIARCH#_amdocl32.icd /#PKGLIBDIR#/#SYSCONFDIR#/OpenCL/vendors/amdocl32.icd \
        --slave /#SYSCONFDIR#/OpenCL/vendors/amdocl64.icd #DEB_HOST_MULTIARCH#_amdocl64.icd /#PKGLIBDIR#/#SYSCONFDIR#/OpenCL/vendors/amdocl64.icd \
        --slave /#LIBDIR#/libOpenCL.so #DEB_HOST_MULTIARCH#_libOpenCL.so /#PKGLIBDIR#/libOpenCL.so \
        --slave /#XORGEXTRA# #DEB_HOST_MULTIARCH#_xorg_extra_modules /#PKGLIBDIR#/xorg \
        --slave /#LIBDIR#/libaticalcl.so #DEB_HOST_MULTIARCH#_libaticalcl.so /#PKGLIBDIR#/libaticalcl.so \
        --slave /#LIBDIR32#/libaticalcl.so #DEB_HOST_MULTIARCH#_libaticalcl.so_lib32 /#PKGLIBDIR32#/libaticalcl.so \
        --slave /#LIBDIR#/libaticalrt.so #DEB_HOST_MULTIARCH#_libaticalrt.so /#PKGLIBDIR#/libaticalrt.so \
        --slave /#LIBDIR32#/libaticalrt.so #DEB_HOST_MULTIARCH#_libaticalrt.so_lib32 /#PKGLIBDIR32#/libaticalrt.so \
        --slave /#DATADIR#/grub-gfxpayload-lists/blacklist/10_proprietary-graphics-drivers #DEB_HOST_MULTIARCH#_grub_fb_blacklist /#PKGDATADIR#/fglrx-updates.grub-gfxpayload

    # This is for switchable graphics
    # Note: the radeon kernel module is still blacklisted
    update-alternatives --force \
        --install /#SYSCONFDIR#/ld.so.conf.d/#DEB_HOST_MULTIARCH#_GL.conf #DEB_HOST_MULTIARCH#_gl_conf /#PXLDSOCONF# #PXALTPRIORITY# \
        --slave /#BINDIR#/aticonfig #DEB_HOST_MULTIARCH#_aticonfig /#PKGBINDIR#/aticonfig \
        --slave /#BINDIR#/amdconfig #DEB_HOST_MULTIARCH#_amdconfig /#PKGBINDIR#/amdconfig \
        --slave /#BINDIR#/atiode #DEB_HOST_MULTIARCH#_atiode /#PKGBINDIR#/atiode \
        --slave /#BINDIR#/amdnotifyui #DEB_HOST_MULTIARCH#_amdnotifyui /#PKGBINDIR#/amdnotifyui \
        --slave /#BINDIR#/amdcccle #DEB_HOST_MULTIARCH#_amdcccle /#PKGBINDIR#/amdcccle \
        --slave /#BINDIR#/amdxdg-su #DEB_HOST_MULTIARCH#_amdxdg_su /#PKGBINDIR#/amdxdg-su \
        --slave /#BINDIR#/amdupdaterandrconfig #DEB_HOST_MULTIARCH#_amdupdaterandrconfig /#PKGBINDIR#/amdupdaterandrconfig \
        --slave /#DATADIR#/applications/ubuntu-amdcccle.desktop #DEB_HOST_MULTIARCH#_amdcccle_desktop /#PKGDATADIR#/amdcccle.desktop \
        --slave /#DATADIR#/applications/ubuntu-amdccclesu.desktop #DEB_HOST_MULTIARCH#_amdccclesu_desktop /#PKGDATADIR#/amdccclesu.desktop \
        --slave /#BINDIR#/fgl_glxgears #DEB_HOST_MULTIARCH#_fgl_glxgears /#PKGBINDIR#/fgl_glxgears \
        --slave /#BINDIR#/fglrxinfo #DEB_HOST_MULTIARCH#_fglrxinfo /#PKGBINDIR#/fglrxinfo \
        --slave /#BINDIR#/atiodcli #DEB_HOST_MULTIARCH#_atiodcli  /#PKGBINDIR#/atiodcli \
        --slave /#BINDIR#/clinfo #DEB_HOST_MULTIARCH#_clinfo  /#PKGBINDIR#/clinfo \
        --slave /#BINDIR#/atieventsd #DEB_HOST_MULTIARCH#_atieventsd  /#PKGBINDIR#/atieventsd \
        --slave /#LIBDIR#/xorg/modules/drivers/fglrx_drv.so #DEB_HOST_MULTIARCH#_fglrx_drv /#PKGLIBDIR#/xorg/modules/drivers/fglrx_drv.so \
        --slave /#LIBDIR#/dri/fglrx_dri.so #DEB_HOST_MULTIARCH#_fglrx_dri /#PKGLIBDIR#/dri/fglrx_dri.so \
        --slave /#LIBDIR#/libAMDXvBA.cap #DEB_HOST_MULTIARCH#_libAMDXvBA_cap /#PKGLIBDIR#/libAMDXvBA.cap \
        --slave /#SYSCONFDIR#/modprobe.d/fglrx.conf #DEB_HOST_MULTIARCH#_fglrx_modconf /lib/fglrx/modprobe.conf \
        --slave /#SYSCONFDIR#/X11/Xsession.d/10fglrx #DEB_HOST_MULTIARCH#_10fglrx /#PKGLIBDIR#/10fglrx \
        --slave /#SYSCONFDIR#/ati #DEB_HOST_MULTIARCH#_ati_conf /#PKGLIBDIR#/#SYSCONFDIR#/ati \
        --slave /#SYSCONFDIR#/OpenCL/vendors/amdocl32.icd #DEB_HOST_MULTIARCH#_amdocl32.icd /#PKGLIBDIR#/#SYSCONFDIR#/OpenCL/vendors/amdocl32.icd \
        --slave /#SYSCONFDIR#/OpenCL/vendors/amdocl64.icd #DEB_HOST_MULTIARCH#_amdocl64.icd /#PKGLIBDIR#/#SYSCONFDIR#/OpenCL/vendors/amdocl64.icd \
        --slave /#LIBDIR#/libOpenCL.so #DEB_HOST_MULTIARCH#_libOpenCL.so /#PKGLIBDIR#/libOpenCL.so \
        --slave /#XORGEXTRA# #DEB_HOST_MULTIARCH#_xorg_extra_modules /#PXDIR#/xorg \
        --slave /#LIBDIR#/libaticalcl.so #DEB_HOST_MULTIARCH#_libaticalcl.so /#PKGLIBDIR#/libaticalcl.so \
        --slave /#LIBDIR32#/libaticalcl.so #DEB_HOST_MULTIARCH#_libaticalcl.so_lib32 /#PKGLIBDIR32#/libaticalcl.so \
        --slave /#LIBDIR#/libaticalrt.so #DEB_HOST_MULTIARCH#_libaticalrt.so /#PKGLIBDIR#/libaticalrt.so \
        --slave /#LIBDIR32#/libaticalrt.so #DEB_HOST_MULTIARCH#_libaticalrt.so_lib32 /#PKGLIBDIR32#/libaticalrt.so \
        --slave /#DATADIR#/grub-gfxpayload-lists/blacklist/10_proprietary-graphics-drivers #DEB_HOST_MULTIARCH#_grub_fb_blacklist /#PKGDATADIR#/fglrx-updates.grub-gfxpayload

    # Install just the ld.so.conf.d alternatives for the secondary architecture.
    # We need only to ensure that any mesa ld.so.conf.d alternative isn't active.
    update-alternatives --force \
        --install /#SYSCONFDIR#/ld.so.conf.d/#OTHER_ARCH#_GL.conf #OTHER_ARCH#_gl_conf /#ALTLDSOCONF# #ALTPRIORITY#
    update-alternatives --force \
        --install /#SYSCONFDIR#/ld.so.conf.d/#OTHER_ARCH#_GL.conf #OTHER_ARCH#_gl_conf /#ALTPXLDSOCONF# #PXALTPRIORITY#

    # ldconfig needs to be run immediately as we're changing /etc/ld.so.conf.d/ with
    # alternatives.
    LDCONFIG_NOTRIGGER=y ldconfig

    # Trigger gmenu so that the icons will show up in the menu
    dpkg-trigger --by-package=$PACKAGE_NAME gmenucache || true

    CURRENT_KERNEL=$(uname -r)
    NEWEST_KERNEL=$(get_newest_kernel "$KERNELS")

    ALTERNATIVE=$(readlink /etc/alternatives/#DEB_HOST_MULTIARCH#_gl_conf)
    if [ "$ALTERNATIVE" = "/#LDSOCONF#" ]; then
        # Update initramfs so that the blacklist ends up in the initramfs
        update-initramfs -u

        if [ -n "$NEWEST_KERNEL" ] && [ ${CURRENT_KERNEL} != ${NEWEST_KERNEL} ]; then
            # This can fail when dealing with a chroot see LP: #556653
            update-initramfs -u -k $CURRENT_KERNEL || true
        fi
    fi

    # If the delayed module build feature is enabled
    # i.e. if $DONT_BUILD_MODULE is set to 1
    # or if $TMP_FILE exists
    if [ "${DONT_BUILD_MODULE}1" -eq "11" ] || [ -e $TMP_FILE ]; then
        # Do not build the module and create
        # the Upstart script which will
        # build the module on next boot
        cat > $INIT_SCRIPT <<EOF
# Warning: This file is autogenerated by $PACKAGE_NAME. All changes to this file will be lost.
start on (starting oem-config
          or starting gdm
          or starting kdm
          or starting xdm
          or starting uxlaunch)
task

script
     dkms add -m $PACKAGE_NAME -v $CVERSION
     /usr/lib/dkms/dkms_autoinstaller start || ( rm -f $INIT_SCRIPT && exit 1 )
     modprobe $PACKAGE_NAME || true
     rm -f $INIT_SCRIPT
end script
EOF
    else
        /usr/lib/dkms/common.postinst $PACKAGE_NAME $CVERSION /usr/share/$PACKAGE_NAME $ARCH $2
    fi

    #Update the initramfs as radeon might get loaded from it normally
    /usr/sbin/update-initramfs -u

    # Update the grub gfxpayload blacklist
    if which update-grub-gfxpayload >/dev/null 2>&1; then
        update-grub-gfxpayload
    fi
fi

# NOTE: Usually generated by dh_installinit, but because two packages are
# providing the same initscript, this needs some manual handling.
if [ -x "/etc/init.d/atieventsd" ]; then
    update-rc.d atieventsd defaults 31 >/dev/null || exit $?
fi

#DEBHELPER#
