#!/usr/bin/env bash

FCKIT_VERSION_STR="0.14.2"
FCKIT_VERSION="0.14.2"

FCKIT_MAJOR_VERSION=0
FCKIT_MINOR_VERSION=14
FCKIT_PATCH_VERSION=2

FCKIT_GIT_SHA1=""

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

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

version()
{
  echo "0.14.2"
}

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 (0.14.2), git-sha1 "
  echo ""
  echo "Build:"
  echo "  build type      : Release"
  echo "  op. system      : Linux (linux.64)"
  echo "  processor       : x86_64"
  echo "  c++ compiler    : GNU 15.2.0"
  echo "  fortran compiler: GNU 15.2.0"
  echo ""
  echo "Features:"
  echo "  MPI             : $(print_feature 1)"
  echo "  final           : $(print_feature 1)"
  echo "  eckit           : $(print_feature 1)"
  echo ""
  echo "Dependencies: "
  if [ -n "1" ]; then
  echo "  eckit version (2.0.2), git-sha1 $(short_gitsha1 )"
  else
  echo "  None"
  fi
}

gitsha1()
{
  echo ""
}

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
