#!/bin/bash

INC=/usr/share/ltsp/diskless-functions
if [ ! -e "$INC" ]; then
	echo "couldn't find $INC, exiting..." >&2
	exit 16
fi
. "$INC"


if [ -z "$1" ]; then
	echo "Usage: $PROG [-d TFTPBOOT_DIR] BASEDIR"
	exit 1
fi

while [ -n "$1" ]; do
	case "$1" in
	-d)
		shift
		BOOTDIR="$1"
		shift
		;;
	esac

	if [ ! -d "$1" ]; then
		shift
		continue
	fi

	basedir=$1
	dirs=$(find $basedir/ -mindepth 1 -maxdepth 1 -type d \! -wholename '*~' | xargs)

	for i in $dirs; do
		[ ! -d "$i/boot" ] && continue
		chmod 0644 "$i/boot/"initr* 2>/dev/null
	done

	action "running ltsp-prepare-kernel..."
	for i in $dirs; do
		ARCH=${i##*/} ltsp-prepare-kernel -b "$basedir"
	done

	action "running ltsp-update-kernels..."
	ltsp-update-kernels -b "$basedir" -d "$BOOTDIR"

	shift
done
