#!/bin/bash

# Copyright (C) 2015-2016 NIWA Hideyuki

usage()
{
	echo "usage: lib-stop [ -f ] NAME"
}

# check options
OPT_FLG=""
FLG_F=0

while getopts f OPT
do
  case $OPT in
    "f" ) FLG_F=1; OPT_FLG=" " ;;
    \?  ) usage; exit -1;;
  esac
done

shift `expr $OPTIND - 1`

if [ $# -eq 1 ]; then
	NAME=$1
else
	usage
	exit -1
fi

fulcon-list-running | awk 'BEGIN{r=1}{if($1=="'$NAME'")r=0}END{exit(r);}'
if [ $? -ne 0 ]; then
	echo "error: can't find running" $NAME
	exit -1
fi

fulcon-stop $NAME >& /dev/null

for i in `brctl show | egrep fulcon | awk '{print $1}' `
do
	N=`brctl show $i | awk '($1=="'$i'"){print NF}'`
	if [ $N -eq 3 ]; then
		ip link set $i down
		brctl delbr $i
	fi
done

echo "stopped" $NAME
exit 0


