#!/bin/bash
#
# you can choose your OS and Architecture as follows:
# ./build.sh --os=windows --arch=amd64
#
# or you can use --help to check a list of platforms and options
#
if ! which go &>/dev/null; then
    echo "Go is not installed or is not in \$PATH"
    echo "giving up..."
    exit 1
fi
unset GOBIN
BIN_NAME=acme-downloader
PATH=$PATH:$(go env GOPATH)/bin
GOPATH=$(go env GOPATH)
BUILDTIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
REPO_DIR=$(dirname $0)
if [[ "$REPO_DIR" = '.' ]]; then
    REPO_DIR=$(pwd)
fi
test -L "${GOPATH}/src/${REPO_DIR}" || ln -sf "$REPO_DIR" "${GOPATH}/src/"
REPO_NAME=$(basename "$REPO_DIR")
cd "${GOPATH}/src/$REPO_NAME"

export PATH GOPATH

usage() {
    echo "Usage: $(basename $0)"
    echo "    -h | --help [Print this help and exit]"
    echo "    --os=OS     (Compile binary for this OS)"
    echo "    --arch=ARCH (Compile binary for this Architecture)"
    echo "    --upx       (Enable UPX compression)"
    echo "    --no-upx    (Disable UPX compression)"
    echo "    --version   (Version number. If not used it will be the latest tag)"
    echo ""
    echo "    Below is a list of supported OS/Arch combinations:"
    echo "OS         ARCH"
    go tool dist list | column -t -s '/' --table-columns =======,=======
    exit
}

OPTS=$(getopt -o "h" --longoptions "help,os:,arch:,upx,no-upx,version:" -- "$@")
eval set -- "$OPTS"

while true; do
    case "$1" in
    -h | --help)
        usage
        ;;
    --os)
        shift
        OS="$1"
        ;;
    --arch)
        shift
        ARCH="$1"
        ;;
    --upx)
        UPX='UPX'
        ;;
    --no-upx)
        NOUPX='NOUPX'
        ;;
    --version)
        shift
        PROG_VERSION="$1"
        ;;
    --)
        shift
        break
        ;;
    esac
    shift
done

if [[ -n $PROG_VERSION ]]; then
    PROG_VERSION=$(echo $PROG_VERSION | tr -d v)
else
    PROG_VERSION=$(git describe --tags $(git rev-list --tags --max-count=1) | tr -d v)
fi

if [[ -z $OS ]] || [[ -z $ARCH ]]; then
    echo -e "\nYou need to supply OS and Architecture\n"
    usage
fi

if [[ "$OS" == "windows" ]]; then
    BIN_NAME="${BIN_NAME}.exe"
fi

# env GOOS=$OS GOARCH=$ARCH go build -ldflags "-s -w -X main.appVersion=${PROG_VERSION} -X main.buildTime=${BUILDTIME}" .
env GOOS=$OS GOARCH=$ARCH go get -ldflags "-s -w -X main.appVersion=${PROG_VERSION} -X main.buildTime=${BUILDTIME}" .
if [ $? -gt 0 ]; then
    echo -e "\nthere was an error while compiling the code\n"
    exit
fi

EXECUTABLE_PATH="${GOPATH}/bin/${OS}_${ARCH}/${BIN_NAME}"
[ -f $EXECUTABLE_PATH ] || EXECUTABLE_PATH="${GOPATH}/bin/${BIN_NAME}"

run_upx() {
    if ! which upx &>/dev/null; then
        echo "please download upx here: https://github.com/upx/upx/releases"
        echo "and store the executable within your \$PATH"
        exit
    fi
    upx --brute "$EXECUTABLE_PATH"
}

if [[ "$UPX" == 'UPX' ]]; then
    run_upx
elif [[ "$NOUPX" == 'NOUPX' ]]; then
    true
else
    while true; do
        echo -e "\nUPX degrades performances but in the case of acme-downloader it is not noticeable"
        echo -e "UPX does not work on some OS/Arch combination (I haven't tried them all)\n"
        read -p "Do you wish to run upx against ${BIN_NAME}? (y/n) " yn
        case $yn in
        [Yy]*)
            echo ""
            run_upx
            break
            ;;
        [Nn]*) break ;;
        *)
            echo "Please answer Y|y or N|n."
            ;;
        esac
    done
fi

cp $EXECUTABLE_PATH .

echo -e "\nthe binary was compiled and it is avilable as:\n - \"$EXECUTABLE_PATH\"\n"