#!/bin/bash

export ROOT=$PWD
CMD_LINE="$0 $*"

full_defined="FALSE"
arch_defined="FALSE"
job=1

CPP_KEY="CPP_NONE"
ICOSA_LIB=""

while (($# > 0))
  do
  case $1 in
      "-h") cat <<EOF
Usage :
setup [options] -arch nom_arch
[-h]                       : help
 -arch nom_arch            : nom de l\'architecture cible
EOF
          exit;;

      "-arch")
          arch=$2 ; arch_defined="TRUE"; shift ; shift ;;
      "-job")
          job=$2 ; shift ; shift;;
      "-full")
          full_defined="TRUE" ; shift ;;
      *)
	  echo "Unknown argument : $*"
	  echo "Try $0 -h"
	  exit 1
  esac
done

if [[ "$arch_defined" == "TRUE" ]]
then
  cat <<EOF >current_args
# This file was generated by running :
# $CMD_LINE
ROOT="$ROOT"
arch="$arch"
job="$job"
EOF

  echo "Setup saved in file current_args :"
  cat current_args
  # create separate build dirs sharing the same src/ and tools/ subdirs to save disk and time
  BUILD=$ROOT/DYNAMICO_serial
  echo "Creating build directory $BUILD"
  rm -rf $BUILD
  mkdir -p $BUILD
  cd $ROOT/DYNAMICO
  for NAME in arch build compile make_icosa src bld.cfg  clean doc tools ; do
      ln -s $ROOT/DYNAMICO/$NAME $BUILD/
  done
  for NAME in mpi mpi_omp ; do
      echo "Creating build directory $ROOT/DYNAMICO_$NAME"
      rm -rf $ROOT/DYNAMICO_$NAME
      cp -pr $BUILD $ROOT/DYNAMICO_$NAME
  done
  
  echo "You may now run ./build.sh"
else
  echo "Please define a target architecture"
  exit 1
fi
