#!/bin/sh -eu

VERSION="0.1.0"
STAMP="$VERSION-$(date "+%Y%m%d%H%M%S")-$(git log --pretty=format:%h -n 1)"
: ${PLATFORM:=musl/static}
: ${BRANCH:=$BUILDKITE_BRANCH}

export AWS_DEFAULT_REGION=us-east-1

rm rust-toolchain
docker run -v $PWD:/volume --rm -t clux/muslrust cargo build --release

mkdir -p "publish"
for EXECUTABLE in smith smith-host smith-whoami; do
    SOURCE="target/x86_64-unknown-linux-musl/release/$EXECUTABLE"
    TARGET="s3://smith-artefacts/executables/$BRANCH/$EXECUTABLE/$PLATFORM/$VERSION/$STAMP/$EXECUTABLE-$STAMP"
    MARKER="s3://smith-artefacts/executables/$BRANCH/$EXECUTABLE/$PLATFORM/latest"
    echo "$TARGET" > latest;
    cp latest "publish/$EXECUTABLE"
    aws s3 cp "$SOURCE" "$TARGET"
    aws s3 cp latest "$MARKER"
done
