#!/bin/bash

# Copyright (C) 2016 NIWA Hideyuki

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

SLOTOSDIR=/var/lib/slot-os

usage()
{
	echo "usage: resume NUMBER ..."
	echo "usage: resume all"
}

lsdir() {
  ls -f --ind=none $1 | sed '/^\.\{1,2\}$/d'
}


if [ $# -lt 1 ]; then
	usage
	exit -1
fi

RCODE=0

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

SNUMS=$*
if [ x"$1" == x"all" ]; then
	SNUMS=`ls $SLOTOSDIR/slot | sort -n`
fi

for SNUM in $SNUMS
do
	if [ ! -x $SLOTOSDIR/slot/$SNUM ]; then
		printf "error: slot%2d is not exist\n" $SNUM
		RCODE=-1
		continue
	fi

	NAME=`cat $SLOTOSDIR/slot/$SNUM/fulcon`
	if [ x"$NAME" == x"-" -o x"$NAME" == x"" ]; then
		printf "error: slot%2d has not a container\n" $SNUM
		RCODE=-1
		continue
	fi

	fulcon resume $NAME

done

exit $RCODE

