#!/bin/ksh
# $Id: ins_make 108 2007-07-26 10:18:31Z bellier $
#---------------------------------------------------------------------
#- Installation of Makefiles according to an environment
#---------------------------------------------------------------------
function ins_make_Usage
{
echo "
ins_make install the Makefile in the directories

Usage :
  ${b_n} [-h] [-v] [-d] [-p r] [-t h] [-w w] [-m m]

Options :
  -h   : help
  -v   : verbose mode
  -d   : debug mode
  -p p : int and real precision I4R4/I4R8/I8R8/4/8
  -t h : target host_name
  -w w : directory Where to install the Makefile (recursive)
  -m m : mpi context choosen for the coupling MPI1/MPI2

Defaults :
   -t local_host -p I4R8

Example for remote host compilation :
  ins_make -t sx8brodie
"
}
#-  dirname and  basename
d_n=${0%/*}; b_n=${0##*/};
#- Retrieving and validation of the options
x_v='silencious'; x_d='normal'; x_p="??"; x_t="??"; x_w="??"; x_m='??';
while getopts :hvdp:m:t:w: V
  do
   case ${V} in
    (h)  ins_make_Usage; exit 0;;
    (v)  x_v='verbose';;
    (d)  x_d='debug';;
    (p)  x_p=${OPTARG};;
    (t)  x_t=${OPTARG};;
    (w)  x_w=${OPTARG};;
    (m)  x_m=${OPTARG};;
    (:)  echo ${b_n}" : -"${OPTARG}" option : missing value" 1>&2;
         exit 2;;
    (\?) echo ${b_n}" : -"${OPTARG}" option : not supported" 1>&2;
         exit 2;;
   esac
  done
shift $(($OPTIND-1));
#-
# Treatment of the options
#-
# Default INTEGER and REAL precision
case ${x_p} in
 ( I4R4 | 4 )  x_p='I4R4'; w_ip='4'; w_rp='4';;
 ( I4R8 | ?? ) x_p='I4R8'; w_ip='4'; w_rp='8';;
 ( I8R8 | 8 )  x_p='I8R8'; w_ip='8'; w_rp='8';;
 (*)
   echo 'Invalid precision ' 1>&2;
   echo ${x_p}' (must be I4R4, I4R8, I8R8, 4 or 8)' 1>&2;
   exit 1;;
esac
#-
# Target_Host Name
[[ ${x_t} = '??' ]] && \
 { x_t=$(${d_n}/w_i_h);
   { [[ ${?} != 0 ]] && \
      { echo "Bad installation : w_i_h/uname unreachable" 1>&2;
        exit 1; }; } || \
     [[ ${x_t} = "Unknown" ]] && \
      { echo "Local_Host not supported" 1>&2; exit 1; }; }
#-
MKTG=${d_n}'/AA_make.gdef'
[[ ! -f "${MKTG}" ]] && { echo "${MKTG} unreachable ..."; exit 3; }
#-
W_XX=$(sed -n -e "s/^#-Q-  *\([^ ]*\).*$/\1/p" ${MKTG} | \
       sort -u | sed -e "/${x_t}/!d");
[[ '\?'"${W_XX}" != '\?'${x_t} ]] && \
  { echo "Target_Host  "${x_t}"  not supported" 1>&2; exit 1; }
#-
case ${x_m} in
 ( mpi1 | mpi2 | MPI1 | MPI2 | ?? );;
 (*) echo "MPI context "${x_m}" not supported" 1>&2; exit 1;;
esac
#-
[[ ${x_v} = 'verbose' ]] && \
 { echo "";
   echo '--- Mode         : '${x_v};
   echo '--- Compilation  : '${x_d};
   echo '--- Precision    : '${x_p};
   echo '--- Target       : '${x_t};
   echo '--- MPI context  : '${x_m}; }
#-
if [ '\?'${x_w} = '\?' ]; then
l_rep=$(find ${d_n}/.. -name AA_make -print)
else
l_rep=$(find ${d_n}/../*/${x_w} -name AA_make -print)
fi
#-
W_OS=${x_t};
echo " ";
echo "Installation of makefiles for $W_OS";
echo " ";
#-
for i in ${l_rep}
do
  j=${i%/AA_make};
  echo "Installation in ${j}";
  MKTL="${j}"'/AA_make.ldef';
  [[ ! -f "${MKTL}" ]] && { echo "${MKTL} unreachable ..."; exit 3; }
  MKTR="${j}"'/AA_make';
  cat ${MKTL} ${MKTG} ${MKTR} > mk1.$$;
  W_PR='#-Q- '; W_XX=${W_PR}${x_t};
  sed -e "/^${W_XX} */ s///" -e "/^${W_PR}/d" mk1.$$ > mk2.$$;
#-
# Default INTEGER and REAL precision
  W_PR='#-P- '; W_XX=${W_PR}${x_p};
  sed -e "/^${W_XX} */ s///" -e "/^${W_PR}/d" mk2.$$ > mk1.$$;
#-
# Handle the precision module "defprec.f90" in "IOIPSL/src"
  [[ '!!'${j##*/modeles} = '!!/IOIPSL/src' ]] && \
   { [[ ${x_v} = 'verbose' ]] && W_XX='-v' || W_XX="";
     ${d_n}/ins_m_prec ${W_XX} -i ${w_ip} -r ${w_rp}; r_c=${?};
     unset w_ip w_rp;
     [[ ${r_c} != 0 ]] && exit ${r_c}; }
#-
# Debug
  W_PR='#-D- ';
  [[ ${x_d} = 'debug' ]] && W_XX=${W_PR}'MD' || W_XX=${W_PR}'MN';
  sed -e "/^${W_XX} */ s///" -e "/^${W_PR}/d" mk1.$$ > mk2.$$;
#-
# Retrieve the path of the directory
  mk_dir=$(cd ${j};/bin/pwd;)
  W_XX='MAKE_DIR ='
# Update the value
  sed -e "s!^${W_XX}.*!${W_XX} ${mk_dir}!" mk2.$$ > mk1.$$
  unset mk_dir;
#-
# Choice of mpi library for the coupling
  W_XX='LIB_MPI ='; W_XX_BIS='LIB_MPI_BIS =';
  case ${x_m} in
   ( mpi1 | MPI1 )
    lib_mpi='MPI1'; lib_mpi_bis='MPI1';
    sed -e "s!^${W_XX}.*!${W_XX} ${lib_mpi}!" \
        -e "s!^${W_XX_BIS}.*!${W_XX_BIS} ${lib_mpi_bis}!" \
      mk1.$$ > ${j}/Makefile;;
   ( mpi2 | MPI2 )
    lib_mpi='MPI2'; lib_mpi_bis='';
    sed -e "s!^${W_XX}.*!${W_XX} ${lib_mpi}!" \
        -e "s!^${W_XX_BIS}.*!${W_XX_BIS} ${lib_mpi_bis}!" \
      mk1.$$ > ${j}/Makefile;;
   (??) 
    cp mk1.$$ ${j}/Makefile;;
   (*) ;;
  esac
  unset lib_mpi lib_mpi_bis;
done
#-
echo "";
unset W_XX W_XX_BIS; rm -f mk1.$$ mk2.$$;
#-
exit 0;
