#!/bin/bash

docker_name=$1
docker_command=$2
PROGNAME="$(basename "$0")"



# ---------------
# Error Handling
# ---------------
set -e
trap 'catch $? $LINENO' EXIT
catch() {
#  echo "catching!"
  if [ "$1" != "0" ]; then
    #echo $1
    docker exec -ti "$docker_name" /bin/ash -c $docker_command
  else
    #echo $1
    docker exec -ti "$docker_name" /bin/bash -c $docker_command
  fi
trap - ERR
}
# ---------------
# Error Handling
# ---------------

if [ "$docker_name" == "" ]
then
  echo "$PROGNAME is expecting a container name"
  exit 1
fi

docker exec "$docker_name" /bin/bash > /dev/null ||  false # docker exec -ti "$docker_name" /bin/ash

