#!/bin/sh

# K.I.S.S :-)

with_shape=1        # shaped window extension
with_sm=1           # register with session manager
with_imlib=0        # use imlib for loading/scaling pixmaps/icons
with_gnome=0        # generate menu of gnome applications (from .desktop files)
with_i18n=0         # I18N support
with_exceptions=0   # when 0 disable exception support in g++
with_ccmalloc=0     # malloc debugging

usage() {
   cat <<EOF
Options:
   --with-shape
   --without-shape
   --with-sm
   --without-sm
   --with-i18n
   --without-i18n
   --with-imlib
   --without-imlib
   --with-gnome
   --without-gnome
EOF
#   --x-incdir=DIR
#   --x-libdir=DIR
#   --xpm-incdir=DIR
#   --xpm-libdir=DIR
}

with_x11=1
x11_incdir='/usr/X11R6/include'
x11_inc='X11/X.h'
x11_libdir='/usr/X11R6/lib'
x11_libs='X11'

with_xpm=1
xpm_inc='X11/xpm.h'
xpm_libs='Xpm'

shape_cflags='-DSHAPE'
shape_libs='Xext'

sm_cflags='-DSM'
sm_inc="X11/SM/SMlib.h"
sm_libs='SM ICE'

i18n_cflags='-DI18N'

gnome_cflags='-DGNOME $(shell gnome-config --cflags gnome)'
gnome_lconf='$(shell gnome-config --libs gnome)'

imlib_cflags='-DIMLIB $(shell imlib-config --cflags)'
imlib_lconf='$(shell imlib-config --libs)'

ccmalloc_lconf="/usr/local/lib/ccmalloc.o -ldl"

gccwarn="-Wall -Wpointer-arith -Wconversion -Wwrite-strings \
-Wmissing-prototypes -Wmissing-declarations \
-Winline"

cxx="g++"
cxxopts="$gccwarn"
link="g++"
linkopts=""
optimize="-O2"

use() {
    dep="$1"
    with="$2"
    cflags="$3"
    incdir="$4"
    inc="$5"
    libdir="$6"
    libs="$7"
    lconf="$8"

    if [ "$with" = "1" ] ; then 
        CFLAGS="$CFLAGS $cflags"
        if [ "$incdir" != "" ] ; then    
            INCDIRS="$INCDIRS -I$incdir"
            if [ ! -f $incdir/$inc ] ; then
                echo $dep: $incdir/$inc not found.
                exit
            fi
        fi
        if [ "$libdir" != "" ] ; then
            LIBDIRS="$LIBDIRS -L$libdir"
            for lib in $libs ; do
                if [ ! -f $libdir/lib${lib}.a ] ; then
                    echo $dep: $libdir/lib${lib}.a not found.
                    if [ ! -f $libdir/lib${lib}.so ] ; then
                      echo $dep: $libdir/lib${lib}.so also not found.
                      exit
                    fi
                fi
                LIBS="$LIBS -l$lib"
            done
        else
            for lib in $libs ; do
                LIBS="$LIBS -l$lib"
            done
        fi
        LIBDIRS="$LIBDIRS $lconf"
        echo "$1 enabled. $incdir $libdir"
    else
        echo "$1 disabled."
    fi
}

CFLAGS="SYS_CFLAGS="
INCDIRS="SYS_INCDIRS="
LIBDIRS="SYS_LIBDIRS="
LIBS="SYS_LIBS="

if [ `uname` = 'AIX' ] ; then
    CFLAGS="$CFLAGS -DNEED_BOOL -DNEED_STRINGS_H -DNEED_SYS_SELECT_H -DNEED_TIME_H"
    x11_incdir=/usr/include/X11R5
    x11_libdir=/use/lib/X11R5
    cxx="lcc"
    link="lcc"
    with_sm=0
fi

if [ `uname` = 'HP-UX' ] ; then
    case `uname -r` in
       9.*)
           CFLAGS="$CFLAGS -DHPUX9" ;
           x11_incdir=/usr/include/X11R5 ;
           x11_libdir=/usr/lib/X11R5 ;
           with_sm=0
       ;;
    esac
fi

if [ `uname` = 'SunOS' ] ; then
    x11_incdir=/usr/openwin/include
    x11_libdir=/usr/openwin/lib
fi

if [ `uname` = 'SGI' ] ; then
    CFLAGS="$CFLAGS -DNEED_BOOL -D_ABI_SOURCE"
    x11_incdir=/usr/include/X11R5
    x11_libdir=/usr/lib/X11R5
    cxx="CC"
    link="CC"
    with_sm=0
fi

if [ `uname` = 'Linux' ] ; then
    i18n_cflags="$i18n_cflags -DX_LOCALE" # ???
fi

if [ "$with_noexceptions"="1" ] ; then
    if [ "$cxx" = "g++" ] ; then
        ver="`g++ --version`"
        #echo "$ver"
        case "$ver" in
            egcs*)
               cxxopts="$cxxopts -fno-rtti -fno-exceptions";
               linkopts="$linkopts -fno-rtti -fno-exceptions"
               ;;
            *)
               cxxopts="$cxxopts -fno-rtti -fno-handle-exceptions";
               linkopts="$linkopts -fno-rtti -fno-handle-exceptions"
               ;;
        esac
    fi
fi

>sysdep.inc
for option in $@; do
    echo '#' $option >>sysdep.inc
    case "$option" in
       --help | -h | -help | -\? | help ) usage ; exit ;;
       --with-gnome) with_gnome=1 ;;
       --without-gnome) with_gnome=0 ;;
       --with-shape) with_shape=1 ;;
       --without-shape) with_shape=0 ;;
       --with-sm) with_sm=1 ;;
       --without-sm) with_sm=0 ;;
       --with-i18n) with_i18n=1 ;;
       --without-i18n) with_i18n=0 ;;
       --with-noexceptions) with_exceptions=0 ;;
       --without-noexceptions) with_exceptions=1 ;;
       --with-ccmalloc) with_ccmalloc=1 ;;
       --without-ccmalloc) with_ccmalloc=0 ;;
       --with-imlib) with_imlib=1 ;;
       --without-imlib) with_imlib=0 ;;
    esac
done

use "GNOME" "$with_gnome" "$gnome_cflags" "$gnome_incdir" "$gnome_inc" "$gnome_libdir" "$gnome_libs" "$gnome_lconf"
use "Imlib" "$with_imlib" "$imlib_cflags" "$imlib_incdir" "$imlib_inc" "$imlib_libdir" "$imlib_libs" "$imlib_lconf"
use "SMlib" "$with_sm" "$sm_cflags" "$sm_incdir" "$sm_inc" "$sm_libdir" "$sm_libs" "$sm_lconf"
use "SHAPE extension" "$with_shape" "$shape_cflags" "$shape_incdir" "$shape_inc" "$shape_libdir" "$shape_libs" "$shape_lconf"
use "Xpm library" "$with_xpm" "$xpm_cflags" "$xpm_incdir" "$xpm_inc" "$xpm_libdir" "$xpm_libs" "$xpm_lconf"
use "X11" "$with_x11" "$x11_cflags" "$x11_incdir" "$x11_inc" "$x11_libdir" "$x11_libs" "$x11_lconf"
use "I18N" "$with_i18n" "$i18n_cflags" "$i18n_incdir" "$i18n_inc" "$i18n_libdir" "$i18n_libs" "$i18n_lconf"
use "ccmalloc" "$with_ccmalloc" "$ccmalloc_cflags" "$ccmalloc_incdir" "$ccmalloc_inc" "$ccmalloc_libdir" "$ccmalloc_libs" "$ccmalloc_lconf"

echo "CXX=$cxx $optimize $cxxopts" >>sysdep.inc
echo "LD=$link $optimize $linkopts" >>sysdep.inc
echo $CFLAGS >>sysdep.inc
echo $INCDIRS >>sysdep.inc
echo $LIBDIRS >>sysdep.inc
echo $LIBS >>sysdep.inc
