#!/bin/bash

# Copyright (C) 2016 NIWA Hideyuki

FULCONDRIVER=`fulcon driver-name`
PATH=/usr/lib/fulcon/driver/$FULCONDRIVER:/usr/lib/fulcon/lib:$PATH
FULCONDIR=/var/lib/fulcon
SLOTOSDIR=/var/lib/slot-os

usage()
{
	echo "usage: auotstart [ on | off ] ( all | SLOT_NUMBER...) "
}


if [ $# -eq 1 -a x"$1" == x"autostart" ]; then
	usage
	exit -1
fi

if [ x"$1" == x"on" ]; then
	ONOFF="on"
	shift
elif [ x"$1" == x"off" ]; then
	ONOFF="off"
	shift
fi

if [ x"$1" == x"all" ]; then
	NUMS=`ls $SLOTOSDIR/slot`
else
	NUMS=$*
fi

if [ x"$NUMS" == x"" ]; then
	usage
	exit -1
fi

for NUM in $NUMS
do
	if [ ! -d $SLOTOSDIR/slot/$NUM ]; then
		echo "error: slot $NUM is not exist"
		continue
	fi
	NAME=`cat $SLOTOSDIR/slot/$NUM/fulcon`

	if [ x"$ONOFF" == x"on" ]; then
		touch $SLOTOSDIR/slot/$NUM/autostart
		fulcon autostart on $NAME
	elif [ x"$ONOFF" == x"off" ]; then
		rm -f  $SLOTOSDIR/slot/$NUM/autostart
		fulcon autostart off $NAME
	else
		fulcon autostart $NAME
	fi
done

exit 0
