#!/bin/sh
#--------------------------- Shell MegaWave2 Macro --------------------------#
_sccsid="%Z%MegaWave %R%, %M% %I%, %G%";
_Func="List the modules and macros found in the MegaWave2 directory mw2dir";
_Prog="`basename $0`"
_Vers="1.7"
_Date="1998-2002"
_Auth="Jacques Froment";
_Usage="[-mfile] [-bad] [-group gdir] mw2dir"
#----------------------------------------------------------------------------#
# 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 
#-----------------------------------------------------------------------------

# Usage
if [ $# -lt 1 ] || [ $# -gt 5 ]; then
  . .mw2_help_lg_com
fi

mfile=0
bad=0
opt=""
GROUP=""
while [ "$1" != "" ]
do
  case "$1" in
    -mfile)	mfile=1
		opt="$opt -mfile"
		;;

    -bad)	bad=1
		opt="$opt -bad"
		;;

    -group)	GROUP=$2
		shift
		;;

    [!-]*)	MW=$1
		;;

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

if [ ! -d $MW ]; then
  echo "MegaWave2 directory '$MW' not found !"
  exit 1
fi 

SRC=$MW/src

if [ ! -d $SRC ]; then
  echo "Source directory '$SRC' not found !"
  exit 1
fi 

if [ ! -d $SRC/$GROUP ]; then
  echo "Group directory '$SRC/$GROUP' not found !"
  exit 1
fi 

BIN=bin/$MW_MACHINETYPE
OBJ=obj/$MW_MACHINETYPE

PRINTGROUP=$GROUP
if  [ "$GROUP" = "" ]; then
  PRINTGROUP="."
fi

#if [ $mfile -eq 1 ] && [ "$GROUP" != "" ]; then
if [ $mfile -eq 1 ]; then
  echo " "
  subgroup=`echo ${GROUP}/ | cut -d "/" -f 2`
  if [ "$subgroup" = "" ]; then
    echo "%--------------------------"
  fi
  echo "#group $PRINTGROUP"
  if [ "$subgroup" = "" ]; then
    echo "%--------------------------"
  fi
fi

for fic in `ls ${SRC}/$GROUP`
do
  if [ ! -d ${SRC}/$GROUP/${fic} ]; then
    f=`basename ${fic} .c`
    fp=`echo ${fic} | grep "\."`
    if [ "$f.c" = "$fic" ] || [ "$fp" = "" ]; then
      # Automatically identifies a module as a macro when its name 
      # begins with a capital letter.
      mwisupper $f > /dev/null
      macro=$?
      ok=1
      if [ $macro -eq 0 ]; then
        # This is not a macro : check for objects
        if [ ! -r $MW/$BIN/${f} ] || [ ! -r $MW/doc/obj/${f}.doc ]; then 
          ok=0
        fi
        Func=`awk 'BEGIN {FS="\""} / ?function ?= ?/ {print $2;}' ${SRC}/$GROUP/${fic}`        
      else
        # This is a macro 
        if [ ! -x $MW/shell/${f} ] || [ ! -r $MW/doc/obj/${f}.doc ]; then 
          ok=0
        fi
        Func=`awk 'BEGIN {FS="\""} /^_Func=/ {print $2;}' ${SRC}/$GROUP/${fic}`
      fi  
      if [ $bad -eq 1 ]; then
       if [ $ok -eq 0 ] && [ $macro -eq 0 ]; then
        echo $f
       fi
      else
       if  [ $mfile -eq 1 ]; then
         if [ $ok -eq 1 ]; then
          echo $f
         else
          echo "%Warning : the following module was not successfully compiled !"
          echo "%$f"
         fi
       else
         if [ $ok -eq 1 ]; then
           echo "${PRINTGROUP}/$f : $Func"
         else
           echo "${PRINTGROUP}/$f  !! Warning : not successfully compiled !!"
         fi
       fi
     fi
    fi
# End of if [ ! -d ${SRC}/$GROUP/${fic} ]; then
  fi
done

for fic in `ls ${SRC}/$GROUP`
do  
  if [ -d ${SRC}/$GROUP/${fic} ]; then
    if [ "${GROUP}" = "" ]; then
      $0 $opt -group ${fic} $MW
      if [ $mfile -eq 1 ]; then
        echo "#group ."
      fi
    else
      $0 $opt -group ${GROUP}/${fic} $MW
      if [ $mfile -eq 1 ]; then
        echo "#group $PRINTGROUP"
      fi
    fi
  fi
done

exit 0
