#!/bin/bash

# This file is part of MEGAlib.
# Copyright (C) by Andreas Zoglauer.
#
# Please see the MEGAlib software license and documentation for more informations.


COMPILER=$1
if ( [[ ${COMPILER} != g++ ]] && [[ ${COMPILER} != clang++ ]] && [[ ${COMPILER} != c++ ]] && [[ ${COMPILER} != icc ]] ); then
  echo "ERROR: You must give an acceptable compiler to this script, e.g. g++, clang++, c++, or icc"
  exit 1;
fi

type xcode-select >/dev/null 2>&1
if [ $? -eq 0 ]; then
  # Check if git is here:
  COMPILEREXE=`xcode-select -p`/usr/bin/${COMPILER}
  if [ ! -f ${COMPILEREXE} ]; then
    echo "ERROR: You seem to be on macOS but the command line tools have not been installed completely"
    echo "       Please execute \"xcode-select --install\", open a new terminal, and try again to run this script"
    exit 1;
  fi
fi



# Allowed versions
GCCMIN=40700
CLANGMIN=30300
APPLECLANGMIN=60000
ICCMIN=1500

OS=`uname -s`
OS=`echo ${OS} | tr '[:upper:]' '[:lower:]'`

CODE="
#include <iostream>
using namespace std;

int main()
{
  int Version = 0;
#ifdef __clang__
  Version = __clang_patchlevel__;
  Version += 100*__clang_minor__;
  Version += 10000*__clang_major__;
  cout<<Version<<endl;
  return 0;
#endif

#ifdef __ICC
  Version = __ICC;
  cout<<Version<<endl;
  return 0;
#endif

#ifdef __GNUC__
  Version = __GNUC_PATCHLEVEL__;
  Version += 100*__GNUC_MINOR__;
  Version += 10000*__GNUC__;
  cout<<Version<<endl;
  return 0;
#endif

  return 1;
}
"

#echo "${CODE}"

echo "${CODE}" | ${COMPILER} -o compilertest -x c++ -
STATUS=""
if [[ -f compilertest ]]; then
  STATUS=`./compilertest`
  rm -f compilertest
else
  echo " "
  echo "ERROR: I could not compile the test program"
  exit 1;
fi

#echo ${STATUS}

if [[ ${COMPILER} == g* ]]; then
  if [[ ${STATUS} -ge ${GCCMIN} ]]; then
    exit 0;
  else
    MAJOR=$(( GCCMIN / 10000 ))
    MINOR=$(( (GCCMIN - 10000*MAJOR) / 100 ))
    PATCH=$(( (GCCMIN - 10000*MAJOR - 100*MINOR) ))
    echo " "
    echo "ERROR: You need at least g++ version ${MAJOR}.${MINOR}.${PATCH} for MEGAlib"
    exit 1;
  fi
elif  [[ ${COMPILER} == c* ]]; then
  if [[ ${OS} == linux ]]; then
    if [[ ${STATUS} -ge ${CLANGMIN} ]]; then
      exit 0;
    else
      MAJOR=$(( CLANGMIN / 10000 ))
      MINOR=$(( (CLANGMIN - 10000*MAJOR) / 100 ))
      PATCH=$(( (CLANGMIN - 10000*MAJOR - 100*MINOR) ))
      echo " "
      echo "ERROR: You need at least clang++ version ${MAJOR}.${MINOR}.${PATCH} for MEGAlib on Linux"
      exit 1;
    fi
  elif [[ ${OS} == darwin ]]; then
    if [[ ${STATUS} -ge ${APPLECLANGMIN} ]]; then
      exit 0;
    else
      MAJOR=$(( APPLECLANGMIN / 10000 ))
      MINOR=$(( (APPLECLANGMIN - 10000*MAJOR) / 100 ))
      PATCH=$(( (APPLECLANGMIN - 10000*MAJOR - 100*MINOR) ))
      echo " "
      echo "ERROR: You need at least Apple compiler version ${MAJOR}.${MINOR}.${PATCH} for MEGAlib on a Mac"
      exit 1;
    fi
  fi
elif  [[ ${COMPILER} == i* ]]; then
  if [[ ${STATUS} -ge ${ICCMIN} ]]; then
    exit 0;
  else
    echo " "
    echo "ERROR: You need at least icc ${ICCMIN} for MEGAlib"
    exit 1;
  fi
fi

echo " "
echo "ERROR: I was unable to determine the compiler you are using"

exit 1
