#!/usr/bin/env bash
set -e

#docker login -u richard -p $DKREGISTRY_PASS $DKREGISTRY

build() {
  set -x
  local version=$1

  #build
  docker build --rm -f Dockerfile-${version} -t xaicorp/php:${version} .

  #test
  docker run --rm --entrypoint php xaicorp/php:${version} --version | grep ${version}
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep curl
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep pdo
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep pgsql
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep redis
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep mbstring
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep mysql
#  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep mcrypt
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep zip
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep dom
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep json
  docker run --rm --entrypoint php xaicorp/php:${version} -m | grep raphf
  docker run --rm -d --name test-php xaicorp/php:${version} \
    && sleep 10 \
    && docker logs test-php | grep "fpm is running"; \
    docker kill test-php
#  docker run --rm --entrypoint php xaicorp/php:${version} -i | grep conf.d/25-http.ini #fixme
}

build_dev() {
  local version=$1

  #build
  docker build --rm -f Dockerfile-${version}-dev -t xaicorp/php:${version}-dev .

  #test
  docker run --rm --entrypoint php xaicorp/php:${version}-dev --version | grep ${version}
  docker run --rm --entrypoint php xaicorp/php:${version}-dev --version | grep 'Xdebug'
  docker run --rm --entrypoint git xaicorp/php:${version}-dev --version | grep 'git version'
}

push() {
  local version=$1

  #register artifact
  docker tag xaicorp/php:${version} dkregistry.xai-corp.net:5000/xaicorp/php:${version}
  docker push dkregistry.xai-corp.net:5000/xaicorp/php:${version}

  #register artifact
  docker tag xaicorp/php:${version}-dev dkregistry.xai-corp.net:5000/xaicorp/php:${version}-dev
  docker push dkregistry.xai-corp.net:5000/xaicorp/php:${version}-dev
}

final() {
  code=$?

  echo
  if [ ${code} != 0 ]; then
    echo failed
    exit ${code}
  fi

  echo success
  exit 0
}
trap final EXIT

#build 7.0
#build_dev 7.0
#
#build 7.1
#build_dev 7.1

#build 7.2
#build_dev 7.2
#push 7.2

build 7.3
build_dev 7.3
push 7.3

exit
