#!/bin/sh
#
# Update Debian installation after retore:
# - update mdadm.conf if applicable
# - update configuration of installed kernel packages 
#
# Original version by Guillaume Pernot - thank you!
#
# Changelog:
# 03Jun06AL: - initial version
# 08Oct06AL: - added COLUMNS parameter so that dpkg outputs all information
#              (sarge compatibility) - thank you, Augustin Amann
#

# Restore target
TARGET=$1

# Variables
TMP_CONF=/tmp/mdadm.conf
BAK_CONF=$1/etc/mdadm/mdadm.conf.bak
ACT_CONF=$1/etc/mdadm/mdadm.conf

# Make sure dpkg doesn't truncate output lines
export COLUMNS=200

# Update mdadm.conf in target if it exists
if [ -f $ACT_CONF ]; then
	echo "DEVICE partitions" >  $TMP_CONF
	mdadm --detail --scan    >> $TMP_CONF
	cp $ACT_CONF $BAK_CONF
	cp $TMP_CONF $ACT_CONF
fi

# Mount proc and sys in restore target
mount -t proc proc $TARGET/proc
mount -t sysfs sys $TARGET/sys

# Update configuration for installed 2.4 kernels
chroot $TARGET                      \
dpkg -l kernel-image\* | grep ^ii | \
while read status pkg junk ; do
        chroot $TARGET dpkg-reconfigure -fnoninteractive $pkg
done

# Update configuration for installed 2.6 kernels
chroot $TARGET                     \
dpkg -l linux-image\* | grep ^ii | \
while read status pkg junk ; do
        chroot $TARGET dpkg-reconfigure -fnoninteractive $pkg
done

# Bring down network interfaces in restore target
ifdown --all

# Unmount sys and proc in restore target
umount $TARGET/proc $TARGET/sys

# Finish script
exit 0
