#!/bin/sh

# $Id: makeSrcMakefile,v 1.17 2004/09/23 09:17:20 svein Exp $

if_test () {
# $1: First operand
# $2: Operator
# $3: Second operand

    op1=$1; operator=$2; op2=$3;

    if test "$XITE_HOSTTYPE" != "Win32"; then
	echo "       -@if test $op1 $operator $op2; then \\"
    else
	if test "$operator" = "="; then
	    operator="NEQ"
	else
	    operator="EQU"
	fi
	echo "       -@IF $op1 $operator $op2 goto EOF"
    fi
}

end_if () {
    if test "$XITE_HOSTTYPE" != "Win32"; then
	echo "       fi"
    fi
}

unix_only_tab () {
    if [ "$XITE_HOSTTYPE" != "Win32" ]; then
	echo "	     $*"
    fi
}

unix_only () {
    if [ "$XITE_HOSTTYPE" != "Win32" ]; then
	echo "$*"
    fi
}

win_only () {
    if [ "$XITE_HOSTTYPE" = "Win32" ]; then
	echo "$*"
    fi
}

cd_and_make () {
    # $1: Target to update in subdirectory makefile
    # $2: Directory to process
    if [ "$XITE_HOSTTYPE" != "Win32" ]; then
	echo "	cd $2; \$(MAKE) $1; cd ..;"
    else
	echo "	cd $2 & \$(MAKE) /nologo \$(MAKEFLAGS) /f \$(MAKEFILE) $1 & cd .."
    fi
}

for_loop () {
    # $1: Target to update in subdirectory makefile
    # $2: List of directories to process
    if [ "$XITE_HOSTTYPE" != "Win32" ]; then
	echo "	for d in $2; do \\"
	echo "	  if [ -d \$\$d ]; then \\"
	echo "	    cd \$\$d; \$(MAKE) $1; cd ..; \\"
	echo "	  fi \\"
	echo "	done"
    else
	echo "	FOR /D %%d IN ($2) DO \\"
	echo "	  cd %%d & \$(MAKE) /nologo \$(MAKEFLAGS) /f \$(MAKEFILE) $1 & cd .."
    fi
}

dir_target () {
    # $1: Name of target
    # $2: List of directory dependencies

    $echoNl "${1}: "
    for d in $2; do
	echo " \\"
	$echoNl "	${d}_$1"
    done
    echo ""
    for d in $2; do
	echo "${d}_$1: "
	cd_and_make "$1" "$d"
    done

} # dir_target()

prog=`basename $0`

tmpHosttype=""; help=0
while test "$#" -gt 0
do
    case $1 in
    -p)
	    if test $# -le 1; then
	      echo "Argument missing for option $1." 1>&2; help=1; break
	    fi
	    shift
	    tmpHosttype="$1"
	    ;;
    -h|-help)
	    help=1; break
	    ;;
    -x)
	    if test $# -le 1; then
	      echo "Argument missing for option $1." 1>&2; help=1; break
	    fi
	    shift
	    XITE_HOME=$1
	    ;;
    *)
	    echo "Unknown argument $1." 1>&2;
	    help=1; break
	    ;;
    esac
    shift
done

if test -z "$XITE_HOME"
then
    echo "$prog: XITE home directory is not known." 1>&2
    echo "Aborting $prog." 1>&2
    help=1
fi

if test "$help" -eq 1
then
    echo "usage: $prog [-x <XITE home-directory>] [-p <platform>] [-h(elp)]" \
	1>&2
    echo "       where default <platform> is the one you're using now" 1>&2
    echo "       and default XITE home-directory is given by the" 1>&2
    echo "       environment variable XITE_HOME." 1>&2
    exit 1
fi

utildir=$XITE_HOME/etc/.install
getPlatform=$utildir/getPlatform
echoNl=$utildir/echonl

if test -z "$tmpHosttype" -a -n "$XITE_HOSTTYPE"
then
    tmpHosttype=$XITE_HOSTTYPE
fi

XITE_HOSTTYPE=`$getPlatform "$tmpHosttype" "$utildir" stderr`

#############################################################################
#
# Start to generate Makefile.
#
#############################################################################

echo "# Main makefile for XITE software
"

if [ "$XITE_HOSTTYPE" != "Win32" ]; then

    X_SRC="Image fwf ximage xpm xshow"
    SUN=sunraster
    DIR_SEP="/"
else
    DIR_SEP="\\"
fi

MATLAB=matlab
PNG=png
PNM=pnm
TIFF=tiff

unix_only "SHELL         = /bin/sh"
unix_only "MAKE          = make"
win_only  "MAKE          = nmake"
unix_only "RM            = rm"
win_only  "RM            = del"
echo      "XITE_HOME     = $XITE_HOME"
unix_only "MAKEFILE      = \$(XITE_HOME)/src/Makefile.unix"
win_only  "MAKEFILE      = \$(XITE_HOME)\etc\Makefile.Win32"
echo      "XITE_HOSTTYPE = $XITE_HOSTTYPE"

if [ "$XITE_HOSTTYPE" != "Win32" ]; then
    echo ""
    $echoNl X_SRC = 

    for f in $X_SRC; do
	echo " \\"
	$echoNl "	$f"
    done
    echo ""

    echo ""
    echo "SUN           = $SUN"
fi

echo "MATLAB        = $MATLAB"
echo "PNG           = $PNG"
echo "PNM           = $PNM"
echo "TIFF          = $TIFF"

if [ "$XITE_HOSTTYPE" != "Win32" ]; then

    IFI_ONLY="cdb compression jpeg"

    echo ""
    $echoNl "IFI_ONLY    ="
    for f in $IFI_ONLY; do
	echo " \\"
	$echoNl "	$f"
    done
    echo ""
fi

INDEPENDENT="PostScript \
arithmetic \
biff \
binarize \
canny \
cdoc \
classification \
color \
combine \
contour \
convert \
convolve \
copy \
dither \
draw \
extrema \
fft \
fht \
fractal \
geometry \
gradInv \
haar \
histo \
hough \
ideal \
ihs \
kncn \
knn \
median \
minmax \
morph \
negate \
noise \
principal \
pyramid \
region \
resample \
scatter \
scripts \
segmentation \
sigma \
snn \
statistics \
stdiff \
texture \
thin \
thresMl \
threshold \
utils \
zernike"
#topogr \

echo ""
$echoNl "INDEPENDENT = "
for f in $INDEPENDENT; do
    echo " \\"
    $echoNl "	$f"
done
echo ""

if [ "$XITE_HOSTTYPE" = "Win32" ]; then
	ALL="$INDEPENDENT $MATLAB $PNG $PNM $TIFF"
	DSO="arithmetic $MATLAB $PNG $PNM $TIFF"
	echo ""
	echo "ALL =   \$(INDEPENDENT) \\
        \$(MATLAB) \$(PNG) \$(PNM) \$(TIFF)
"
else
	ALL="$INDEPENDENT $MATLAB $PNG $PNM $SUN $TIFF $X_SRC $IFI_ONLY"
cat <<\EOF_ALL

ALL =	$(INDEPENDENT)	\
	$(MATLAB)	\
	$(SUN)		\
	$(PNG)		\
	$(PNM)		\
	$(TIFF)		\
	$(X_SRC)	\
	$(IFI_ONLY)

EOF_ALL
	DSO="arithmetic $MATLAB $PNG $PNM $TIFF $X_SRC $IFI_ONLY"
fi

echo "
all:	include lib dso program doc"

dir_target "include" "$ALL"; echo ""
dir_target "object"  "$ALL"; echo ""
dir_target "lib"     "$ALL"; echo ""
dir_target "dso"     "$DSO"; echo ""
dir_target "program" "$ALL"; echo ""
[ "$XITE_HOSTTYPE" != "Win32" ] && ( dir_target "xprog" "$ALL"; echo "")
dir_target "doc"     "$ALL"; echo ""
dir_target "clean"   "$ALL"; echo ""

if [ "$XITE_HOSTTYPE" != "Win32" ]; then
    echo ""
    echo "independent: \$(INDEPENDENT)"
    for_loop "" "\$(INDEPENDENT)"
fi

for f in $INDEPENDENT; do
    echo ""
    echo "${f}: "
    cd_and_make "" "$f"
done

echo ""
echo "pnm: "
cd_and_make "" "\$(PNM)"

echo ""
echo "png: "
cd_and_make "" "\$(PNG)"

echo ""
echo "tiff: "
cd_and_make "" "\$(TIFF)"

echo ""
echo "matlab: "
cd_and_make "" "\$(MATLAB)"


if [ "$XITE_HOSTTYPE" != "Win32" ]; then
    echo "X_SRC:	\$(X_SRC)"

    for f in $X_SRC; do
	echo ""
	echo "${f}: "
	cd_and_make "" "$f"
    done

    echo ""
    echo "sun: "
    cd_and_make "" "\$(SUN)"

    echo ""
    echo "ifi_only: \$(IFI_ONLY)"

    for f in $IFI_ONLY; do
	echo ""
	echo "${f}: "
	cd_and_make "" "$f"
    done
fi
