#!/usr/bin/env bash

# (C) Copyright 2020- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

ECTRANS_VERSION_STR="1.6.0"
ECTRANS_VERSION="1.6.0"
ECTRANS_GIT_SHA1="2c4c818d79effe56d30bb2896866aba590a5fad8"

#################################################################
# Commands
#################################################################

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

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

append_git()
{
  if (( $# > b )); then
    git="$@"
    echo ", git-sha1 ${git::${#git}-33}"
  fi
}

info()
{
  echo "ectrans version (${ECTRANS_VERSION_STR})$(append_git ${ECTRANS_GIT_SHA1})"
  echo ""
  echo "Build:"
  echo "  build type      : Release"
  echo "  timestamp       : 20250418125020"
  echo "  op. system      : Linux-5.15.0-88-generic (linux.64)"
  echo "  processor       : x86_64"
  echo "  c compiler      : GNU 10.2.1"
  echo "    flags         :  -pipe -O3 -DNDEBUG"
  echo "  fortran compiler: GNU 10.2.1"
  echo "    flags         :  -fallow-argument-mismatch -O3 -DNDEBUG -funroll-all-loops -finline-functions"
  echo ""
  echo "Features:"
  echo "  MPI             : 0"
  echo "  OMP             : 1"
  echo "  MKL             : 0"
  echo "  FFTW            : "
  echo "  TRANSI          : 1"
  echo ""
  echo "Dependencies: "
  echo "  fiat version (1.3.0)$(append_git 6ddce6473d5aed25cb495a23a85186e7ecd822c0)"
  
}

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


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

if test $# -eq 0; then
    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
