######################################################################
#
# Installation script for Code_Aster : utilities
#
######################################################################

# $Id: utils.sh,v 1.8 2003/11/05 15:28:13 mcourtoi Exp $
# $Name: v7_3-8 $

######################################################################
function CLEAR
{
   echo " "
   echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
   echo " "
}

######################################################################
# confirm txt indir defaut
#     pour accepter ou rejeter "indir"
# txt    : texte, prompt, question
# indir  : valeur en entree
# defaut : y ou n
# result : resultat (valeur si confirmation, "" sinon)
# RETOUR : 0 si confirmation, 1 si pas confirme
function confirm
{
   if [ $# -ne 3 ]; then
      echo "Wrong arguments for confirm"
      exit 1
   fi
   txt=$1
   indir=$2
   def=$3
   result=""
   echo " $txt (y/n, q=quit, default $def) ? "
	read rtn
   if [ -n "$rtn" ] && [ $rtn = "q" -o $rtn = "Q" ]; then
      echo "Installation aborted !"
      exit 1
   fi
   if [ $def = "y" ]; then
	   if [ -z "$rtn" ] || [ $rtn != "n" -a $rtn != "N" ]; then
         result=$indir
         return 0
	   fi
      return 1
   else
	   if [ -z "$rtn" ] || [ $rtn != "y" -a $rtn != "Y" ]; then
         return 1
	   fi
      result=$indir
      return 0
   fi
}

######################################################################
# compdir inidir
#     retourne le chemin absolu pour acceder au repertoire inidir
# indir   : chemin en entree (absolu ou relatif)
# fulldir : chemin complet
# RETOUR  : 0
function compdir
{
   if [ $# -ne 1 ]; then
      echo "Wrong arguments for confirm"
      exit 1
   fi
   inidir=$1
   dir0=`pwd`
   cd $inidir
   fulldir=`pwd`
   cd $dir0
   return 0
}

######################################################################
# search_which objw suppw [stop]
#     essai de trouver le programme avec which
# objw   : executable
# suppw  : chaine a retirer du resultat
# stop   : si "y", accepte si trouve, s'arrete si non trouve
#          si "a", accepte automatiquement dès que trouve
# result : chemin resultat
# RETOUR : 0 si trouve et confirme, 1 si pas trouve ou pas confirme
function search_which
{
   if [ $# -lt 2 -o $# -gt 3 ]; then
      echo "Wrong arguments for search_which"
      exit 1
   fi
   objw=$1
   suppw=$2
   stop="n"
   if [ $# -eq 3 ]; then
      stop=$3
   fi
   if [ -z $"suppw" ]; then
      suppw="@"
   fi
   search=`which $objw 2> /dev/null | grep -v "no *$objw" | grep -v "not in"`
   result=""
   if [ -n "$search" ]; then
      result=`echo $search | sed -e "s#$suppw\\$##"`
      if [ $stop = "y" ]; then
         return 0
      fi
      echo " $objw found : $search"
      if [ $stop = "a" ]; then
         return 0
      fi
      confirm "Accept" $result "y"
      return $?
   fi
   if [ $stop = "y" ]; then
      echo "$objw not found in PATH."
      echo "Installation aborted !"
      exit 1
   fi
   return 2
}

######################################################################
# search_find objt from suppf errf
#     essai de trouver le fichier avec find
# objt   : binaire ou lib ou include...
# from   : a partir d'ou
# suppf : chaine a retirer du resultat
# errf   : n on arrete de chercher a la premiere tentative
# result : chemin resultat (par confirm)
# RETOUR : 0 si trouve et confirme, 1 si pas trouve ou pas confirme
function search_find
{
   if [ $# -ne 4 ]; then
      echo "Wrong arguments for search_find"
      exit 1
   fi
   objt=$1
   from=$2
   suppf=$3
   errf=$4
   if [ -z $"suppf" ]; then
      suppf="@"
   fi

   if [ -z "$from" ]; then
      echo " Search for $objt, from (ex: /, /usr or /usr/lib... or c=cancel) ? "
      read from
      if [ -n "$from" ] && [ $from = "c" -o $from = "C" ]; then
         return 4
      fi
      search_find "$objt" "$from" "$suppf" "$errf"
      return $?
   fi

   echo " Searching $objt from $from, please wait..."
   search=`find $from -follow -name $objt \( -type f -o -type l \) 2> /dev/null | head -1`
   result=""
   if [ -n "$search" ]; then
      echo " $objt found : $search"
      result=`echo $search | sed -e "s#$suppf\\$##"`
      if [ $errf != "n" ]; then
         confirm "Accept" $result "y"
         iret=$?
         echo " "
         if [ $iret -eq 1 ]; then
            from=""
            search_find "$objt" "$from" "$suppf" "$errf"
            iret=$?
         fi
         return $iret
      else
         echo " "
         return 0
      fi
   else
      if [ $errf != "n" ]; then
         echo " $objt not found, try another location."
         echo " "
         from=""
         search_find "$objt" "$from" "$suppf" "$errf"
         return $?
      else 
         echo " $objt not found"
         echo " "
         return 1
      fi
   fi
}

######################################################################
# search_bin qbin from suppb errb
#     cherche un executable avec search_which puis search_find
# qbin   : binaire
# from   : a partir d'ou
# suppb : chaine a retirer du resultat
# errb   : y si on arrete en cas d'erreur
# result : chemin resultat (par confirm)
# RETOUR : 0 si binaire trouve et confirme, 1 si pas trouve ou pas confirme
function search_bin
{
   if [ $# -ne 4 ]; then
      echo "Wrong arguments for search_bin"
      exit 1
   fi
   qbin=$1
   from=$2
   suppb=$3
   errb=$4
   search_which "$qbin" "$suppb" "n"
   iret=$?
   if [ $iret -eq 1 ]; then
      from=""
   fi
   jret=$iret
   while [ $jret -ne 0 ];
   do
      search_find "$qbin" "$from" "$suppb" "$errb"
      iret=$?
      if [ $iret -eq 4 ]; then
         jret=0
      else
         jret=$iret
         from=""
      fi
      if [ $iret -eq 0 ]; then
         confirm "Accept" "$result" "y"
         jret=$?
      fi
   done
   if [ $iret -ne 0 -a $errb = "y" ]; then
      echo " Installation aborted !"
      exit 1
   fi
   return $iret
}

######################################################################
# search_lib qlib label from errl [sd]
#     cherche une lib avec search_find
# qlib   : lib
# label  : label parlant
# from   : a partir d'ou
# errl   : y si on arrete en cas d'erreur
# sd     : si on veut imposer static ou dynamic
# chem   : chemin resultat
# opt_lib : ligne .../lib.a ou -Ldir -llib
# RETOUR : 0 si lib trouvee et confirmee, 1 si pas trouvee ou pas confirmee
function search_lib
{
   if [ $# -lt 4 -o $# -gt 5 ]; then
      echo "Wrong arguments for search_lib"
      exit 1
   fi
   qlib=$1
   label=$2
   from=$3
   errl=$4
   sd="sd"
   if [ $# -eq 5 ]; then
      sd=$5
   fi
   typ="s"
   jret=0
   while [ $jret -eq 0 ];
   do
      if [ "$sd" = "sd" ]; then
         echo " Search for $label <s>tatic or <d>ynamic library (s/d, default s) ? "
         read rtn
      else
         rtn="$sd"
      fi
      if [ -z "$rtn" ] || [ $rtn = "s" -o $rtn = "S" ]; then
         typ="s"
         lib="lib$qlib.a"
      else
         typ="d"
         lib="lib$qlib.so"
      fi
      if [ -z "$from" ]; then
         echo " Enter location to search for $lib (ex: /, /usr or /usr/lib...) ? "
         read from
      fi
      search_find "$lib" "$from" "@" "n"
      iret=$?
      if [ $iret -ne 0 ]; then
         from=""
         confirm " Do you want to try another location or change static/dynamic" "bidon" "y"
         jret=$?
      else
         jret=1
      fi
      sd="sd"
   done
   if [ $iret -eq 0 ]; then
      chem=`echo $result | sed -e "s#/$lib##"`
      if [ $typ = "d" ]; then
         opt_lib="-L$chem -l$qlib"
      else
         opt_lib="$result"
      fi
   else
      if [ $errl = "y" ]; then
        echo " Installation aborted !"
        exit 1
      fi
   fi
   return $iret
}

######################################################################
# search_prog prg label from suppb errl
#     cherche un programme avec search_bin
# prg    : programme
# label  : label parlant
# from   : a partir d'ou
# suppb : chaine a retirer du resultat
# errl   : y si on arrete en cas d'erreur
# result : chemin resultat (par confirm)
# RETOUR : 0 si lib trouvee et confirmee, 1 si pas trouvee ou pas confirmee
function search_prog
{
   if [ $# -ne 5 ]; then
      echo "Wrong arguments for search_prog"
      exit 1
   fi
   prg=$1
   label=$2
   from=$3
   suppb=$4
   errl=$5
   jret=0
   while [ $jret -eq 0 ];
   do
      if [ -z "$prg" ]; then
         echo " Enter a name for $label ? "
         read prg
      fi
      if [ -z "$from" ]; then
         echo " Enter location to search for $prg (ex: /, /usr or /usr/bin...) ? "
         read from
      fi
      search_bin "$prg" "$from" "$suppb" "n"
      iret=$?
      if [ $iret -ne 0 ]; then
         from=""
         prg=""
         confirm " Do you want to try another location or change program name" "bidon" "y"
         jret=$?
      else
         jret=1
      fi
   done
   if [ $iret -ne 0 ]; then
      if [ $errl = "y" ]; then
        echo " Installation aborted !"
        exit 1
      fi
   fi
   return $iret
}

######################################################################
# check_lib lib label symbol errk
#     verifie que la librairie "lib" contient le symbole "symbol"
# lib    : librairie
# label  : label parlant
# symbol : symbol a tester
# errk   : y si on arrete en cas d'erreur
# RETOUR : 0 si symbole trouve et confirme, 1 si pas trouve ou pas confirme
function check_lib
{
   if [ $# -ne 4 ]; then
      echo "Wrong arguments for check_lib"
      exit 1
   fi
   lib=$1
   label=$2
   symbol=$3
   errk=$4
   shlib=`basename $lib`
   echo " Check for $label library : $lib"
   flag=""
   if [ `file $lib | grep -c 'symbolic *link'` -eq 0 ]; then
      tlib="$lib"
   else
      tlib=`file $lib | $awk '{print $NF;}'`
      if [ ! -f "$tlib" ]; then
         dil1=`dirname $lib`
         tlib="$dil1/$tlib"
      fi
   fi
   if [ `file $tlib | grep -c archive` -eq 0 ]; then
      cmd="$nmD"
   else
      cmd="$nm"
   fi      
   if [ ! -f $tlib ] || [ `$cmd $tlib | grep -c $symbol` -eq 0 ]; then
	   if [ $errk = "y" ]; then
         echo " $shlib inexistant or require symbol not found in $shlib,"
         echo " you need $label sources to compile Code_Aster."
	      echo " Installation aborted !"
	   	exit 8
      else
         echo " $shlib inexistant or require symbol not found in $shlib."
	   fi
      echo " "
      return 1
   else
      echo "   $shlib seems ok."
      echo " "
      return 0
   fi
}

######################################################################
# check_modpy pydir binpy label module vers
#     verifie que le module "module" est accessible par binpy
# pydir  : directory python
# binpy  : exec python
# label  : label parlant
# module : module a tester
# vers   : version minimal attendue
# RETOUR : 0 si module trouve et version correcte,
#          1 si trouve ou version moins recente
#          4 si import failed
function check_modpy
{
   if [ $# -ne 5 ]; then
      echo "Wrong arguments for check_modpy"
      exit 1
   fi
   pydir=$1
   binpy=$2
   label=$3
   module=$4
   vers=$5
   if [ -z "$vers" ]; then
      vers="?"
   fi
   ftmp=/tmp/check_modpy.$$
   echo " Check for Python module : $label"
   PYTHONHOME=$pydir
cat << EOF > $ftmp
import $module
print "$module", $module.__version__
exit
EOF
   $binpy $ftmp > $ftmp.out 2>&1
   iret=0
   if [ `grep -c ImportError $ftmp.out` -ne 0 ]; then
      echo " $module inexistant or import failed."
      echo " You should install $label version $vers to use all Code_Aster capabilities."
      iret=4
   else
      modver=`grep $module $ftmp.out | head -1 | $awk '{print $2;}'`
      if [ -z "$modver" ] || [ $modver != $vers ]; then
         if [ -z "$modver" ]; then
            modver="(unknown)"
         fi
         echo "   Version $modver have not been tested with Code_Aster."
         echo "   If you have problems, you should install version $vers."
         iret=1
      else
         echo "   $module seems ok."
      fi
   fi
   echo " "
   rm -f $ftmp $ftmp.out
   return $iret
}

######################################################################
# check_inc inc label erri
#     verifie que l'include "inc" est present
# inc    : include
# label  : label parlant
# erri   : y si on arrete en cas d'erreur
# RETOUR : 0 si symbole trouve et confirme, 1 si pas trouve ou pas confirme
function check_inc
{
   if [ $# -ne 3 ]; then
      echo "Wrong arguments for check_inc"
      exit 1
   fi
   inc=$1
   label=$2
   erri=$3
   shinc=`basename $inc`
   echo " Check for $label include : $inc"
   if [ ! -f $inc ]; then
	   if [ $erri = "y" ]; then
         echo " $inc inexistant, you need $label sources to compile Code_Aster."
	      echo " Installation aborted !"
	   	exit 8
      else
         echo " $inc inexistant."
	   fi
      echo " "
      return 1
   else
      echo "   $inc seems ok."
      echo " "
      return 0
   fi
}

######################################################################
#
# check commands use by the installation script
# sed, file, head, grep are not tested

search_which "awk" "@" "y"
awk=$result

search_which "nm" "@" "y"
nm=$result
nmD=$nm

