function Build()
{
    if [ $2 = "clean" ]
    then
        docker build -t $1 --no-cache -f $3 $4
    else
        docker build -t $1 -f $3 $4
    fi
}
function Run()
{
    docker stop $1
    docker rm $1
    if [ $2 = "i" ]
    then
        docker run -it --name $1 -h localhost -p $3 $1
    else
        docker run -d --name $1 -h localhost -p $3 $1
    fi
}
function Attach()
{
    if [ $2 = "i" ]
    then
        docker attach $1
    else
        docker attach $1
    fi

}
function GarbageCollect()
{
    docker rmi $(docker images -f "dangling=true" -q)
}
function DeleteAll()
{
    docker kill $(docker ps -q)
    docker rmi -f $(docker images -a -q)
}

function coloredEcho()
{
    local exp=$1;
    local color=$2;
    if ! [[ $color =~ '^[0-9]$' ]] ; then
       case $(echo $color | tr '[:upper:]' '[:lower:]') in
        black) color=0 ;;
        red) color=1 ;;
        green) color=2 ;;
        yellow) color=3 ;;
        blue) color=4 ;;
        magenta) color=5 ;;
        cyan) color=6 ;;
        white|*) color=7 ;; # white or invalid color
       esac
    fi
    tput setaf $color;
    echo -e "${exp}";
    tput sgr0;
}
function yes_or_no {
    while true; do
        read -p "$* [y/n]: " yn
        case $yn in
            [Yy]*) return 0  ;;  
            [Nn]*) echo "Aborted" ; return  1 ;;
        esac
    done
}

#docker rmi $(docker images -f "dangling=true" -q)

target=""
build=""
run=""
dockerfile="Dockerfile"
context="."
port="8888:8888"
command=""
usage="$(basename "$0")"'  [-f dockerfile] [-b clean or incr] [-r i or d] [-t target] [-e container] [-g]  [-D]
where:
	-t  targetname
	-b  build clean or incremental
	-r  run interactive or detached
	-h  show this help text 
	-D  Delete all images 	 
	-g  Garbage collect images
    -f  Dockerfile name
    -c  docker buid context
    -p  port pair <port external>:<port internal>
    -e  exec specific command in the container specified by target
examples:
    // below docker build context is current directory
    '"$0"' -b clean -t test -f Dockerfile -c .
    // docker build context is parent directory of the parent directory to pwd
    '"$0"' -b incr -r i -t -f ./Dockerfile test -c ../.. -p 8888:8888
    '"$0"' -t targetcontainer -e command
    '


while getopts ":f:b:r:t:c:p:e:ghD" opt;do
  case $opt in
    g)
        GarbageCollect
        exit 0
        ;;
    D)
        yes_or_no "Delete All Docker Images?" && DeleteAll
        exit 0
        ;;
  	b)
        build="$OPTARG"
		;;
  	t)
        target="$OPTARG"
		;;
  	r)
        run=$OPTARG
      ;;
    
  	p)
        port=$OPTARG
      ;;
    
    h)
	    coloredEcho "${usage}" red 
	    exit 1
	    ;;
    f)
        dockerfile=$OPTARG
        ;;
    c)
        context=$OPTARG
        ;;
    e)
        command=$OPTARG
        ;;
    *)
	    coloredEcho "${usage}" red 
	    exit 1
	    ;;
	
  esac
done

echo build=$build target=$target context=$context run=$run
if [ -z $target ]
then
	coloredEcho "${usage}" red
    exit 1
fi
if [ ! -z $command ]
then
    docker exec -it $target $command
    exit
fi


if [ ! -z $build ]
then
    Build $target $build $dockerfile $context
fi
if [ ! -z $run ]
then
    Run $target $run $port
fi


