#! /bin/sh
# postinst script for walpole-tiger-config
#
# see: dh_installdeb(1)
#
# Copyright (C) 2009 Canonical Limited
# Author: Alberto Milone <alberto.milone@canonical.com>
#
set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# For details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

# At this point a diversion has been added on the xorg.conf
# and we can modify the xorg.conf directly through X-Kit
case "$1" in
    install|upgrade|configure)

    #if [ "$1" != "upgrade" ] && [ -f /usr/bin/xorgconfig.py ]; then
    # Brute force here as dexconf seems to be called twice.
    if [ -f /usr/bin/xorgconfig.py ]; then
#        echo "Situation before using X-Kit:"
#        ls /etc/X11/
#        echo "xorg.conf before using X-Kit:"
#        cat /etc/X11/xorg.conf || true
        echo "Modifying xorg.conf through X-Kit..."
        python /usr/bin/xorgconfig.py -o /etc/X11/xorg.conf
        echo "Done"
#        echo "Situation after using X-Kit:"
#        ls /etc/X11/
#        echo "xorg.conf after using X-Kit:"
#        cat /etc/X11/xorg.conf || true

	# GMA500 vaapi workaround
	if grep -q "GMA500_WORKAROUND=yes" /etc/rc.local
	then
		echo "GMA500 vaapi workaround already present"
	else
		echo "Adding GMA500 vaapi workaround"
		sed -i 's/exit 0$/export GMA500_WORKAROUND=yes\nexit 0/g' /etc/rc.local
	fi
	
	# Grub workaround
	native_resolution=$(grep "Modeline" /var/log/Xorg.0.log | egrep -m 1 -o '".*"' | sed 's/"//g')
	
	memory_available=$(cat /proc/meminfo | grep MemTotal | sed 's/MemTotal://g' | sed 's/kB//g' | sed 's/ //g')
	if [ "$memory_available" -gt "1100000" ] 
	then
		memory_grub='1920mb'
	else
		memory_grub='896mb'
	fi
	
	if grep -q "GRUB_CMDLINE_LINUX=\"acpi_osi=Linux acpi_backlight=vendor acpi_skip_timer nomodeset video=uvesafb:mode_option=$native_resolution-24,mtrr=3,scroll=ywrap pci=nocrs mem=$memory_grub\"" /etc/default/grub
	then
		echo "Grub workaround already present"
	else
		cp /usr/share/grub/default/grub /etc/default/grub
		echo "Adding Grub workaround"
		sed -i "s/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"acpi_osi=Linux acpi_backlight=vendor acpi_skip_timer nomodeset video=uvesafb:mode_option=$native_resolution-24,mtrr=3,scroll=ywrap pci=nocrs mem=$memory_grub\"/g" /etc/default/grub	
		sed -i "s/#GRUB_GFXMODE=640x480/GRUB_GFXMODE=$native_resolution/g" /etc/default/grub
	fi
		
	# Suspend workaround
	if [ -f /usr/lib/pm-utils/sleep.d/99video ]
	then
		echo "Adding suspend workaround"
		chmod a-x /usr/lib/pm-utils/sleep.d/99video
	fi
	
	# KMS workaround (disabled)
	if grep -q "psb" /etc/initramfs-tools/modules
	then
		echo "Removing old KMS workaround"
		sed -i 's/drm_psb//g' /etc/initramfs-tools/modules
		sed -i 's/psb//g' /etc/initramfs-tools/modules
	fi
	
	# Plymouth workaround
	if grep -q "uvesafb mode_option=$native_resolution-24 mtrr=3 scroll=ywrap" /etc/initramfs-tools/modules
	then
		echo "Plymouth workaround 1 already present"
	else
		echo "Adding Plymouth workaround 1"
		echo "uvesafb mode_option=$native_resolution-24 mtrr=3 scroll=ywrap" | tee -a /etc/initramfs-tools/modules
	fi
	
	if grep -q "FRAMEBUFFER=y" /etc/initramfs-tools/conf.d/splash
	then
		echo "Plymouth workaround 2 already present"
	else
		echo "Adding Plymouth workaround 2"
		echo "FRAMEBUFFER=y" | tee /etc/initramfs-tools/conf.d/splash
	fi
	
	# Finalize workarounds
	update-grub2
	update-initramfs -u
		
    fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

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

exit 0
#DEBHELPER#

