#!/usr/bin/env bash

set -e

if [ "$EUID" -ne 0 ]; then
    echo "This script uses functionality which requires root privileges"
    exit 1
fi

# Start the build with an empty ACI
acbuild --debug begin $1

# In the event of the script exiting, end the build
trap "{ export EXT=$?; sudo acbuild --debug end && exit $EXT; }" EXIT

# Name the ACI
acbuild --debug set-name coreos.com/dex

# Add a version label
acbuild --debug label add version $( ./scripts/git-version )

acbuild --debug run -- apk add --update ca-certificates 

acbuild --debug copy _output/bin/dex /usr/local/bin/dex

acbuild --debug port add www tcp 5556
acbuild --debug port add grcp tpc 5557

acbuild --debug set-exec -- /usr/local/bin/dex
acbuild --debug write --overwrite _output/images/dex.aci