#!/bin/sh
set -e

. /usr/share/debconf/confmodule

log() {
	logger -t nslu2-firmware-installer "$@"
}

error() {
	log "error: $@"
}

db_progress START 0 2 nslu2-firmware-installer/progress
db_progress INFO nslu2-firmware-installer/prepare

# The non-free ixp400_eth can be manually included on a d-i image,
# if it is copy it into the installed system so it's available there too,
# and gets put into the initramfs in flash.
ixp400_found=0
for file in $(find /lib/modules -type f |grep ixp400); do
	ixp400_found=1
	dir=$(dirname $file)
	mkdir -p /target/$dir
	cp -a $file /target/$file
done
if [ "$ixp400_found" ]; then
	log "copied ixp400 driver to /target"
	chroot /target depmod -a
fi

apt-install initramfs-tools || true # probably already installed; make sure
if ! apt-install nslu2-utils; then
	error "apt-install nslu2-utils failed"
	exit 1
fi

# Set up nslu2-flashkernel to run on kernel upgrades.
if ! grep -q nslu2-flashkernel /target/etc/kernel-img.conf; then
	echo "postinst_hook = /usr/sbin/nslu2-flashkernel" >> \
		/target/etc/kernel-img.conf
fi

db_progress STEP 1
db_progress INFO nslu2-firmware-installer/flashing

if ! in-target nslu2-flashkernel; then
	error "nslu2-flashkernel failed"
	exit 1
fi

db_progress STEP 1
db_progress STOP
