#!/usr/bin/env bash

: ${PLATFORMS=linux/amd64}
: ${CONTINUOUS_INTEGRATION=}
: ${DAILY_TARGETS=}

progressFlag=""
if [ "$CONTINUOUS_INTEGRATION" == "true" ]; then progressFlag="--progress=plain"; fi

usage() {
  echo "$0 (master|tag|daily) (tag|channel) <repo> [push]"
  exit 1
}

if [ $# != 4 ]; then
  usage
fi

parseTag() {
  local prefix=$(echo $1 | cut -d/ -f 1)
  if [[ "$prefix" != "dockerfile" ]]; then
    echo "invalid tag $1"
    exit 1
  fi
  local suffix=$(echo $1 | awk -F- '{print $NF}')
  local tagf=./frontend/dockerfile/release/$suffix/tags
  if [ "$sufffix" == "$1" ] || [ ! -f $tagf ]; then
    suffix="mainline"
  fi

  local mainTag=$(echo $1 | cut -d/ -f 2)

  publishedNames=$REPO:$mainTag

  local versioned=""
  # \d.\d.\d becomes latest
  if [[ "$mainTag" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    publishedNames=${publishedNames},$REPO:latest
    versioned=1
  fi

  # \d.\d.\d-channel becomes <channel>
  if [[ "$mainTag" =~ ^[0-9]+\.[0-9]+\.[0-9]+-$suffix$ ]] && [ -f $tagf ]; then
    publishedNames=${publishedNames},$REPO:$suffix
    versioned=1
  fi

  # \d.\d.\d* -> \d.\d* -> \d* (except "0")
  if [ "$versioned" == "1" ]; then
    publishedNames=${publishedNames},$REPO:$(echo $mainTag | sed -E 's#^([0-9]+\.[0-9]+)\.[0-9]+#\1#')
    if [ "$(echo $mainTag | sed -E 's#^([0-9]+)\.[0-9]+\.[0-9]+.*$#\1#')" != "0" ]; then
      publishedNames=${publishedNames},$REPO:$(echo $mainTag | sed -E 's#^([0-9]+)\.[0-9]+\.[0-9]+#\1#')
    fi
  fi

  TAG=$suffix
}

TYP=$1
TAG=$2
REPO=$3
PUSH=$4

pushFlag=""
if [ "$PUSH" = "push" ]; then
  pushFlag="push=true"
fi

case $TYP in
"master")
  tagf=./frontend/dockerfile/release/$TAG/tags
  if [ ! -f $tagf ]; then
    echo "invalid release $TAG"
    exit 1
  fi
  buildTags=$(cat $tagf)
  pushTag="master"
  if [ "$TAG" != "mainline" ]; then
    pushTag=${pushTag}-$TAG
  fi
  set -x
  buildctl build $progressFlag --frontend=dockerfile.v0 \
    --local context=. --local dockerfile=. \
    --opt filename=./frontend/dockerfile/cmd/dockerfile-frontend/Dockerfile \
    --opt platform=$PLATFORMS \
    --opt "build-arg:CHANNEL=$TAG" \
    --opt "build-arg:BUILDTAGS=$buildTags" \
    --output type=image,name=$REPO:$pushTag,$pushFlag
  ;;
"tag")
  publishedNames=""
  parseTag $TAG
  tagf=./frontend/dockerfile/release/$TAG/tags
  if [ ! -f $tagf ]; then
    echo "no build tags found for $TAG"
    exit 1
  fi
  buildTags=$(cat $tagf)

  set -x
  buildctl build $progressFlag --frontend=dockerfile.v0 \
    --local context=. --local dockerfile=. \
    --opt filename=./frontend/dockerfile/cmd/dockerfile-frontend/Dockerfile \
    --opt platform=$PLATFORMS \
    --opt "build-arg:CHANNEL=$TAG" \
    --opt "build-arg:BUILDTAGS=$buildTags" \
    --output type=image,\"name=$publishedNames\",$pushFlag
  ;;
"daily")
  if [ -z $DAILY_TARGETS ]; then
    DAILY_TARGETS="mounts secrets ssh"
  fi
  
  for TAG in $DAILY_TARGETS; do
  
    tagf=./frontend/dockerfile/release/$TAG/tags
    if [ ! -f $tagf ]; then
      echo "invalid release $TAG"
      exit 1
    fi
    buildTags=$(cat $tagf)
  
    # find the buildID of the last pushed image
    # returns a BuildID if rebuild needed
  
    tmp=$(mktemp -d -t buildid.XXXXXXXXXX)
    set -x
    dt=$(date +%Y%m%d)
    buildctl build $progressFlag --progress=plain --frontend=dockerfile.v0 \
      --local context=. --local dockerfile=. \
      --opt filename=./frontend/dockerfile/cmd/dockerfile-frontend/Dockerfile \
      --opt "build-arg:CHANNEL=$TAG" \
      --opt "build-arg:REPO=$REPO" \
      --opt "build-arg:DATE=$dt" \
      --opt "build-arg:BUILDTAGS=$buildTags" \
      --opt target=buildid\
      --output type=local,dest=$tmp
    
    if [ -f $tmp/buildid ]; then
      buildid=$(cat $tmp/buildid)
      echo "buildid: $buildid"
      
      buildctl build $progressFlag --frontend=dockerfile.v0 \
        --local context=. --local dockerfile=. \
        --opt filename=./frontend/dockerfile/cmd/dockerfile-frontend/Dockerfile \
        --opt platform=$PLATFORMS \
        --opt "build-arg:CHANNEL=$TAG" \
        --opt "build-arg:BUILDTAGS=$buildTags" \
        --output type=image,name=$REPO:$dt-$TAG,$pushFlag
      rm $tmp/buildid
    fi
    rm -r $tmp

  done
  
  ;;
esac
