#!/bin/sh -euf

set -euf

BASE="$(dirname $0)"

if [ $# -eq 0 ]; then
    VERSION="$(cockpit-bridge --version | sed -n 's/Version: \(.*\)/\1/p' || true)"
    if [ -z "$VERSION" ]; then
        echo "git-version-check: cockpit-bridge is not installed"
        exit 1
    fi
elif [ $# -eq 1 ]; then
    VERSION="$1"
else
	echo "usage: git-version-check VERSION" >&2
	exit 2
fi

OVERRIDE=/usr/share/cockpit/branding/default/login.html
TAG=$($BASE/git-version-gen $BASE/../.tarball)

if [ "${VERSION%.x}" = "${TAG%.x}" ]; then
    echo "git-version-check: installed cockpit matches sources: $VERSION"
    rm -f $OVERRIDE
else
    echo "git-version-check: installed cockpit does not match sources: $VERSION != $TAG"
    MESSAGE="The installed version of cockpit is incompatible with the sources"
    sed "s/@@message@@/$MESSAGE/g" "$BASE/fail.html" > $OVERRIDE
fi
