#!/bin/bash
# 20190829 Robert Martin-Legene
# This builds the docker bfanode on a Debian host system.

trap "exit 1" ERR
OK=0
TAG="$1"
while [ "$OK" = "0" ]
do
    case "$TAG" in
        "latest")
            DOCKERFILENAME=Dockerfile-${TAG}
            sed '
                s/BFANETWORKID=__BFANETWORKID__/BFANETWORKID=$BFANETWORKIDPROD/;
                s/BFANETWORKDIR=__BFANETWORKDIR__/BFANETWORKDIR=$BFANETWORKDIRPROD/;
                s/GITBRANCH=__GITBRANCH__/GITBRANCH=master/
                ' < Dockerfile > ${DOCKERFILENAME}
            OK=1
            ;;
        "test")
            DOCKERFILENAME=Dockerfile-${TAG}
            sed '
                s/BFANETWORKID=__BFANETWORKID__/BFANETWORKID=$BFANETWORKIDTEST/;
                s/BFANETWORKDIR=__BFANETWORKDIR__/BFANETWORKDIR=$BFANETWORKDIRTEST/;
                s/GITBRANCH=__GITBRANCH__/GITBRANCH=dev/
                ' < Dockerfile > ${DOCKERFILENAME}
            OK=1
            ;;
        *)
            read -p 'Build which tag (latest/test)? : ' TAG
            ;;
    esac
done
docker build --no-cache -f ${DOCKERFILENAME} -t bfaar/nodo:${TAG} .
rm Dockerfile-${TAG}
if [ "$USER" = "robert" ]
then
    read -p 'Upload? : ' -n 1
    echo
    test "$REPLY" = "y"                 || exit 0
    docker push bfaar/nodo:${TAG}       || ( echo "Did you remember to log in using 'docker login'?" ; false )
fi
