#! /bin/sh

set -e

. /usr/share/debconf/confmodule

if [ "configure" = "$1" ] && [ -z "$2" ]; then 
    cat > /etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
EOF
    
fi


if [ ! -f /config-done ]; then
	oem-config-prepare --quiet
	touch /config-done
fi

# Ew
if [ -f /etc/X11/Xwrapper.config ]; then
    sed -i -e 's/allowed_users=.*/allowed_users=anybody/' /etc/X11/Xwrapper.config
fi

# Double ew. Hack to work around unionfs bug/feature.
if [ ! -L /squashmnt ]; then
	ln -s / /squashmnt
fi

# Clobber existing xorg.conf with our own, ehh
cp /etc/X11/xorg.conf.netbook /etc/X11/xorg.conf

# explicitly specify modules to load during early boot as this is the fastest
# way to load them
grep sg /etc/modules > /dev/null 2>&1 || echo "sg" >> /etc/modules
grep psmouse /etc/modules > /dev/null 2>&1 || echo "psmouse" >> /etc/modules
grep serio_raw /etc/modules > /dev/null 2>&1 || echo "serio_raw" >> /etc/modules
grep evdev /etc/modules > /dev/null 2>&1 || echo "evdev" >> /etc/modules
grep intel_rng /etc/modules > /dev/null 2>&1 || echo "intel_rng" >> /etc/modules
grep iTCO_vendor_support /etc/modules > /dev/null 2>&1 || echo "iTCO_vendor_support" >> /etc/modules
grep iTCO_wdt /etc/modules > /dev/null 2>&1 || echo "iTCO_wdt" >> /etc/modules
grep ieee80211_crypt /etc/modules > /dev/null 2>&1 || echo "ieee80211_crypt" >> /etc/modules
grep r8169 /etc/modules > /dev/null 2>&1 || echo "r8169" >> /etc/modules
grep pci-hotplug /etc/modules > /dev/null 2>&1 || echo "pci-hotplug" >> /etc/modules
grep ieee80211_crypt_tkip /etc/modules > /dev/null 2>&1 || echo "ieee80211_crypt_tkip" >> /etc/modules

# Disable links for rc2.d services that we now explicitly start with runlatebootcmds
# script. Note that the packages that install the rc2.d links must be added to the
# control file's Depends field to ensure the link is properly removed.
if [ -e /etc/rc2.d/S89anacron ]; then
	mv /etc/rc2.d/S89anacron /etc/rc2.d/K89anacron > /dev/null 2>&1
fi

if [ -e /etc/rc2.d/S20apport ]; then
	mv /etc/rc2.d/S20apport /etc/rc2.d/K20apport > /dev/null 2>&1
fi

if [ -e /etc/rc2.d/S25bluetooth ]; then
	mv /etc/rc2.d/S25bluetooth /etc/rc2.d/K25bluetooth > /dev/null 2>&1
fi

if [ -e /etc/rc2.d/S20cupsys ]; then
	mv /etc/rc2.d/S20cupsys /etc/rc2.d/K20cupsys  > /dev/null 2>&1
fi

if [ -e /etc/rc2.d/S25pulseaudio ]; then
	mv /etc/rc2.d/S25pulseaudio /etc/rc2.d/K25pulseaudio > /dev/null 2>&1
fi

# Disable some upstart events
if [ -e /etc/event.d/tty3 ]; then
	update-upstart disable tty3
fi

if [ -e /etc/event.d/tty4 ]; then
	update-upstart disable tty4
fi

if [ -e /etc/event.d/tty5 ]; then
	update-upstart disable tty5
fi

if [ -e /etc/event.d/tty6 ]; then
	update-upstart disable tty6
fi

if [ "configure" = "$1" ] && `dpkg --compare-versions "$2" le-nl 0.77` ; then 
    /usr/share/ume-config-belmont/repartition-disk.sh
    cp /usr/share/ume-config-belmont/runresize2fs /etc/rcS.d/S21runresize2fs
    if [ -x /usr/share/update-notifier/notify-reboot-required ]; then
	/usr/share/update-notifier/notify-reboot-required
    fi
fi

if [ "configure" = "$1" ] && dpkg --compare-versions "$2" lt 0.30; then
    # Update any users' soft contracts
    for dir in /home/*/*/Service\ Agreements; do
        for new in /usr/share/ume-config-belmont/dell-contract/*.pdf; do
            name=$(basename "$new")
            orig="$dir/$name"
            if [ -e "$orig" ] && ! diff -q "$new" "$orig" > /dev/null; then
                ln -f "$new" "$orig"
            fi
        done
    done
fi

update-icon-caches /usr/share/icons/hicolor

#DEBHELPER#
