#!/bin/sh -eu

if [ "$(uname -s)" != Darwin ]; then
    echo >&2 "MacOS only build."
    exit 1
fi

VERSION="0.1.0"
BUILD="$(date "+%Y%m%d%H%M%S")"
STAMP="$VERSION-$BUILD-$(git log --pretty=format:%h -n 1)"
: ${PLATFORM:=macos/mojave}
: ${BRANCH:=$(git rev-parse --abbrev-ref HEAD)}
: ${BUILD_NUMBER:=$BUILD}

export AWS_DEFAULT_REGION=us-east-1

cargo build --release

mkdir -p "publish"
for EXECUTABLE in smith smith-host smith-whoami; do
    SOURCE="target/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
