#!/bin/sh
# postinst script for sense-hat
#
# see: dh_installdeb(1)

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>
#        * <postinst> `abort-remove'
#        * <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


case "$1" in
    configure)
        echo Enabling I2C...

        sed /boot/config.txt -i -r -e "s/^((device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?)(=[^,]*)?/\1=on/"
        if ! grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?=[^,]*" /boot/config.txt; then
            printf "dtparam=i2c_arm=on\n" >> /boot/config.txt
        fi

        if ! [ -e /etc/modprobe.d/raspi-blacklist.conf ]; then
            touch /etc/modprobe.d/raspi-blacklist.conf
        fi

        sed /etc/modprobe.d/raspi-blacklist.conf -i -e "s/^\(blacklist[[:space:]]*i2c[-_]bcm2708\)/#\1/"
        sed /etc/modules -i -e "s/^#[[:space:]]*\(i2c[-_]dev\)/\1/"
        if ! grep -q "^i2c[-_]dev" /etc/modules; then
            printf "i2c-dev\n" >> /etc/modules
        fi

        echo "If I2C was disabled prior to this installation, a reboot is required."
        echo "To work under Python 3, the Sense HAT library requires pillow."
        echo "This can be installed by running 'sudo pip-3.2 install pillow'."
    ;;

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

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

#DEBHELPER#

exit 0
