#!/bin/bash -

LXC_SPOOL_PATH=/var/spool/lxc/
LXC_LIB_PATH=/var/lib/lxc/
ALL_LXC_NAME="$(cat /root/mylxc.txt)"

if [[ $2 == "all" ]] ; then
	LXC_NAME=$ALL_LXC_NAME ;
else
	LXC_NAME=$2 ;
fi

case "$1" in

start) 
	for i in $LXC_NAME ; do 
		if ! /usr/bin/lxc-info -n $i | grep -q RUNNING ; then
			echo "mounting $i"
			if ! /bin/mount ${LXC_SPOOL_PATH}${i} ; then
				echo "unable to mount $i"
			else
				echo "$i starting now ... please wait 5 secondes"
				#LXCI=$i
				lxc-start -n $i -d ; 
				sleep 5 ;
			fi
		else
			echo "$i is already start"
		fi
 	done
;;

status) 
	for i in $LXC_NAME ; do 
		 /usr/bin/lxc-info -n $i  
	done
;;

stop)
	for i in $LXC_NAME ; do 
		if ! /usr/bin/lxc-info -n $i | grep -q RUNNING ; then
			echo "$i is already stopped" ;
		else
			echo "$i stopping now ... " ;
			if ! kill -PWR $(/usr/bin/lxc-ps -C init | grep $i | /usr/bin/awk '{print$2}') ; then
				echo "unable to power off $i" ;
			else
				echo "please wait 5 secondes " ;	
				sleep 5 ;
				lxc-stop -n $i ; 
				echo "umounting $i" ; 
				if ! /bin/umount ${LXC_SPOOL_PATH}${i} ; then
					echo "unable to umount $i" ;
				fi
			fi
		fi
	 done
;;

*)
echo -e 'options are "start" OR "stop" OR "status" AND "lxc name" OR "all"' 
;;

esac