#!/bin/bash

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


[[ -z $1 ]] && {
	echo "Usage: $PROG [-d TFTPBOOT_DIR] BASEDIR"
	exit 1
}

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

	[[ ! -d $1 ]] && {
		shift
		continue
	}

	basedir=$1
	dirs=$(find "$basedir/" -mindepth 1 -maxdepth 1 -type d \! -name '_*' \! -name '*~' | 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
