#!/bin/sh

BOOT_BOOTM_OPTS=
BOOT_DRYRUN=
BOOT_VERBOSE=
list=
bootsrc=${global.boot.default}

usage="
$0 [OPTIONS] [source]\n
 -v  verbose\n
 -d  dryrun\n
 -l  list boot sources\n
 -h  help"

. /env/data/ansi-colors

while getopt "vdhl" opt; do
	if [ ${opt} = v ]; then
		BOOT_BOOTM_OPTS="$BOOT_BOOTM_OPTS -v"
		BOOT_VERBOSE=1
	elif [ ${opt} = d ]; then
		BOOT_DRYRUN=1
	elif [ ${opt} = l ]; then
		list=1
	elif [ ${opt} = h ]; then
		echo -e "$usage"
		exit 0
	fi
done

if [ -n "$list" ]; then
	echo "boot sources:"
	for i in /env/boot/*; do
		basename $i s
		echo $s
	done
	exit 0
fi

if [ -n "$1" ]; then
	bootsrc="$*"
fi

export BOOT_BOOTM_OPTS
export BOOT_DRYRUN
export BOOT_VERBOSE

for src in $bootsrc; do
	if [ -d ${src} ]; then
		realsrc="$realsrc $src/*"
	else
		realsrc="$realsrc $src"
	fi
done

if [ -n "$BOOT_VERBOSE" ]; then
	echo -e "\nboot sequence:${YELLOW}$realsrc${NC}\n"
fi

for s in $realsrc; do
	_boot_list $s
done

exit $ret
