#!/bin/bash

function docker_instance_is_running() {
        # $1 instance name
        # ret:
        #       0 is running
        #       1 is NOT running

        ID=`docker ps -f name=ars_$1 --format="{{.ID}}"`

        if [ -n "$ID" ]; then
                return 0
        else
                return 1
        fi
}

function kvm_instance_is_running() {
	virsh list | grep arouteserver_openbgpd &>/dev/null

	if [ $? -eq 0 ]; then
		return 0
	else
		return 1
	fi
}

INSTANCE_NAME="$1"

shift

docker_instance_is_running "$INSTANCE_NAME"

if [ $? -eq 0 ]; then
	# docker instance is running
	docker exec -it ars_$INSTANCE_NAME "$@"
else
	kvm_instance_is_running "$INSTANCE_NAME"

	if [ $? -eq 0 ]; then
		ssh -o BatchMode=yes -o ConnectTimeout=5 -o ServerAliveInterval=10 -o StrictHostKeyChecking=no root@192.0.2.2 -i ~/.ssh/arouteserver "$@"
	else
		ssh -o BatchMode=yes -o ConnectTimeout=5 -o ServerAliveInterval=10 -o StrictHostKeyChecking=no root@$INSTANCE_NAME -i ~/.ssh/arouteserver "$@"
	fi
fi
