#!/bin/bash

BASE_DIR=$(cd `dirname "$0"`;pwd)
DIST_DIR="$BASE_DIR/../dist/qznn"
SRC_DIR="$BASE_DIR/.."

echo $SRC_DIR

if test -z "$BUILD_NUMBER"  
then
  echo "build number not set"
  BUILD_NUMBER=2211
fi

rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"

githash=`git rev-parse HEAD`
version=1.0.$BUILD_NUMBER.${githash:0:5}
echo $version

cd $SRC_DIR

export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64

go build -tags="jsoniter" -ldflags "-s -w" -trimpath -o $DIST_DIR/dist/qznn cmd/qznn/qznn.go || exit $?

mkdir -p "$DIST_DIR/conf/"
mkdir -p "$DIST_DIR/dist/"

echo $version > $DIST_DIR/version.txt
cp -r $BASE_DIR/Dockerfile $DIST_DIR/Dockerfile
cp -r $SRC_DIR/assets $DIST_DIR/
cp -r $SRC_DIR/configs/qznn/* $DIST_DIR/conf/

cd $DIST_DIR
docker build -t harbor.daxia.dev/qznn-server:$version . || exit $?
docker tag harbor.daxia.dev/qznn-server:$version harbor.daxia.dev/qznn-server:latest || exit $?

docker push harbor.daxia.dev/qznn-server:$version || exit $?
docker push harbor.daxia.dev/qznn-server:latest || exit $?
