#!/bin/bash -e

# Simple continuous integration script which is usefule locally and in
# a pipeline


project=NAME
dist=/tmp/$project
profile=/tmp/$project.tprof

case $1 in
    s|setup)
	# Install any tools needed during the other steps
	go install github.com/gregoryv/uncover/cmd/stamp@latest	
	go install github.com/gregoryv/uncover/cmd/uncover@latest
        ;;
    b|build)
	# Build artefacts
        mkdir -p $dist
	# todo...
        ;;
    t|test)
        # Run unit tests on the build artifacts
        go test -coverprofile $profile ./...
        ;;
    u|uncover)
	# Check coverage with minimum control
        uncover -min 90 $profile
        ;;
    d|deploy)
	# deploy to different hosts based on some environment setting

        # Guard against releasing untagged
        version=$($dist/COMMAND --version)
        if [[ "$version" == "unreleased" ]]; then
            echo "cannot deploy unreleased version to production"
            exit 0
        fi
	# todo...	
        ;;
    c|clean)
        rm -rf $dist
        ;;
    *)
        echo "Usage: $0 TARGET..."
	echo ""
	echo "Targets"
	echo ""	
	echo "  [s]etup"
	echo "  [b]uild"
        echo "  [t]est"
        echo "  [u]ncover"
        echo "  [d]eploy"
        echo "  [c]lean"
        echo ""
        echo "$0 build test uncover"
        echo "$0 b t u"
        exit 1
        ;;
esac

# Run next target if any
shift
[[ -z "$@" ]] && exit 0
$0 $@
