#!/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="@ectrans_VERSION_STR@"
ECTRANS_VERSION="@ectrans_VERSION@"
ECTRANS_GIT_SHA1="@ectrans_GIT_SHA1@"

#################################################################
# 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      : @CMAKE_BUILD_TYPE@"
  echo "  timestamp       : @BUILD_TIMESTAMP@"
  echo "  op. system      : @CMAKE_SYSTEM_NAME@ (@EC_OS_NAME@.@EC_OS_BITS@)"
  echo "  processor       : @CMAKE_SYSTEM_PROCESSOR@"
  echo "  c compiler      : @CMAKE_C_COMPILER_ID@ @CMAKE_C_COMPILER_VERSION@"
  echo "    flags         : @EC_C_FLAGS@"
  echo "  fortran compiler: @CMAKE_Fortran_COMPILER_ID@ @CMAKE_Fortran_COMPILER_VERSION@"
  echo "    flags         : @EC_Fortran_FLAGS@"
  echo ""
  echo "Features:"
  echo "  MPI             : @ectrans_HAVE_MPI@"
  echo "  OMP             : @ectrans_HAVE_OMP@"
  echo "  MKL             : @ectrans_HAVE_MKL@"
  echo "  FFTW            : @ectrans_HAVE_FFTW@"
  echo "  TRANSI          : @ectrans_HAVE_TRANSI@"
  echo ""
  echo "Dependencies: "
  echo "  fiat version (@fiat_VERSION_STR@)$(append_git @fiat_GIT_SHA1@)"
  
}

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
