#!/usr/bin/env bash

ECCODES_VERSION_STR="2.44.0"
ECCODES_MAJOR_VERSION=2
ECCODES_MINOR_VERSION=44
ECCODES_PATCH_VERSION=0
# ECCODES_VERSION="$ECCODES_MAJOR_VERSION.$ECCODES_MINOR_VERSION.$ECCODES_PATCH_VERSION"

ECCODES_GIT_SHA1=""
if [ "x$ECCODES_GIT_SHA1" = "x" ]; then
  ECCODES_GIT_SHA1="unknown"
fi

#################################################################
# Commands
#################################################################
usage()
{
  echo "Usage: $0 [--version] [--info] [--git]"
  exit $1
}

version()
{
  echo "${ECCODES_VERSION_STR}"
}

print_feature()
{
  if [ -z "$1" ]; then
    echo "OFF"
  elif [[ $1 =~ (true|TRUE|ON|1) ]]; then
    echo "ON"
  else
    echo "OFF"
  fi
}

build_type="Release"
info()
{
  echo "ecCodes version ${ECCODES_VERSION_STR}, git-sha1 ${ECCODES_GIT_SHA1}"
  echo ""
  echo "Build:"
  echo "  build type      : Release"
  echo "  timestamp       : 20260904173045"
  echo "  op. system      : OpenBSD-8.0 (openbsd.64)"
  echo "  processor       : aarch64"
  echo "  sources         : /usr/obj/ports/eccodes-2.44.0/eccodes-2.44.0-Source"
  echo "  c++ compiler    : Clang 22.1.6"
  # Add the most common build type
  if [ $build_type = "RelWithDebInfo" ]; then
    echo "    flags         : -O2 -g -DNDEBUG"
  else
    echo "    flags         : -O2 -pipe -pipe"
  fi
#  echo "  fortran compiler: GNU 16.1.0"
#  echo "    flags         : "
  echo ""
  echo "Features:"
  echo "  FORTRAN             : $(print_feature 1)"
  echo "  AEC                 : $(print_feature 1)"
  echo "  MEMFS               : $(print_feature 1)"
  echo "  ECCODES_THREADS     : $(print_feature 0)"
  echo "  ECCODES_OMP_THREADS : $(print_feature 0)"
  echo "  JPG                 : $(print_feature 1)"
  echo "  PNG                 : $(print_feature 0)"
  echo "  NETCDF              : $(print_feature 1)"
  echo "  ECKIT_GEO           : $(print_feature 0)"
  echo ""
  # echo "Dependencies: "
  # if [ -n "" ]; then
  #  echo "  eckit version (), git-sha1 $(short_gitsha1 )"
  # else
  #  echo "  None"
  # fi
}

gitsha1()
{
  echo "${ECCODES_GIT_SHA1}"
}

short_gitsha1()
{
  if [ -z "$1" ]; then
    echo "unknown"
  else
    echo $1 | head -c 13
  fi
}

#################################################################
# Parse command-line
#################################################################

if test $# -eq 0; then
    info
    # usage 1
fi

while test $# -gt 0; do

    # Split --option=value in $opt="--option" and $val="value"

    opt=""
    val=""

    case "$1" in
    --*=*)
      opt=`echo "$1" | sed 's/=.*//'`
      val=`echo "$1" | sed 's/--[_a-zA-Z0-9]*=//'`
      ;;
    --*)
      opt=$1
      ;;
    *)
      break
      ;;
    esac

    # Parse options
    case "$opt" in
      --version)
        version
        ;;
      --git)
        gitsha1
          ;;
      --info)
        info
        ;;
      --)
        shift
        break
        ;;
      *)
        echo "unknown option: $opt"
        usage 1
        ;;
    esac
    shift
done
