#!/bin/sh
# Config Script for Crystal Space, outputs compiler flags for compiling 
# Crystal Space applications
# (c)2002,2003 by Matthias Braun <matze@braunis.de>
#
# ...code stolen from gnome-config...
CRYSTAL="${CRYSTAL-/usr}"
prefix="${CRYSTAL}"
exec_prefix="${prefix}"
makeout="./out/linuxx86/optimize"
version="0.98"
longversion="crystalspace 0.98"
# try to determine if we're in a source or installed CS version
if [ -r ${prefix}/include/cssysdef.h ] ; then
  includedir="${prefix}/include"
else
  includedir="/usr/include"
fi
syslibs=" -lm -ldl -lnsl  "
common_cflags="  "
common_cxxflags="   "

makevars()
{
cat << "__EOF__"
LFLAGS.L = -L
LFLAGS.l = -l
CFLAGS.D = -D
CFLAGS.I = -I
EXE=
DLL=.so
LIBS.EXE.PLATFORM=-ldl -lm -lnsl
LFLAGS.GENERAL=$(LFLAGS.L)/usr/local/lib -lpthread
LFLAGS.DLL=-shared -Wl,-soname -Wl,$@

__EOF__
}
# dependencies of CS
depends()
{
    case $1 in
        -lcsgeom) DEPS=" -lcsutil" ;;
        -lcsgfx) DEPS=" -lcsgeom -lcsutil" ;;
        -lcstool) DEPS=" -lcsgfx -lcsgeom -lcsutil" ;;
        -lcsutil) DEPS=" -lpthread -lz" ;;
        -lcsws) DEPS=" -lcstool -lcsgfx -lcsgeom -lcsutil" ;;

	*)
	    CEXFILE=.cex
	    findcexfile "$CEXFILE"
	    if test -r "$CEXFILE"; then
		DEPS=`/bin/sh $CEXFILE --deps`
	    else
		DEPS=''
	    fi
	    ;;                                        	    
    esac
}

checklibname()
{
    case $1 in
     csgeom) 	
	addlib "-lcsgeom" 	
	;;			
     csgfx) 	
	addlib "-lcsgfx" 	
	;;			
     cstool) 	
	addlib "-lcstool" 	
	;;			
     csutil) 	
	addlib "-lcsutil" 	
	;;			
     csws) 	
	addlib "-lcsws" 	
	;;			

    *)
	findcexfile "$1"
	if test -z "$CEXFILE"; then
	    echo "Unknown lib: $1" 1>&2
            usage 1>&2
	    exit 1
	fi
	
	addexlib "$CEXFILE"
        ;;
    esac
}

liblist="        csgeom 
        csgfx 
        cstool 
        csutil 
        csws 

"
# Detect type of the installation and the directories where the library files
# reside in.
lflags=""

#A standard installation
if test -r "${exec_prefix}/lib/libcsutil.a"; then
    lflags="-L${exec_prefix}/lib"
fi
#is it an old make type build?
if test -z "$libdir" -a -r "$prefix/Makefile" \
     -a -r "$prefix/$makeout/libcsutil.a"; then
    lflags="-L$prefix/$makeout"
fi
#new style jam build?
if test -z "$libdir" -a -r "$prefix/Jamfile" \
     -a -r "$prefix/$makeout/libs/csutil/libcsutil.a"; then
    libd=$prefix/$makeout/libs
    lflags="-L$libd/csutil -L$libd/cstool -L$libd/csgfx -L$libd/csgeom -L$libd/csws"
fi

usage()
{
	cat <<EOF
Usage: cs-config [OPTIONS] [LIBRARIES]
Options:
	[--prefix]
	[--exec-prefix]
	[--version]
	[--long-version]
	[--libdir]
	[--includedir]
	[--libs]
	[--cflags]
	[--cxxflags]
	[--makevars]
	[--help]
Libraries:
${liblist}
Note that the Crystal Space directory is detect by looking at the CRYSTAL
environment variable. Make sure this variable is set correctly.
EOF
}

includedeps()
{
#we have to remember vars here because on older shells $1,$2... are global
    id_first=$1
    id_all=$@
    shift
    id_second=$1
    id_rest=$@

# already had all dependencies of this lib? then exit
    case " $ALREADY_TESTED " in
    	*\ ${id_first}\ *) return 0;;
    	*) ;;
    esac

# if not add 1 dependency
    depends ${id_first}
    ALREADY_TESTED="$ALREADY_TESTED ${id_first}"
    for a in $DEPS; do
	case " ${id_all} " in
	    *\ $a\ *) ;;
    	    *)  
		the_libs="${the_libs} $a"
	    	return 1
	    ;;
	esac
    done
    if test -n "${id_second}"; then
	if includedeps ${id_rest}; then
	    return 0
	else
	    return 1
	fi
    else
	return 0
    fi
}

addlib()
{
    # Lib already in list?
    case " $the_libs " in
	*\ $1\ *) return;;
	*) ;;
    esac

    the_libs="$1 $the_libs"

    # loop till all dependencies are resolved
    loop=true
    while $loop; do
	includedeps $the_libs
	if test $? -eq 0; then
	    loop=false
	else
	    ALREADY_TESTED=""
	fi
    done
}

findcexfile()
{
#   file in search path?
    for p in $CSCONFPATH; do
	if test -f "$p/$1.cex"; then
	    CEXFILE="$p/$1.cex"
	    return
	fi
    done

    if test -f "$CRYSTAL/bin/$1.cex"; then
	CEXFILE="$CRYSTAL/bin/$1.cex"
	return
    fi
    
    if test -f "$CRYSTAL/$1.cex"; then
	CEXFILE="$CRYSTAL/$1.cex"
	return
    fi

    CEXFILE=""
}      

addexlib()
{
    EXDEP=`/bin/sh $1 --deps`
    EXLIB=`/bin/sh $1 --libs`
    EXCXXFLAGS=`/bin/sh $1 --cxxflags`
    EXCFLAGS=`/bin/sh $1 --cflags`
    add_cxxflags="$add_cxxflags $EXCXXFLAGS"
    add_cflags="$add_cflags $EXCFLAGS"

    for lib in "$EXDEP"; do
	addlib "$lib"
    done

    if test -n "$EXLIB"; then
        addlib "$EXLIB"
    fi
}

the_libs=""
show_libs=""
show_cflags=""
show_cxxflags=""

if test $# -eq 0; then
	usage 1>&2
	exit 1
fi

while test $# -gt 0; do
  case $1 in
    --prefix)
	echo $prefix
	exit 0
	;;
    --exec-prefix)
	echo $exec_prefix
	exit 0
	;;
    --help)
	usage
	exit 0
	;;
    --version)
        echo $version
        exit 0
        ;;
    --long-version|--longversion)
	echo $longversion
	exit 0
	;;
    --libdir)
	echo $libdir
	exit 0
	;;
    --includedir)
	echo $includedir
	exit 0
	;;
    --cflags)
        show_cflags=true
        ;;
    --includes)
	show_includes=true
	;;
    --cxxflags)
	show_cxxflags=true
	;;
    --makevars)
	makevars
	exit 0
	;;
    --libs)
	show_libs=true
	;;
    *)
	checklibname $1
        ;;
  esac
  shift
done

if test -z "$lflags"; then
  cat 1>&2 <<EOF
Couldn't detect dir with lib files, aborting!
Did you already compile CS? Did you set CRYSTAL environment variable
correctly?
EOF
  exit 1
fi

if test -n "$show_cflags"; then
	echo "-I$includedir $common_cflags $add_cflags"
else
    if test -n "$show_cxxflags"; then
	echo "-I$includedir $common_cxxflags $add_cxxflags"
    else
	if test -n "$show_includes"; then
	    echo "-I$includedir"
	fi
    fi
fi    
    
if test -n "$show_libs"; then
    # if users specified no lib, output commonly needed ones
    if test -z "$the_libs"; then
        addlib "-lcsgfx"
        addlib "-lcsgeom"
        addlib "-lcstool"
        addlib "-lcsutil"
    fi
    the_libs="${lflags} $the_libs ${syslibs}"
    echo "$the_libs"
fi

exit 0
