#! /bin/sh

set -e

MENU=/boot/grub/menu.lst

if [ "configure" = "$1" ] && `dpkg --compare-versions "$2" le-nl 0.20` ; then 
    if [ -f "$MENU" ]; then
        # Change grub timeout to 10sec if it's at the default
        # 3sec
        sed -i 's/\(^timeout[ \t]*\)3[ \t]*$/\110/g' "$MENU"
    fi

    if [ -x /usr/sbin/reorder-grub.sh ]; then
        /usr/sbin/reorder-grub.sh
    fi
fi

if [ "configure" = "$1" ] && `dpkg --compare-versions "$2" le-nl 0.30` ; then 
    # Update recovery partition udebs. Assumes athens
    # partition layout w/ Linux stuff at the end of the disk
    # Recovery partition can be either sda5 or sda6
    RECOVERY=$(sfdisk -l /dev/sda | grep 1b | tail -1 | cut -f 1 -d ' ')
    if [ "$RECOVERY" = "/dev/sda5" -o "$RECOVERY" = "/dev/sda6" ]; then
	TMPDIR=`mktemp -d` || exit 1
	mount ${RECOVERY} ${TMPDIR}

	#
	# *** Update the archive contents here ***
	# 
	SRCDIR=/usr/lib/athens-config/recovery-replacements
	DESTDIR=${TMPDIR}/pool/main/r/recovery-installer
	# recovery-installer 0.2athens[2|4] shipped in production,
	# athens6 was an update
	cp ${SRCDIR}/recovery-installer_0.2athens8_all.udeb ${DESTDIR}
	cp ${SRCDIR}/recovery-installer-prep_0.2athens8_all.udeb ${DESTDIR}
	rm -f ${DESTDIR}/recovery-installer*0.2athens[2-6]*.udeb
	#
	# *** End archive-updating code ***
	#

	# Update preseed on original BA01 systems
	if [ `grep -c "20090827-1" /etc/buildstamp` = 1 ]; then
	    cp ${SRCDIR}/BA01/recovery-preseed.cfg ${TMPDIR}/install/recovery-preseed.cfg
	fi

	/usr/lib/athens-config/regenerate-archive.sh ${TMPDIR}
	sync && sync && sync
	umount ${TMPDIR}
    fi

    # Make sure that recovery entry in menu.lst points
    # at the actual recovery partition. Only affects original
    # BA01 systems
    if [ `grep -c "20090827-1" /etc/buildstamp` = 1 ]; then
	sed -i 's/\(^root[ \t]*(hd0,\)4)[ \t]*$/\15)/g' "$MENU"
    fi

    # Make sure that swap partition has the right type
    # Only affects original BA01 systems
    if [ `grep -c "20090827-1" /etc/buildstamp` = 1 ]; then
	sfdisk --change-id /dev/sda 5 82
    fi
fi

if [ "configure" = "$1" ] && `dpkg --compare-versions "$2" le-nl 0.20.3` ; then 
    # Some systems shipped with a misconfigured update-manager
    # (since fixed in a SW update). We don't support automatic 
    # dist upgrades (for now, maybe forever)
    for dir in `ls /home`; do
	rm -f /home/${dir}/.update-manager-core/meta-release
	rm -f /var/lib/update-manager/meta-release
    done
fi


#DEBHELPER#
