#!/bin/bash

set -euo pipefail

echo "Checking for a newer version"
mkdir -p ${DEVP_BIN_PATH}
git archive --remote git@git.mws-team.ru:mws/devp/platform-go-tools.git HEAD latest.txt -o ${DEVP_BIN_PATH}/latest.tar
$(cd ${DEVP_BIN_PATH} && tar xf latest.tar && rm latest.tar)

VERSION=$(cat ${DEVP_BIN_PATH}/latest.txt | xargs)
rm ${DEVP_BIN_PATH}/latest.txt

if [[ "$DEVP_VERSION" == "$VERSION" ]]; then
    echo "Up to date $VERSION"
    exit 0
fi

GOBIN=${DEVP_BIN_PATH} go install git.mws-team.ru/mws/devp/platform-go-tools/cmd/devp@v${VERSION}

EXE=${DEVP_BIN_PATH}/devp-$(echo ${VERSION} | tr . -)

mv ${DEVP_BIN_PATH}/devp ${EXE}
${EXE} golang install --skip-defaults
