#!/bin/sh 
#--------------------------- Shell MegaWave2 Macro --------------------------#
_sccsid="%Z%MegaWave %R%, %M% %I%, %G%";
_Func="Check the consistency of a modules file regarding the content of a source directory (\$MEGAWAVE2/src)";
_Prog="`basename $0`"
_Vers="1.4"
_Date="1998-2003"
_Auth="Jacques Froment";
_Usage="[-X file_of_modules_for_xmw2] file_of_modules"
#----------------------------------------------------------------------------#
# 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 3 ]; then
  . .mw2_help_lg_com
fi

# TMP file to output lists of modules
modlistfile="/usr/tmp/${_Prog}_file_$$"
modlistsrc="/usr/tmp/${_Prog}_src_$$"
if [ -f $modlistfile ]; then
  \rm $modlistfile
fi
if [ -f $modlistsrc ]; then
  \rm $modlistsrc
fi

# Default source directory (could be overwritten using #dir)
MW_SOURCE=$MEGAWAVE2
# Default source subdir (could be overwritten using #group)
MW_GROUP=""

XMW2file=""

while [ "$2" != "" ]
do
        case "$1" in
        "-X")        shift
                     XMW2file=$1
		     echo "Warning : option -X not implemeted yet"
                     shift
                     ;;

        *)      . .mw2_help_lg_com
                ;;
       
       esac
done

file=$1

if [ ! -f $file ]; then
  echo "${_Prog}: Cannot open file ${file}" 1>&2
  exit 1
fi
if [ "$XMW2file" != "" ] && [ ! -f $XMW2file ]; then
  echo "${_Prog}: Cannot open file ${XMW2file}" 1>&2
  exit 1
fi


#------------------------------- P H A S E  1  ------------------------------

echo " " 
echo "--> Phase 1 : check modules listed in the file '$file'..."
echo " " 

# Get the number of lines in the file 
nlines=`sed -n "$ =" $file`
if [ "$nlines" = "" ]; then
  echo "${_Prog}: Error : Missing <end of line> at the end of file ${file}" 1>&2
  exit 1
fi

nl=0
while [ $nl -ne $nlines ]
do

 module=""

 nl=`expr $nl + 1`
 line=`sed -n "$nl,$nl p" $file`

 void=`echo $line | cut -d% -f1`
 if [ ! "$void" = "" ]; then

  command=`echo $line | cut -d" " -f1 | grep "#"`
  case "$command" in


#----- New directory
    "#dir") MW_SOURCE=`echo $line | cut -d" " -f2`
	    if [ ! -d $MW_SOURCE ]; then
 	      echo "${_Prog}: Error : Illegal directory $MW_SOURCE" 1>&2
	      exit 1
	    fi
            ;;

#----- New group

    "#group")  MW_GROUP=`echo $line | cut -d" " -f2`
	    if [ ! -d $MW_SOURCE/src/$MW_GROUP ]; then
 	      echo "${_Prog}: Error : Cannot find group directory $MW_SOURCE/src/$MW_GROUP" 1>&2
	      exit 1
	    fi
            echo "Scanning group \"${MW_GROUP}\"..."
            ;;

#----- New module
    "")  module=`echo $line | cut -d" " -f2`
         if [ ! -r $MW_SOURCE/src/$MW_GROUP/${module}.c ]; then
           if [ -x  $MW_SOURCE/src/$MW_GROUP/${module} ]; then
# This is a macro (Shell)
             modfile=${module}
           else
             echo "${_Prog}: Error : Cannot read module $MW_SOURCE/$MW_GROUP/${module}.c or macro $MW_SOURCE/$MW_GROUP/${module}" 1>&2
             exit 1
           fi
	 else
# This is a module (C file)
           modfile=${module}.c
	   if [ ! -r $MW_SOURCE/doc/obj/${module}.doc ]; then
 	     echo "${_Prog}: Error : cannot read doc of module ${module} (Maybe a wrong group declaration or the compilation failed)." 1>&2
	     exit 1
           fi
           if [ ! -r $MW_SOURCE/doc/src/${module}.tex ]; then
 	     echo "${_Prog}: Error : module ${module} is not documented." 1>&2
	     exit 1
	   fi
         fi
         ;;


#----- Illegal command
    *) echo "${_Prog}: Illegal command $command" 1>&2
       exit 1
       ;;
  esac

  if [ ! "$module" = "" ]; then
    echo "${MW_GROUP}/${module}" >> $modlistfile
  fi
 fi 
done

#------------------------------- P H A S E  2 ------------------------------

echo " " 
echo "--> Phase 2 : see if modules may have been forgotten in the file '$file'..."
echo " " 
echo "The following modules of $MW_SOURCE/src are not written in '$file' :"
echo " " 

mwmodlist > $modlistsrc

# Get the number of lines in the file 
nlines=`sed -n "$ =" $modlistsrc`

if [ "$nlines" = "" ]; then
  echo "${_Prog}: Missing <end of line> at the end of file ${modlistsrc}" 1>&2
  exit 1
fi

nl=0
while [ $nl -ne $nlines ]
do
 module=""
 nl=`expr $nl + 1`
 line=`sed -n "$nl,$nl p" $modlistsrc`
 found=`grep $line $modlistfile`
 if [ "$found" = "" ]; then
   echo $line
 fi
done

echo " " 
echo "--> The check of modules file '$file' is done !"

if [ -f $modlistfile ]; then
  \rm $modlistfile
fi
if [ -f $modlistsrc ]; then
  \rm $modlistsrc
fi
exit 0



