#!/bin/bash

set -e
set -o pipefail

APPNAME=mani
PROJECT_DIR=$(dirname "$(cd "$(dirname "${0}")"; pwd -P)")

function help() {
  cat >&2 << EOF
This script is debugger for mani.

Options:
  --test|-t {case}     Run only cases which have specified pattern in the case names
  --count|-c {count}   Run tests multiple times, the clean flag is necessary for this flag
  --help|-h            Show this message

Examples:

  ./test/run.sh

EOF
}

function parse_options() {
  IMAGE=alpine
  SHELL=bash
  while [[ $# -gt 0 ]]; do
    case "${1}" in
      --image|-i)
        IMAGE="${2}"
        shift && shift
        ;;
      --shell|-s)
        SHELL="${2}"
        shift && shift
        ;;
      --help|-h)
        help && exit 0
        ;;
      *)
        printf "Unknown flag: ${1}\n\n"
        help
        exit 1
        ;;
    esac
  done
}

function exec_docker() {
  image="${APPNAME}/exec:${IMAGE}"

  shell=
  case $SHELL in
    zsh)
      shell="/bin/zsh"
      ;;
    fish)
      shell="/usr/bin/fish"
      ;;
    ps)
      shell="/bin/ps"
      ;;
    *)
      shell="/bin/bash"
      ;;
  esac

  docker build                                          \
    --file "$PROJECT_DIR/images/$IMAGE.exec.Dockerfile" \
    --tag ${image}                                      \
    .

  docker run                      \
    -it --rm                      \
    "$image"                      \
    "$shell"
}

function __main__() {
  parse_options $@
  exec_docker
}

__main__ $@
