#!/bin/sh

TOPDIR=$1
INST_LIBS=$2
INST_INCLUDE=$3
INST_SRC=$4
INST_EXAMPLE_SRC=$5
INSTALLCMD=$6
COMPILER=$7
OPTF=$8
shift; shift; shift; shift; shift; shift; shift; shift;
DEPARG=$1
LINKARGS=$2
shift; shift;
OTHER_LIBS=$1
MAKE=$2

copy_example_source()
{
    for d in `ls $TOPDIR/examples/ | grep -v CVS`
    do
        if [ -d $TOPDIR/examples/$d ]
        then
            THISDIR=`pwd`
            cd $TOPDIR/examples/$d
            [ -d "$INST_EXAMPLE_SRC/$d" ] || mkdir -m 0755 -p $INST_EXAMPLE_SRC/$d
            echo "         installing $d"
            for f in `$MAKE __instfiles`
	    do
                src=`echo $f | cut -f1 -d"="`
                dst=`echo $f | cut -f2 -d"="`
                if [ -z "$dst" ] ; then dst=$src; fi
		if [ -f "$src" ] ; then
		    ###echo $INSTALLCMD `pwd`/$src $INST_EXAMPLE_SRC/$d/$dst
		    $INSTALLCMD `pwd`/$src $INST_EXAMPLE_SRC/$d/$dst ;
		fi
            done
            cd $THISDIR
        fi
    done
}

generate_makedefs()
{
    cat <<-EOF 

TOPDIR              ?= ../
THISDIR             = .
INC                 = -I$INST_INCLUDE -I./
DEF                 = $DEF
CXXFLAGS            = \$(ARCHARGS) $OPTF \$(DEF) \$(INC)
LDFLAGS             = \$(ARCHARGS) $OPTF -L$INST_LIBS $LINKARGS
OBJS                = \$(C++FILES:.cpp=.o) \$(CXXFILES:.cpp=.o) \$(CFILES:.c=.o) 
C++                 = $COMPILER
OTHER_LIBS          = $OTHER_LIBS
VPATH               = ..

EOF
}



generate_makerules()
{
    cat <<-EOF1 | sed 's/TAB/	/' 

all : \$(EXEC) \$(LIB) \$(PLUGIN)

\$(EXEC) : Makedepend \$(OBJS) 
TAB\$(C++) \$(LDFLAGS) \$(OBJS) \$(LIBS) -o \$@

\$(LIB) \$(PLUGIN) : \$(OBJS)
TAB\$(C++) \$(LDFLAGS) \$(SHARED) \$(OBJS) \$(LIBS) -o \$@

clean : 
TABrm -f \$(OBJS) \$(MAKEDEPEND)

clobber : clean
TABrm -f \$(EXEC) \$(LIB) \$(PLUGIN)


Makedepend : \$(CXXFILES) \$(CFILES) 
TAB\$(C++) \$(INC) $DEPARG \$? > \$@


.SUFFIXES: .cpp .o
.cpp.o:
TAB\$(C++) \$(CXXFLAGS) -c $<

sinclude Makedepend

EOF1
}


[ -d $INST_SRC/Make ] || mkdir -m 0755 -p $INST_SRC/Make

echo =========== Installing Example Source Code =================

echo "  generating make files ..."
generate_makedefs > $INST_SRC/Make/makedefs
generate_makerules > $INST_SRC/Make/makerules

echo "  copying example files ..."
copy_example_source

exit 0
