#!/usr/bin/env bash

# disable CGO for cross-compiling
export CGO_ENABLED=0

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=vctp

#platforms=("linux/amd64" "darwin/amd64")
platforms=("linux/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

   starttime=$(TZ=Australia/Sydney date +%Y-%m-%dT%T%z)
   echo "build commences at $starttime"
   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 at $buildtime : $output_name"
   sha256sum build/$output_name > build/${output_name}_checksum.txt
done

ls -lah build
