#!/bin/sh
#set -e
#set -x

# disable CGO for cross-compiling
export CGO_ENABLED=0

#branch=$(git rev-parse --abbrev-ref HEAD)
commit=$(git rev-parse HEAD)
#tag=$(git describe --tags --abbrev=0)
buildtime=$(TZ=Australia/Sydney date +%Y-%m-%dT%T%z)
#git_version=$(git describe --tags --always --long --dirty)
package_name=api-tester

#echo "Current directory:"
#pwd

platforms=("windows/amd64" "linux/amd64" "darwin/amd64")

echo Building $package_name with git version: $git_version
for platform in "${platforms[@]}"
do
   platform_split=(${platform//\// })
   GOOS=${platform_split[0]}
   GOARCH=${platform_split[1]}
   output_name=$package_name'-'$GOOS'-'$GOARCH
   if [ $GOOS = "windows" ]; then
       output_name+='.exe'
   fi

   echo "build commences"
   env GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -ldflags="-X main.sha1ver=$commit -X main.buildTime=$buildtime" -o build/$output_name $package
   if [ $? -ne 0 ]; then
       echo 'An error has occurred! Aborting the script execution...'
       exit 1
   fi
   echo "build complete : $output_name"
   sha256sum build/$output_name > build/${output_name}_checksum.txt
   #ls -lah ./build/
done

ls -lah build

#export now=$(TZ=Australia/Sydney date '+%Y%m%d-%H%M%S')
#echo $now
#echo "build commences"
#go build -ldflags "-X main.sha1ver=`git rev-parse HEAD` -X main.buildTime=$now" -o api-tester .
#echo "build complete"
#sha256sum api-tester > api-tester_checksum.txt
#ls -lah