#!/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

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

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

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

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

for NAME in $NAMES
do
	if [ ! -d $FULCONDIR/container/$NAME ]; then
		echo "error: $NAME is not exist"
		continue
	fi

	if [ x"$ONOFF" == x"on" ]; then
		touch $FULCONDIR/container/$NAME/autostart
	elif [ x"$ONOFF" == x"off" ]; then
		rm -f $FULCONDIR/container/$NAME/autostart
	fi

	if [ -f $FULCONDIR/container/$NAME/autostart ]; then
		echo auotstart on $NAME 
	else
		echo autostart off $NAME 
	fi
done

exit 0
