#!/bin/sh 
#--------------------------- Shell MegaWave Macro ----------------------------#
_sccsid="%Z%MegaWave2 %R%, %M% %I%, %G%";
_Prog="`basename $0`"
_Vers="1.31";
_Date="2007";
_Func="Compile all MegaWave2 modules and user's macros, recursively on each subdirectory";
_Auth="Jacques Froment";
_Usage="[-traditional] [-gnu] [-clear] [-g] [-X] [-O]  [-c comp&link option] [-cc compiler option] [-cl linker option] [-Dname[=def]] [-Uname] [-Ipathname] [-w] [-Ldirectory] [-llibrary] [-2p] [-sp] [-dep] [-pubsyslib] [-force] [-v tracefile] [-N processid] group_directory"

#----------------------------------------------------------------------------#
# 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 
#-----------------------------------------------------------------------------

# Remove all old temporary files that have been generated by cmw2,
# so that no confusion can be made.

RemoveTMPfiles()

{
 if  [ "$TMP" != ""  ] && [ -d $TMP ] &&  [ -w $TMP ]; then
  for f in `find ${TMP}/. -name "mw*" -ctime +1 -type f -print 2> /dev/null`
  do
   /bin/rm -f $f > /dev/null 2>&1
  done
 fi
}

ChooseTMP()

{
if  [ "$TMP" = ""  ] || [ ! -d $TMP ] ||  [ ! -w $TMP ]; then
 TMP=/usr/tmp
 if  [ ! -d $TMP ] ||  [ ! -w $TMP ]; then
  TMP=/tmp
  if  [ ! -d $TMP ] ||  [ ! -w $TMP ]; then
   echo "$_Prog : Fatal Error : cannot choose TMP !"
   exit 1
  fi
 fi
fi
}

Echo()

{
 echo "$1"
 echo "$1" >> $trace
}

if [ "${MW_MACHINETYPE}" = "" ]; then
   echo "Please set the \$MW_MACHINETYPE environment variable" 1>&2
   exit 1
fi

BIN=bin/$MW_MACHINETYPE
LIB=lib/$MW_MACHINETYPE
OBJ=obj/$MW_MACHINETYPE
DOC=doc/obj
MWI=mwi

# test entree
if [ $# -lt 1 ]; then
   . .mw2_help_lg_com
fi

force=0
clear=0

# 1 if two-pass process asked (set at the first call only)
twopass=0

# -fp if first-pass in the two-pass process
firstpass=""

# -sp if second-pass
secondpass=""

# trace file. By default, /dev/null : no trace
trace="/dev/null"

cmw_opt=""
cmw_all_opt=""

while [ "$1" != "" ]
do
    case "$1" in

    "-clear")  clear=1
               shift
               ;;

    "-force")  force=1
               shift
               ;;

    "-v")      shift
	       trace=$1
	       shift
	       cmw_all_opt="$cmw_all_opt -v $trace"    
               ;;

    "-2p")  twopass=1
	    shift
            ;;

    "-sp")  secondpass=-sp
	    shift
            ;;

    "-fp")  firstpass=-fp
	    shift
            ;;

    "-dep") cmw_all_opt="$cmw_all_opt -dep"
	    cmw_opt="$cmw_opt -dep"
            shift
            ;;

    "-pubsyslib") cmw_all_opt="$cmw_all_opt -pubsyslib"
		  cmw_opt="$cmw_opt -pubsyslib"
		  shift
		  ;;

     -c)	shift
		cmw_all_opt="$cmw_all_opt -c $1"
		cmw_opt="$cmw_opt -c $1"
		shift
		;;

     -cc)	shift
		cmw_all_opt="$cmw_all_opt -cc $1"
		cmw_opt="$cmw_opt -cc $1"
		shift
		;;

     -cl)	shift
		cmw_all_opt="$cmw_all_opt -cl $1"
		cmw_opt="$cmw_opt -cl $1"
		shift
		;;

    -N)   NoDisplay=yes;
          shift;
          processid=$1;
          shift;
          ;;

          
    -g)   cmw_all_opt="$cmw_all_opt -g";
	  cmw_opt="$cmw_opt -g";
          shift;
          ;;

    -light)	cmw_all_opt="$cmw_all_opt -light"
		cmw_opt="$cmw_opt -light"
    		shift
		;;

    -traditional)	cmw_all_opt="$cmw_all_opt -traditional"
		cmw_opt="$cmw_opt -traditional"
    		shift
		;;

    -gnu)	cmw_all_opt="$cmw_all_opt -gnu"
		cmw_opt="$cmw_opt -gnu"
    		shift
		;;
		
    -X)         cmw_all_opt="$cmw_all_opt -X"
		cmw_opt="$cmw_opt -X"
    		shift
    		;;

    -O*)        cmw_all_opt="$cmw_all_opt $1"
		cmw_opt="$cmw_opt $1"
    		shift
    		;;

    -D*|-I*|-U*) cmw_all_opt="$cmw_all_opt $1"
		 cmw_opt="$cmw_opt $1"
                 shift
		 ;;

    -w)         cmw_all_opt="$cmw_all_opt $1"
		cmw_opt="$cmw_opt $1"
		shift
		;;

    -L*)	cmw_all_opt="$cmw_all_opt $1"
		cmw_opt="$cmw_opt $1"
		shift
		;;

    -l*)	cmw_all_opt="$cmw_all_opt $1"
		cmw_opt="$cmw_opt $1"
		shift
		;;

    -*)   . .mw2_help_lg_com;
	  ;;

    *)  dir=$1
	shift
	;;

    esac
done

if [ ! -d $dir ]; then
  echo "$_Prog : Bad directory '$dir' !" 1>&2
  exit 2
fi

CWD=`/bin/pwd`
cd $dir
TD=`/bin/pwd`
cd $CWD
MWDESTDIR=`echo $TD | grep $MY_MEGAWAVE2`
if [ "$MWDESTDIR" = "$TD" ]; then
  MWDESTDIR=$MY_MEGAWAVE2
else
  MWDESTDIR=`echo $TD | grep $MEGAWAVE2`
  if [ "$MWDESTDIR" = "$TD" ]; then
    MWDESTDIR=$MEGAWAVE2
    if [ ! -w  $MWDESTDIR ]; then
      echo "$_Prog : No write permission on '$MWDESTDIR' !" 1>&2
      echo "           Are you sure you are the MegaWave2 Administrator ?" 1>&2
      exit 4
    fi
  else
    echo "$_Prog : Directory '$dir' is not a subdir of \$MY_MEGAWAVE2 or of \$MEGAWAVE2" 1>&2
    exit 3
  fi
fi

ChooseTMP

if [ "$NoDisplay" != "yes" ]; then

# First entry for this command

 if [ "$trace" != "/dev/null" ] && [ -f $trace ]; then
  /bin/rm -f $trace > /dev/null
 fi

 if [ "$MWDESTDIR" = "$MEGAWAVE2" ] && [ $force -eq 0 ]; then
   echo "Warning: you will modify the system files on $MWDESTDIR"
   mwecho -n "         do you wish to continue [y or n] ?"
   a="N"
   read a
   if [ "$a" = "n" ] || [ "$a" = "N" ]; then
     exit 1
   fi
 fi

 if [ $twopass = 1 ]; then
   clear=1
   echo "Warning: using two-pass compilation to make a complete new version."
   echo "         I have to clear the current version of the objects."
 fi

 if [ "$clear" = "1" ]; then

# Clear target directories

  a="N"
  rep=$MWDESTDIR/$BIN
  arep=""
  if [ -d $rep ]; then
   echo "           $rep"
   arep="$arep $rep"
  fi
  rep=$MWDESTDIR/$LIB
  if [ -d $rep ]; then
   echo "           $rep"
   arep="$arep $rep"
  fi
  rep=$MWDESTDIR/$OBJ
  if [ -d $rep ]; then
   echo "           $rep"
   arep="$arep $rep"
  fi
  rep=$MWDESTDIR/$MWI
  if [ -d $rep ]; then
   echo "           $rep"
   arep="$arep $rep"
  fi
  rep=$MWDESTDIR/$DOC
  if [ -d $rep ]; then
   echo "           $rep"
   arep="$arep $rep"
  fi
  if [ "$arep" != "" ] && [ $force -eq 0 ]; then
   mwecho -n "         Ok to remove these target directories [y or n] ?"
   read a
   if [ "$a" = "y" ] || [ "$a" = "Y" ]; then
     echo "Removing..."
     /bin/rm -rf $arep
   else
     echo "Cannot continue."
     exit 1
   fi
  else
    if [ "$arep" != "" ]; then
      /bin/rm -rf $arep
    fi
  fi

# End clear
 fi

# Remove all old temporary files that have been generated by cmw2,
# so that no confusion can be made.
RemoveTMPfiles

processid=$$
errorfile=$TMP/cmw2_all_${processid}.error

echo "List of unsuccessful compilations :" > $errorfile
echo "--------------------------------- " >> $errorfile
echo '' >> $errorfile

Echo ''
Echo "          ----------------------------"
Echo "               Generic MegaWave2 "
Echo "          Commands Package Compilation"
Echo "          ----------------------------"
Echo ''
mwecho -n "------ Beginning at "
date
echo ''

# End first entry for this command
fi

errorfile=$TMP/cmw2_all_${processid}.error
erroroccured=$TMP/cmw2_all_${processid}.erroroccured

# trap
trap "echo Interrupt; /bin/rm -f $errorfile $erroroccured; exit 1" 2
trap "echo Quit; /bin/rm -f $errorfile $erroroccured ; exit 1" 3

if [ $twopass = 1 ]; then
  echo '' >> $errorfile
  echo 'First pass:' >> $errorfile
  echo '' >> $errorfile
  Echo ' '
  Echo '-----------------------'
  Echo 'Beginning First pass...'
  Echo '-----------------------'
  Echo ' '
  cmw2_all $cmw_all_opt -fp -N $processid ${dir}
  echo '' >> $errorfile
  echo 'Second pass:' >> $errorfile
  echo '' >> $errorfile
  Echo ' '
  Echo '------------------------'
  Echo 'Beginning Second pass...'
  Echo '------------------------'
  Echo ' '
  secondpass="-sp"
fi

for file in `ls ${dir}`
do
  if [ ! -d ${dir}/${file} ]; then
    if [ "$secondpass" != "-sp" ] || 
	[ ! -x $MWDESTDIR/bin/$MW_MACHINETYPE/`basename $file .c` ]; then 
        mwecho -n "$file "
        (cd ${dir}; cmw2 -N $cmw_opt $file 1>> $trace 2>&1;)
        retcmw=$?
	if [ $retcmw -ne 0 ]; then
         echo "${dir}/${file}" >> $errorfile
# retcmw = 2 : error due to failed link (unknown reference)
         if [ $retcmw -ne 2 ] || [ "$secondpass" = "-sp" ] || [ "$firstpass" = "" ]; then
	  mwecho -n "(failed) " 
          touch $erroroccured
         else
          mwecho -n "(delayed) "
         fi
	fi
    fi
  else
   Echo "---> Scan Subdirectory ${dir}/${file} <---"
   cmw2_all $cmw_all_opt $firstpass $secondpass -N $processid ${dir}/${file}
   Echo ''
   Echo ''
  fi
done

if [ "$NoDisplay" != "yes" ]; then

 echo ''
 mwecho -n "------ End at "
 date
 echo ''

 if [ -f $erroroccured ]; then
  cat $errorfile
  cat $errorfile >> $trace
  Echo ''
  rm -f $errorfile $erroroccured
  exit 1
 else
  Echo "All modules were successfully compiled !"
  Echo ''
  rm -f $errorfile $erroroccured
  exit 0
 fi
fi

exit 0








