# $Header: /home/users/hurtta/cvsroot/mail/elmme+/ConfTool/Substitute,v 2.6 2016/02/07 16:26:39 hurtta Exp $

    . ./config.sh

for i in hdrs shared_libs shared_libs/include
do
	if $test ! -d $i
	then
	   echo "Creating build subdirectory $i"

	   mkdir $i || exit 1
	fi
done

write_makefile() {

   echo "Creating build $1"

    echo "#!$make" >$1
    echo "# Generated by $0" >>$1

    if $test "$source_dir" = "$build_dir" 
    then

	$cat >>$1   <<!GROK!THIS!
all:
install_s:
uninstall_s:
clean:

!GROK!THIS!

    else

	$cat >>$1   <<!GROK!THIS!
all: $headers

install_s:
uninstall_s:

clean:
	$rm -f $headers

!GROK!THIS!

	for i in $headers
	do
	  $cat >>$1   <<!GROK!THIS!
$i: $source_dir/$2/$i
		$cp \$? \$@

!GROK!THIS!

	done       
    fi
 

}

# nls/Makefile may be generated these files, so if we have
# new files on source directory use them
A="`$sed '\#hdrs/s_#{s#hdrs/##;p;};d' $source_dir/MANIFEST`"
N="`$echo $A`" 

headers="elm_defs.h me.h defs_major.h shared_imp.h misclib.h $N"
write_makefile hdrs/Makefile hdrs

headers="elmshared.h"
write_makefile shared_libs/include/Makefile shared_libs/include



    xx="$source_dir/MANIFEST" 
    if test ! -f "$xx" ; then
       echo "$xx do not found ..."
       exit 1
    fi

    echo " "
    echo "Doing variable substitutions on .SH files..."
    set x `awk '{print $1}' < "$xx" | $grep '\.SH'`
    shift

    for file in $*; do

	case "$file" in
	*/*)
	    dir=`$expr X$file : 'X\(.*\)/'`
	    file=`$expr X$file : 'X.*/\(.*\)'`
	    
	    if test ! -d "$dir" ; then
	       echo "Creating build subdirectory $dir"
	       mkdir "$dir" || exit 1
	    fi

	    (
	          cd "$dir"	|| exit 1

		  subdir="$dir"
		  . "$source_dir/$dir/$file"
	     )
	    ;;
    	*)
	    ( 
	      subdir="./"	
	      . "$source_dir/$file"
	    )
	    ;;
    	esac
    done
    if test -f "$source_dir/config.h.SH"; then
    	if test ! -f config.h; then
	    : oops, they left it out of MANIFEST, probably, so do it anyway.
	    . "$source_dir/config.h.SH"
    	fi
    fi

    if $test "$catmansrc" != "none" -a ! -d doc/catman
    then
	   echo "Creating build subdirectory doc/catman"

	   mkdir doc/catman || exit 1

    fi

    if $contains '^depend:' Makefile >/dev/null 2>&1; then
    	dflt=n
    	$cat <<EOM

Now you need to generate make dependencies by running "make depend".
You might prefer to run it in background: "make depend > makedepend.out &"
It can take a while, so you might not want to run it right now.

EOM
    	rp="Run make depend now? [$dflt]"
    	$echo $n "$rp $c"
    	. $UU/myread
    	case "$ans" in
    	y*) make depend && echo "Now you must run a make."
	    ;;
    	*)  echo "You must run 'make depend' then 'make'."
	    ;;
    	esac
    elif test -f Makefile; then
    	echo " "
    	echo "Now you must run a make."
    else
    	echo "Done."
    fi
