#!/usr/bin/env bash

ECCODES_VERSION_STR="2.39.0"
ECCODES_MAJOR_VERSION=2
ECCODES_MINOR_VERSION=39
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       : "
  echo "  op. system      : Windows-6.2.9200 (windows.64)"
  echo "  processor       : AMD64"
  echo "  sources         : C:/M/B/src/eccodes-2.39.0-Source"
  echo "  c++ compiler    : Clang 19.1.7"
  # Add the most common build type
  if [ $build_type = "RelWithDebInfo" ]; then
    echo "    flags         : -O2 -g -DNDEBUG"
  else
    echo "    flags         : -march=nocona -msahf -mtune=generic -O2 -pipe -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong -Wp,-D__USE_MINGW_ANSI_STDIO=1 -pipe"
  fi
#  echo "  fortran compiler:  "
#  echo "    flags         : "
  echo ""
  echo "Features:"
  echo "  FORTRAN             : $(print_feature 0)"
  echo "  AEC                 : $(print_feature 1)"
  echo "  MEMFS               : $(print_feature 0)"
  echo "  ECCODES_THREADS     : $(print_feature 0)"
  echo "  ECCODES_OMP_THREADS : $(print_feature 0)"
  echo "  JPG                 : $(print_feature 1)"
  echo "  PNG                 : $(print_feature 1)"
  echo "  NETCDF              : $(print_feature 1)"
  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
