#!/bin/bash
set -x
export PATH=$PWD/../tools/FCM/bin:$PATH

compil_mode_defined="FALSE"
compil_mode="prod"

arch_defined="FALSE"
arch=""

while (($# > 0))
  do
  case $1 in
      "-h") cat <<fin
Usage :
makegcm [options] -m arch exec
[-h]                       : help
[-prod / -dev / -debug]    : compilation en mode production (default) / developpement / debug .
 -arch nom_arch            : nom de l\'architecture cible
fin
	  exit;;

      "-prod")
	  compil_mode="prod" ; shift ;;

      "-dev")
	  compil_mode="dev" ; shift ;;

      "-debug")
	  compil_mode="debug" ; shift ;;

      "-arch")
	  arch=$2 ; arch_defined="TRUE"; shift ; shift ;;

      *)
	  code="$1" ; shift ;;
  esac
done

if [[ "$arch_defined" == "TRUE" ]]
then
  rm -f arch.path
  rm -f arch.fcm
  ln -s arch/arch-${arch}.path ./arch.path
  ln -s arch/arch-${arch}.fcm  ./arch.fcm
  source arch.path
else
  echo "Veuillez definir une architecture cible"
  exit 1
fi

if [[ "$compil_mode" == "prod" ]]
then
  COMPIL_FFLAGS="%PROD_FFLAGS"
elif [[ "$compil_mode" == "dev" ]]
then
  COMPIL_FFLAGS="%DEV_FFLAGS"
elif [[ "$compil_mode" == "debug" ]]
then
  COMPIL_FFLAGS="%DEBUG_FFLAGS"
fi

rm -f config.fcm

echo "%COMPIL_FFLAGS $COMPIL_FFLAGS" >> config.fcm

./build
