#!/bin/bash

PWD=$(pwd)
DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
APP_NAME=$(basename $PWD)

cd code
RELEASE=$(git rev-parse --verify HEAD)
INSTANCE_NAME=$(git rev-parse --abbrev-ref HEAD)
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

usage() {
    cat <<EOF
Usage: $0 [-a|--app-name <name>] [-i|--instance-name <name>]

    Build the Docker container.

    -a | --app-name <name>
        The name of the app. Defaults to the name of the project directory

    -i | --instance-name <name>
        The name of the test instance. Defaults to the branch name

    -h | --help
        This help message
EOF
    exit 1;
}

while (( "$#" )); do
  [[ $1 == --*=* ]] && set -- "${1%%=*}" "${1#*=}" "${@:2}"
  case "$1" in
    -a|--app-name)
      APP_NAME=$2
      shift 2
      ;;
    -i|--instance-name)
      INSTANCE_NAME=$2
      shift 2
      ;;
    -h|--help)
      usage
      ;;
    --) # end argument parsing
      shift
      break
      ;;
    -*|--*=) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      usage
      ;;
  esac
done

echo "Building container with variables:"
echo "  APP_NAME: $APP_NAME"
echo "  INSTANCE_NAME: $INSTANCE_NAME"
echo "  BRANCH_NAME: $BRANCH_NAME"
echo "  RELEASE: $RELEASE"

# Docker command automatically inserted here
