#! /bin/bash -e

# XXX: At some point we probably don't need travis specific logic but for now
#      it's very easy to use this with environment variables to keep everything
#      running as it does in travis.

# test suite root for all travis ci hook style tests...
suite_root="tests/ci"

usage() {
  echo
  echo "  Usage: ./bin/ci [command] [args]"
  echo
  echo "  Commands:"
  echo
  echo "    run <suite> - run a particular test suite (use ls to see all suites)"
  echo "    ls - list all the test suites which can be run"
  echo
  echo
}

cmd_ls() {
  ls $suite_root
}

cmd_run() {
  local steps="before_install install before_script script"
  local root="$suite_root/$1/"

  if [  ! -d $root ] || [ "$1" == "" ] || [ "$1" == " " ];
  then
    echo "CI suite $1 does not exist in path $root"
    echo
    echo "Allowed suites:"
    echo
    echo "$(cmd_ls)"
    echo
    usage
    exit 1
  fi

  for step in $steps; do
    if [ -x "$root/$step" ];
    then
      eval "$root/$step"
    fi
  done
}

case "$1" in
"run")
  cmd_run ${@:2}
  ;;
"ls")
  cmd_ls
  ;;
*)
  usage
  ;;
esac

