#!/bin/sh
#
# this script calls the precompiler for expanding the lists of variables. 
#
# This script is justified because there are some differences between
# the precompiler and the sed command in MacOS and Linux. 
# This script tries to manage these dependencies in a consistent way
#
# Usage: ex: Makefile_use_precompiler shadow_variables
#            will process the shadow_variables_precpp.F90 file and
#            create           shadow_variables.f90
#
# Note that cpp delivered in MacOS did not work for me, I call
# cpp-<version>, in my case cpp-4.0 or greater
#
# srio@esrf.eu 2011-06-21 fete de la musique.
#
echo "+++++ "
echo "----- Using script $0...."
echo "----- "
echo "-----Called: $0 $1"
if [ "$1" = "" ]; then 
  echo "$0: Error" ; exit 1
#else
#  echo "$0: OK."
fi



# step 1


if [ `uname` = "Darwin" ]; then
  echo "----- "
  echo "----- We are using Darwin"
  echo "----- "
  echo "-----Running: cpp-4.0 -w -C -I. $1_precpp.F90 > tmp1_$1.f90  "
  cpp-4.0 -w -C -I. $1_precpp.F90 > tmp1_$1.f90  
else
  echo "----- "
  echo "----- We are NOT using Darwin but `uname`"
  echo "----- "
  echo "-----Running: cpp -w -C -I. $1_precpp.F90 > tmp1_$1.f90  "
  cpp -w -C -I. $1_precpp.F90 > tmp1_$1.f90  
  echo "----- "
fi



# step 2 (system dependent)
if [ `uname` = "Darwin" ]; then
  echo "-----Running: sed -e 's/newline/\'$'\n/g' < tmp1_$1.f90 > tmp2_$1.f90" 
  sed -e 's/newline/\'$'\n/g' < tmp1_$1.f90 > tmp2_$1.f90
else
  echo "-----Running: sed 's/newline/\n/g' < tmp1_$1.f90 > tmp2_$1.f90 "
  sed -e 's/newline/\n/g' < tmp1_$1.f90 > tmp2_$1.f90
fi
echo "----- "

# step 3
echo "-----Running: sed 's/^#/!#/' < tmp2_$1.f90 > $1.f90"
sed 's/^#/!#/' < tmp2_$1.f90 > $1.f90
echo "+++++ "

exit 0
