#!/bin/sh
# Config Script for Crystal Space, outputs compiler flags for compiling 
# Crystal Space applications
#
# ...code stolen from gnome-config...
#	WARNING: This script is generated automatically! 

CRYSTAL=${CRYSTAL-/build/buildd/crystalspace-0.94-20020412/debian/tmp/usr/lib/crystalspace}
prefix=/usr
exec_prefix=${prefix}
version='0.95'
longversion='0.95.dev (Wed, 2-Apr-2002)'
includedir=/usr/include/crystalspace
if test -r ${prefix}/lib; then
  libdir=/usr/lib/crystalspace/lib
fi
syslibs="-ldl -lm -lz"
common_cflags=""
common_cxxflags="-D__CRYSTAL_SPACE__ -Wall -Wunused -W -fno-exceptions -fno-rtti -O -fomit-frame-pointer"

makevars()
{
	cat <<EOF
# Crystal Space config make variables for Linux.
# Note: automatically generated. 
EXE=
DLL=.so
LFLAGS.EXE=
DO.SHARED.PLUGIN.PREAMBLE=
DO.SHARED.PLUGIN.POSTAMBLE=
LIBS.EXE.PLATFORM=
LINK.PLUGIN=
LFLAGS.DLL=-Wl,-shared -Wl,-soname -Wl,\$@
PLUGIN.POSTFLAGS=
EOF
}

# dependencies of CS
depends()
{
    case $1 in
	-lcsutil) DEPS="-lcssys -lcsgeom";;
	-lcssys) DEPS="-lcsutil";;
	-lcsengine) DEPS="-lcssys -lcsutil -lcstool -lcsgeom";;
	-lcsgeom) DEPS="-lcsutil -lcssys";;
	-lcsgfx) DEPS="-lcssys -lcsutil";;
	-lcstool) DEPS="-lcsutil -lcssys -lcsgeom";;
	-lcsws) DEPS="-lcsgfx -lcsgeom -lcsutil -lcssys";;
	-lcsphyzik) DEPS="-lcsgeom -lcsutil -lcssys";;
	*)
	    CEXFILE=`echo "$1.cex" | sed -e "s/\-l//"`
	    findcexfile "$CEXFILE"
	    if test -r "$CEXFILE"; then
		DEPS=`/bin/sh $CEXFILE --deps`
	    else
		DEPS=''
	    fi
	    ;;                                        	    
    esac
}

#is it a source distribution?
if test -r "$prefix/Makefile"; then
#  look for the lib files...
#  a more or less dirty hack...
  if test -r "$prefix/lib/libcssys.a"; then
    libdir=/usr/lib/crystalspace/lib
  else
    #Thanks go to Jorge for this script magic...
    libdir=/usr/lib/crystalspace/lib
  fi
fi
if test -z "$libdir"; then
  cat 1>&2 <<EOF
Couldn't detect dir with lib files, aborting!
Did you already compile CS?
EOF
  exit 1
fi

usage()
{
	cat <<EOF
Usage: cs-config [OPTIONS] [LIBRARIES]
Options:
	[--prefix]
	[--exec-prefix]
	[--version]
	[--longversion]
	[--libdir]
	[--includedir]
	[--libs]
	[--cflags]
	[--cxxflags]
	[--includes]
	[--makevars]
	[--help]
Libraries:
	csengine
	csgeom
	csgfx
	csphyzik
	cssys
	cstool
	csutil
	csws
EOF
}

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

cflags=false
libs_L=false
libs_l=false

includedeps()
{
#we have to rememver 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/$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=""

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
        ;;
    --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
	;;
    cssys)
	addlib "-lcssys"
	;;
    csengine)
	addlib "-lcsengine"
	;;
    csgeom)
	addlib "-lcsgeom"
	;;
    csgfx)
	addlib "-lcsgfx"
	;;
    cstool)
	addlib "-lcstool"
	;;
    csutil)
	addlib "-lcsutil"
	;; 
    csws)
	addlib "-lcsws"
	;;
    csphyzik)
	addlib "-lcsphyzik"
	;;
    *)
	findcexfile "$1"
	if test -z "$CEXFILE"; then
	    echo "Unknown lib: $1" 1>&2
            usage 1>&2
	    exit 1
	fi
	
	addexlib "$CEXFILE"
        ;;
  esac
  shift
done

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 all
    if test -z "$the_libs"; then
	addlib "-lcsengine"
        addlib "-lcsws"
        addlib "-lcsgfx"
        addlib "-lcsgeom"
        addlib "-lcstool"
        addlib "-lcsutil"
        addlib "-lcssys"
    fi
    the_libs="-L${libdir} $the_libs ${syslibs}"

    echo "$the_libs"
fi

exit 0
