#!/bin/sh -eu

: ${PLATFORM:=debian/stretch}
: ${BUILD_NUMBER:=$BUILDKITE_BUILD_NUMBER}

for EXECUTABLE in smith smith-host smith-whoami; do
    TARGET="target/release/$EXECUTABLE"
    SOURCE="$(cat "publish/$EXECUTABLE")"
    mkdir -p "$(dirname "$TARGET")"
    aws s3 cp "$SOURCE" "$TARGET"
done

docker build \
    -t "smithsecurity/smith-cli:$BUILD_NUMBER" \
    -t "smithsecurity/smith-cli:latest" \
    -f "Dockerfile" \
    .
DOCKER_USERNAME=$(./bin/fetch-secret "build/DOCKER_USERNAME")
DOCKER_PASSWORD=$(./bin/fetch-secret "build/DOCKER_PASSWORD")
docker login --username "$DOCKER_USERNAME" --password "$DOCKER_PASSWORD"
docker push "smithsecurity/smith-cli:$BUILD_NUMBER"
docker push "smithsecurity/smith-cli:latest"
