#!/bin/sh

find(){
 
        #sudo ovs-vsctl --data=bare --no-heading --columns=name find interface external_ids:container_id=**con1** external_ids:container_iface=**eth2**
        sudo ovs-vsctl --data=bare --columns=external_ids,name find interface 
}

run() {
	BRIDGE=$2
	INTERFACE=$3
	CONTAINER=$4
	CONTAINER_DIR=$5
	IPADDR=$6
	IPMASK=$7 
        GATEWAY=$8
	sudo ovs-vsctl show
        echo "Delete previous assigned ports for container: $CONTAINER..."
	sudo ovs-docker del-ports $BRIDGE $CONTAINER 
        echo "starting container $CONTAINER..."
	#env $(cat $CONTAINER_DIR/.env | grep "#" -v) docker-compose -f $CONTAINER_DIR/docker-compose.yml up -d
#        docker run -d -p 6080:80 -p 5900:5900 -v /dev/shm:/dev/shm  --name $CONTAINER --cap-add=NET_ADMIN -e USER=mickey -e PASSWORD=mouse -e RESOLUTION=1920x1080 --net=none --rm dorowu/ubuntu-desktop-lxde-vnc:focal 
        docker run -d -v /dev/shm:/dev/shm  --name $CONTAINER --device /dev/snd -e ALSADEV=hw:3,0 --cap-add=NET_ADMIN -e USER=mickey -e PASSWORD=mouse -e RESOLUTION=1920x1080 --net=none --rm ub1 

	sleep 1
        echo "assign $INTERFACE to $CONTAINER with ipaddress $IPADDR/$IPMASK..." 
	sudo ovs-docker add-port $BRIDGE $INTERFACE $CONTAINER --ipaddress=$IPADDR/$IPMASK --gateway=$GATEWAY
	#sudo ovs-docker add-port ovs-br1 eth2 con2 --ipaddress=192.168.6.11/24
        docker exec -it $CONTAINER ip route add default via $GATEWAY dev $INTERFACE
        docker exec -it $CONTAINER route 
        sudo ovs-vsctl show


}


destroy(){
	BRIDGE=$2
	CONTAINER=$3

        echo "Stopping $BRIDGE ...  $CONTAINER..."
	docker stop $CONTAINER
        echo "Delete previous assigned ports for container: $CONTAINER..."
	sudo ovs-docker del-ports $BRIDGE $CONTAINER 
	sudo ovs-vsctl show
        
     
}


usage() {
    cat << EOF
    
    PARAM:
     run 	BRIDGE INTERFACE CONTAINER CONTAINER_DIR IPADDR MASK 
     stop       BRIDGE CONTAINER	
EOF
}

if [ $# -eq 0 ]; then
    usage
    exit 0
fi

case $1 in
    "run")
          run "$@"
          exit 0
          ;;
     "stop") 
          destroy "$@"
          exit 0
          ;;
     "find") 
          find "$@"
          exit 0
          ;;

esac
