#!/bin/bash

#REMOTE=northamerica-northeast2-docker.pkg.dev/dexorder-430504/dexorder
REMOTE=${REMOTE:-git.dxod.org/dexorder/dexorder}

if [ "$1" != "flink" ] && [ "$1" != "relay" ] && [ "$1" != "ingestor" ] && [ "$1" != "web" ] && [ "$1" != "gateway" ] && [ "$1" != "lifecycle-sidecar" ] && [ "$1" != "client-py" ]; then
  echo
  echo usage: "$0 "'{flink|relay|ingestor|web|gateway|lifecycle-sidecar|client-py} [''dev''] [config] [deployment] [kubernetes] [image_tag]'
  echo
  echo '      [''dev''] if the literal string ''dev'' is not the second argument, then the build refuses to run if source code is not checked in. Otherwise, the git revision numbers are used in the image tag.'
  echo
  echo '      [config] is used to find configuration files e.g. web/.env-{config} or backend/dexorder-{config}.toml.  Defaults to project name.'
  echo
  echo '      [deployment] refers to the set of contracts bundled in the image, from contract/deployment/{deployment}.  Defaults to config name.'
  echo
  echo '      [kubernetes] is used for the base image name and also to find the yaml file for deployment: deploy/k8s/{kubernetes}.yaml.  Defaults to project name.'
  echo
  echo '      [image_tag] will be used for the container image name. The standard tag will always be generated as well.'
  echo
  exit 1
else
  PROJECT=$1
  shift
fi

if [ "$1" == "dev" ]; then
  shift
  DEV=1
fi

if [ "$PROJECT" == "dev" ]; then
  DEV=1
#  NO_CACHE=--no-cache
fi

if [ "$DEV" == "1" ]; then
  TAG="dev`date +%Y%m%d%H%M%S`"
  if [ "$1" != "" ]; then
    CONFIG=$1
    shift
  else
    CONFIG=dev
  fi
else
  if [ "$1" != "" ]; then
    CONFIG=$1
    shift
  else
    CONFIG=production
  fi
  DIRTY="$( cd $PROJECT && git status | grep "Changes " )"
  if [ "$DIRTY" != "" ]; then
      echo $PROJECT has uncommited changes.
      echo
      echo Use \`$0 $PROJECT dev\` to deploy a development-tagged version instead.
      exit 1
  fi
  TAG="$( cd $PROJECT && git log --oneline | head -1 | cut -d ' ' -f 1 )"
fi

if [ "$1" != "" ]; then
  DEPLOYMENT=$1
  shift
else
  DEPLOYMENT=$CONFIG
fi

if [ "$1" != "" ]; then
  KUBERNETES=$1
  shift
else
  KUBERNETES=$PROJECT
fi

if [ "$1" != "" ]; then
  IMG_TAG=$1
else
  IMG_TAG=
fi

if [ $(basename "$0") == 'deploy' ]; then
  DEPLOY=1
else
  DEPLOY=0
fi

if [ "$DEPLOY" == "0" ]; then
  ACTION=Building
  #NO_CACHE=--no-cache
else
  ACTION=Making
fi


echo $ACTION $PROJECT config=$CONFIG deployment=$DEPLOYMENT '=>' $TAG

# Copy protobuf definitions into project directory for Docker build (if not gateway or lifecycle-sidecar)
# Using rsync --checksum so unchanged files keep their timestamps (preserves docker layer cache)
if [ "$PROJECT" != "lifecycle-sidecar" ]; then
  rsync -a --checksum --delete protobuf/ $PROJECT/protobuf/
fi

docker build $NO_CACHE -f $PROJECT/Dockerfile --build-arg="CONFIG=$CONFIG" --build-arg="DEPLOYMENT=$DEPLOYMENT" -t dexorder/ai-$PROJECT:latest $PROJECT || exit 1

# Cleanup is handled by trap
docker tag dexorder/ai-$PROJECT:latest dexorder/ai-$PROJECT:$TAG
docker tag dexorder/ai-$PROJECT:$TAG $REMOTE/ai-$PROJECT:$TAG
docker tag $REMOTE/ai-$PROJECT:$TAG $REMOTE/ai-$PROJECT:latest
if [ "$IMG_TAG" != "" ]; then
  docker tag dexorder/ai-$PROJECT:$TAG $REMOTE/ai-$PROJECT:$IMG_TAG
  TAG=$IMG_TAG
fi
echo "$(date)" built $REMOTE/ai-$PROJECT:$TAG

# Output just the tag for scripting purposes (to stderr so scripts can capture it)
echo "$TAG" >&2

if [ "$DEPLOY" == "1" ]; then
  docker push $REMOTE/ai-$PROJECT:$TAG
  YAML=$(sed "s#image: dexorder/ai-$PROJECT*#image: $REMOTE/ai-$PROJECT:$TAG#" deploy/k8s/$KUBERNETES.yaml)
  echo "$YAML" | kubectl apply -f - || echo "$YAML" "\nkubectl apply failed" && exit 1
  echo deployed $KUBERNETES.yaml $REMOTE/ai-$PROJECT:$TAG
fi
