#!/usr/bin/env bash

FCKIT_VERSION_STR="@fckit_VERSION_STR@"
FCKIT_VERSION="@fckit_VERSION@"

FCKIT_MAJOR_VERSION=@fckit_VERSION_MAJOR@
FCKIT_MINOR_VERSION=@fckit_VERSION_MINOR@
FCKIT_PATCH_VERSION=@fckit_VERSION_PATCH@

FCKIT_GIT_SHA1="@fckit_GIT_SHA1@"

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

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

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

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


info()
{
  echo "fckit version (${fckit_VERSION}), git-sha1 ${fckit_GIT_SHA1}"
  echo ""
  echo "Build:"
  echo "  build type      : @CMAKE_BUILD_TYPE@"
  echo "  timestamp       : @EC_BUILD_TIMESTAMP@"
  echo "  op. system      : @CMAKE_SYSTEM@ (@EC_OS_NAME@.@EC_OS_BITS@)"
  echo "  processor       : @CMAKE_SYSTEM_PROCESSOR@"
  echo "  sources         : @PROJECT_SOURCE_DIR@"
  echo "  c++ compiler    : @CMAKE_CXX_COMPILER_ID@ @CMAKE_CXX_COMPILER_VERSION@"
  echo "    flags         : @EC_CXX_FLAGS@"
  echo "  fortran compiler: @CMAKE_Fortran_COMPILER_ID@ @CMAKE_Fortran_COMPILER_VERSION@"
  echo "    flags         : @EC_Fortran_FLAGS@"
  echo ""
  echo "Features:"
  echo "  MPI             : $(print_feature @eckit_HAVE_MPI@)"
  echo "  final           : $(print_feature @fckit_HAVE_FINAL@)"
  echo "  eckit           : $(print_feature @fckit_HAVE_ECKIT@)"
  echo ""
  echo "Dependencies: "
  if [ -n "@fckit_HAVE_ECKIT@" ]; then
  echo "  eckit version (@eckit_VERSION@), git-sha1 $(short_gitsha1 @eckit_GIT_SHA1@)"
  else
  echo "  None"
  fi
}

gitsha1()
{
  echo "${fckit_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
    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
