#!/bin/sh 
#--------------------------- Shell MegaWave2 Macro --------------------------#
_Prog="`basename $0`"
_Vers="1.08"
_Date="1999-2003"
_Func="Configure a Makefile from Makefile.in for the current architecture"
_Auth="Jacques Froment";
_Usage="'optional keywords for #= encapsulated lines'"
#----------------------------------------------------------------------------#
# 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 : Makefile.in is a Makefile with the following encapsulated lines :
# #. <...> : the line <...> is written if the following conditions are met :
# #! <arch1 arch2 ...> : $MW_MACHINETYPE or $MW_SYSTEMETYPE must be in the list
#                        <arch1 arch2 ...> or the list is <all>.
# #= <opt> : <opt> must be in the list given by the optional keywords (if 
#            given) or <opt> is <none>.

# Echo without linefeed on every Unix machines
echon()

{
if [ $MW_SYSTEMTYPE = SunOS ]; then
# On Solaris, use printf instead of echo
  if [ $MW_MACHINETYPE = sun4_5 ]; then
    printf "$1"
  else
    if [ -x /usr/ucb/echo ]; then
      /usr/ucb/echo -n "$1"
    else
     echo -n "$1"
    fi
  fi
else
  if [ $MW_SYSTEMTYPE = Linux ]; then
    echo -n "$1"
  else
    echo $1"\c"
  fi
fi
}

case $# in
  0)      keywords="";;
  1)      keywords=$1;;
  *)      . .mw2_help_lg_com;;
esac

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

makein="./Makefile.in"
makeout="./Makefile"
archi=" all "
opt="none"
ident="# Warning : this file has been automatically generated from $makein using macro $_Prog V.$_Vers with arch = '$MW_SYSTEMTYPE $MW_MACHINETYPE' option = '$keywords'"

if [ ! -f $makein ]; then
  echo "Error: Input Makefile '$makein' not found !"
  exit 1
fi


echo "Makefile configuration parameters : arch = '$MW_SYSTEMTYPE $MW_MACHINETYPE' opt='$keywords'"

# If the inputs are the same, do nothing.

if [ -f $makeout ]; then
  older=`find $makeout -newer $makein`
  if [ "$older" = "$makeout" ]; then
    cline=`sed -n "1 p" $makeout`
    if [ "$ident" = "$cline" ]; then
      echo "No change : do not rewrite $makeout"
      exit 0
    fi 
  fi
fi

# Else, do something (write $makeout)...

nline=`wc -l $makein | awk '{print $1}'`
line=1

# new trap
trap "echo 'interrupt ! (incomplete $makeout removed)'; rm -f $makeout; exit 1" 2
trap "echo 'quit ! (incomplete $makeout removed)'; rm -f $makeout; exit 1" 3

echon "Scanning $makein and writing $makeout ($nline lines)"
echo "$ident" > $makeout

while [ $line -le $nline ]
do
  echon "."
  cline=`sed -n "$line p" $makein`
  id=`echo $cline | cut -c 1,2`
  case "$id" in
# Add/check space before and after the word so that e.g. sun4 does not match sun4_5
    "#!") archi=" "`echo $cline | cut -c 3-`" ";
          thisarchi=`echo "$archi" | grep ' '$MW_SYSTEMTYPE' '`
	  if  [ "$thisarchi" != "$archi" ]; then
	    thisarchi=`echo "$archi" | grep ' '$MW_MACHINETYPE' '`
	  fi
	  ;;
    "#=") opt=`echo $cline | cut -c 3-`;
          thisopt=`echo "$keywords" | grep "$opt"`
	  ;;
    "#.") 
          if [ "$archi" = " all " ] || [ "$thisarchi" = "$archi" ]; then
	    if [ "$opt" = none ] || [ "$thisopt" != "" ]; then
	     echo "$cline" | cut -c 3- >> $makeout
            fi
	  fi
	  ;;
    *)  echo "$cline" >> $makeout
        ;;
  esac
  line=`expr $line + 1`
done
echo " "

exit 0
