#!/bin/sh 
#--------------------------- Shell MegaWave2 Macro --------------------------#
_sccsid="%Z%MegaWave %R%, %M% %I%, %G%";
_Func="Make the dependencies list (See Also) of the modules' documentation"
_Prog="`basename $0`"
_Vers="2.9"
_Date="1996-2007"
_Auth="Jacques Froment";
_Usage="[-adm] [-v]"
#----------------------------------------------------------------------------#
# This file is part of the MegaWave2 system macros. 
# MegaWave2 is a "soft-publication" for the scientific community. It has
# been developed for research purposes and it comes without any warranty.
# The last version is available at http://www.cmla.ens-cachan.fr/Cmla/Megawave
# CMLA, Ecole Normale Superieure de Cachan, 61 av. du President Wilson,
#       94235 Cachan cedex, France. Email: megawave@cmla.ens-cachan.fr 
#-----------------------------------------------------------------------------
# WARNING: This code is compiler and language-dependent.
#          More precisely, it has to be adapted to the text written by the
#          linker when it lists the unsatisfied symbols.
#          Such code lines are labeled using -- CD -- (begin) and 
#          -- CD -- (end)

MW_MYDEP=$MY_MEGAWAVE2/doc/obj/DEPENDENCIES
verbose=0
while [ "$1" != "" ]
do
  case "$1" in
    -adm)	MW_MYDEP=$MEGAWAVE2/doc/obj/DEPENDENCIES
		;;

    -v)		verbose=1;
		;;

    *)		echo "arg \"$1\" cannot be used in `basename $0`"
		. .mw2_help_lg_com
		exit 1
		;;
  esac
  shift
done


if [ ! -d $MW_MYDEP ]; then
  echo "Error: directory $MW_MYDEP not found !"
  exit 1
fi

#--- Figure out the output type of the linker ---

FirstMis="${MW_MYDEP}/*.mis"
FirstMis=`echo $FirstMis  | cut -f 1 -d " "`
if [ ! -r $FirstMis ]; then
  if [ $verbose -eq 1 ]; then
    echo "-> No dependencies found ! <-"
  fi
  exit 0
fi

# -- CD -- (begin)
# cases where the linker use word "symbol" 
magic=`head -3 $FirstMis | grep "symbol" | head -1 | cut -d " " -f 2-`
ldtype=0
#echo 1:\"$magic\"
if [ "$magic" = "Unsatisfied symbols:" ]; then
  ldtype=1
else
  if [ "$magic" = "Undefined symbol " ]; then
    ldtype=2
  else
    magic=`echo $magic | cut -d " " -f 1-3`
# SunOS 5.x
    if [ "$magic" = "symbol in file" ]; then
      ldtype=3
    else
# SunOS 5.x messages en francais
      if [ "$magic" = "symbole dans le" ]; then
        ldtype=30
      fi
    fi
#echo 2:\"$magic\"
  fi
fi
# Linker does not use word "symbol". Try "In function" (Linux)
if [ $ldtype = 0 ]; then
 magic=`head -1 $FirstMis | grep "In function" | cut -d " " -f 2-3` 
# echo 3:\"$magic\" 
 if [ "$magic" = "In function" ]; then
# Linux
   ldtype=4
 fi
fi

#echo "ldtype=$ldtype"

# -- CD -- (end)

if [ $ldtype = 0 ]; then
 echo "Output generated by the linker not compatible with `basename $0` !"
 echo "Tip : if your language setting is not english, redo all compilations with english locale"
 echo "Abort the operation."
 exit 1
fi


#--- Remove all called / calling files

rm -f ${MW_MYDEP}/*.called
rm -f ${MW_MYDEP}/*.calling

if [ $verbose -eq 1 ]; then
  echo "-> Make the list of called and calling modules <-"
fi

for fdep in `ls ${MW_MYDEP}/*.dep`
do
  module=`basename ${fdep} .dep`
  if [ $verbose -eq 1 ]; then
    mwecho -n "$module "
  fi
  fmis=${module}.mis
  fcalled=${module}.called

#--- List in $fcalled the modules called by the current module $module

  if [ -f ${MW_MYDEP}/$fmis ]; then
    case $ldtype in
# -- CD -- (begin)
      1)  mcalled=`cat ${MW_MYDEP}/$fmis | grep "(code)" | cut -d " " -f 4`
        ;;
      2)  mcalled=`cat ${MW_MYDEP}/$fmis | grep "_" |  cut -d "_" -f 2`
        ;;
      3|30)  mcalled=`cat ${MW_MYDEP}/$fmis | grep "${module}.o" |  cut -d " " -f 1`
        ;;
      4) mcalled=`cat ${MW_MYDEP}/$fmis | grep "undefined reference to" | cut -d '\`' -f 2 | cut  -d "'" -f 1 | sort | uniq | tr -d [:blank:]`
	 ;;
# -- CD -- (end)
      *)  echo "Invalid linker type value: $ldtype"
        echo "Abort the operation."
        exit 1
        ;;
    esac   
  echo "$mcalled" > ${MW_MYDEP}/$fcalled  

#--- For each called module, add in fcalling the current module $module.

  for mc in $mcalled
  do
    fcalling=${mc}.calling
    echo $module >> ${MW_MYDEP}/$fcalling
  done
  fi
# end of for fdep in...
done


#--- From fcalled and fcalling, make the TeX dependencies file $fdep

if [ $verbose -eq 1 ]; then
  echo " "
  echo "-> Make the dependencies TeX file <-"
fi

for fdep in `ls ${MW_MYDEP}/*.dep`
do
  module=`basename ${fdep} .dep`
  if [ $verbose -eq 1 ]; then
    mwecho -n "$module "
  fi
  fcalled=${module}.called
  fcalling=${module}.calling
  if [ -f ${MW_MYDEP}/$fcalled ] || [ -f ${MW_MYDEP}/$fcalling ]; then
    echo "\SeeAlso{"  > $fdep
    if [ -f ${MW_MYDEP}/$fcalled ]; then
      first=1
      for m in `cat ${MW_MYDEP}/$fcalled`
      do
        if [ $first = 0 ]; then
          mwecho -n ", " >> $fdep
        fi
        mwecho -n -E "\bblmodules{${m}}" | sed "s/_/\\\_/g" >> $fdep
        first=0
      done
      echo ". " >> $fdep   
      echo " " >> $fdep
    fi
    if [ -f ${MW_MYDEP}/$fcalling ]; then
      first=1
      for m in `cat ${MW_MYDEP}/$fcalling`
      do
        if [ $first = 0 ]; then
          mwecho -n ", " >> $fdep
        fi
        mwecho -n -E "\bblmodules{${m}}" | sed "s/_/\\\_/g"  >> $fdep
        first=0
      done
      echo ". " >> $fdep   
    fi
    echo "}"  >> $fdep
  fi
# end of for fdep in...
done
if [ $verbose -eq 1 ]; then
  echo " "
fi

exit 0





