#!/bin/sh

set -e

status() {
   state=$1
   shift
   case "${state}" in
      RUN) color_number=6 ;;
      PASS) color_number=2 ;;
      FAIL) color_number=1 ;;
   esac
   color=
   reset=
   if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && command -v tput >/dev/null 2>&1; then
      color_count=$(tput colors 2>/dev/null || printf '0')
      case "${color_count}" in
         ''|*[!0-9]*) color_count=0 ;;
      esac
      if [ "${color_count}" -ge 8 ]; then
         color=$(tput setaf "${color_number}" 2>/dev/null || :)
         reset=$(tput sgr0 2>/dev/null || :)
      fi
   fi
   printf '%s[%s]%s %s\n' "${color}" "${state}" "${reset}" "$*"
}

VERSION=$(dpkg-query --show --showformat='${Version}' sentencepiece | cut -d- -f1)
status RUN "Import sentencepiece and check its version"
if PYVERSION=$(python3 -c 'import sentencepiece as spm; print("VERSION={}".format(spm.__version__))'); then
   :
else
   status FAIL "Could not import sentencepiece or read its version"
   exit 1
fi
if [ "VERSION=$VERSION" = "${PYVERSION}" ]; then
   status PASS "sentencepiece Python module version ${VERSION}"
else
   status FAIL "Expected version ${VERSION}, got <${PYVERSION}>"
   exit 1
fi
