#! /bin/sh
# preinst script for fglrx
#
# see: dh_installdeb(1)
#
# Copyright (C) 2007 Mario Limonciello
# Copyright (C) 2009-2011 Canonical Ltd.

set -e

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
#
# For details see /usr/share/doc/packaging-manual/

PKGNAME=#DRIVERNAME#
OLDPKGNAME=xorg-driver-fglrx

if [ `dpkg --print-architecture` = "amd64" ]; then
	HAS_LIB32="yes"
fi

fix_diversions()
{
    # Remove very old style diversions
    if [ -d /usr/share/fglrx/diversions ]; then
	if [ -n "$HAS_LIB32" ]; then
	    dpkg-divert --remove --package $OLDPKGNAME \
		--divert /usr/share/fglrx/diversions/libGL.so.1.2_32bit \
		--rename /usr/lib32/libGL.so.1.2
	fi

	dpkg-divert --remove --package $OLDPKGNAME \
	    --divert /usr/share/fglrx/diversions/libGL.so.1.2 \
	    --rename /usr/lib/libGL.so.1.2

	rmdir /usr/share/fglrx/diversions || true
    fi

    #remove old and unnecessary libGL.so.1 symlinks
    if [ -L /usr/lib/fglrx/libGL.so.1.xlibmesa ]; then
        rm  /usr/lib/fglrx/libGL.so.1.xlibmesa
    fi

    if [ -L /usr/lib32/fglrx/libGL.so.1.xlibmesa ]; then
        rm  /usr/lib32/fglrx/libGL.so.1.xlibmesa
    fi
    #remove unnecessary X11R6 symlinks that may not have
    #been cleaned up in earlier releases
    for i in /usr/lib/libGL.so.1 \
             /usr/lib32/libGL.so.1 \
             /usr/lib/libGL.so.1.2 \
             /usr/lib32/libGL.so.1.2 \
             /usr/X11R6/lib/libGL.so.1 \
             /usr/X11R6/lib/libGL.so.1.2 \
             /usr/X11R6/lib32/libGL.so.1 \
             /usr/X11R6/lib32/libGL.so.1.2 ; do \
	dpkg-divert --remove --package $OLDPKGNAME $i > /dev/null; \
     done

    #before 8.699 we had to divert libdri
    if dpkg-divert --list $OLDPKGNAME | \
       grep /usr/lib/xorg/modules/extensions/libdri.so > /dev/null ; then
        rm -f /usr/lib/xorg/modules/extensions/libdri.so > /dev/null
        dpkg-divert --remove --rename --package $OLDPKGNAME \
        --divert /usr/lib/fglrx/libdri.so.xlibmesa \
        /usr/lib/xorg/modules/extensions/libdri.so > /dev/null
    fi

}

case "$1" in
    install|upgrade)

        ### remove old fglrx diversions ###
	fix_diversions

        # Remove the file if it's not a symlink (see LP: #552903)
        if [ ! -L /etc/X11/Xsession.d/10fglrx ]; then
            rm -f /etc/X11/Xsession.d/10fglrx
        fi

        ## Just in case they installed without debs before, finish a proper uninstall routine ##
        ## This will error out on any failure (which should be cleaned up anyway before installing debs) ##
        [ -x /usr/share/ati/fglrx-uninstall.sh ] && /usr/share/ati/fglrx-uninstall.sh

        # Remove the directory if it's not a symlink (as it should be
        # a slave link now)
        if [ ! -L /etc/ati ]; then
            rm -Rf /etc/ati
        fi
    ;;

    abort-upgrade)
    ;;

    *)
        echo "preinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
