#!/usr/bin/env bash
#
#
# About version.
# If on the exact tag version is git tag, functions are deployed to /functions/$tag path.
# If thare are commits after tag tag is apended with commit hash, functions are deployed to /dev/$tag.
# If repo is dirty username is added to the tag.
#
# Flags:
#   --only-cli                    just builds cli
#   --silent                      don't send slack notification

GIT_ROOT=$(git rev-parse --show-toplevel)

export MANTIL_NO_EVENTS=1

cd "$GIT_ROOT/cli"

cp ../../infrastructure/secrets/event-publisher.creds ./secret/
cp ../../infrastructure/secrets/event-listener.creds ./secret/
cp ../../infrastructure/secrets/logs-publisher.creds  ./secret/
cp ../../infrastructure/secrets/logs-listener.creds   ./secret/

# collect variables
tag=$(git describe --always)
# if we are exactly on the tag
on_tag=0; (git describe --exact-match > /dev/null 2>&1 && git diff --quiet) && { on_tag=1; }
mantil_bin="$(go env GOPATH)/bin/mantil"

echo "> Building cli with tag=$tag dev=$USER on_tag=$on_tag"
go build -o "$mantil_bin" -ldflags "-X github.com/mantil-io/mantil/domain.tag=$tag -X github.com/mantil-io/mantil/domain.dev=$USER -X github.com/mantil-io/mantil/domain.ontag=$on_tag" -trimpath

if [[ $* == *--only-cli* ]]; then
   exit 0
fi

# set BUCKET, BUCKET2, RELEASE env variables
$mantil_bin --version # ensure that mantil is in the path
eval "$(MANTIL_ENV=1 $mantil_bin)"

if [ -n "$RELEASE" ]; then
   echo "> Releasing new cli version to homebrew"
   cd "$GIT_ROOT"
   (export tag=$tag dev=$USER on_tag=$on_tag; goreleaser release --rm-dist)
   scripts/copy_release_to_latest.sh $tag
fi

deploy_function() {
    env GOOS=linux GOARCH=arm64 go build -o bootstrap -trimpath
    zip -j -y -q "$1.zip" bootstrap

    aws s3 cp --no-progress "$1.zip" "$BUCKET"
    if [ -n "$BUCKET2" ]; then
       aws s3 cp --no-progress "$1.zip" "$BUCKET2"
    fi
    rm "$1.zip"
}

echo "> Deploying functions to $BUCKET"
for d in $GIT_ROOT/node/functions/*; do
    func_name=$(basename $d)
    (cd $d && deploy_function $func_name)
done

if [[ $* == *--silent* ]]; then
   exit 0
fi
