#! /bin/sh

staging=${DESTDIR}
prefix="/usr"
exec_prefix="${prefix}"
libdir="${exec_prefix}/lib"
datarootdir="${prefix}/share"
datadir="${datarootdir}"
pkgdatadir="${datadir}/xenomai"
includedir="/usr/include/xenomai"

XENO_VERSION="2.5.5.2"
XENO_PREFIX="${staging}${prefix}"
XENO_CC="gcc"
XENO_TARGET_ARCH="x86"
XENO_BASE_CFLAGS="-I${staging}${includedir} -D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__"
XENO_BASE_LDFLAGS="-L${staging}${libdir} -lxenomai -lpthread "
XENO_POSIX_LDFLAGS="-L${staging}${libdir} -lpthread_rt -lxenomai -lpthread -lrt "
XENO_POSIX_WRAPPERS="${staging}${libdir}/posix.wrappers"
XENO_POSIX_FAST_WRAPPING="yes"
XENO_LIBRARY_DIR="${staging}${libdir}"
XENO_INCLUDE_DIR="${staging}${includedir}"

unset prefix exec_prefix libdir datadir datarootdir pkgdatadir includedir

posix_ldflags ()
{
    if test \! -r $XENO_POSIX_WRAPPERS; then
        echo "POSIX support is not available" >&2
        exit 1
    fi
    if test "$XENO_POSIX_FAST_WRAPPING" = "yes"; then
        echo -n "-Wl,@$XENO_POSIX_WRAPPERS $XENO_POSIX_LDFLAGS"
    else
        wrappers=`while read wrap_option symbol; do \
                      echo -n "-Wl,$wrap_option,$symbol " ; \
                  done < $XENO_POSIX_WRAPPERS`
        echo -n "$wrappers $XENO_POSIX_LDFLAGS"
    fi
}

usage ()
{
cat <<EOF
Usage xeno-config --skin=skinname OPTIONS
Options :
        --help
        --v,--verbose
        --version
        --cc
        --arch
        --prefix
        --skin native|posix|psos|rtai|rtdm|uitron|vrtx|vxworks
        --cflags
        --ldflags
        --lib*-dir,--libdir,--user-libdir

Deprecated options:
        --xeno-cflags
        --xeno-ldflags
        --posix-cflags
        --posix-ldflags
EOF
    exit $1
}

verbose ()
{
    echo xeno-config --verbose
    echo "        --version=\"${XENO_VERSION}\""
    echo "        --cc=\"$XENO_CC\""
    echo "        --arch=\"$XENO_TARGET_ARCH\""
    echo "        --prefix=\"$XENO_PREFIX\""
    echo "        --xeno-cflags=\"$XENO_BASE_CFLAGS\""
    echo "        --xeno-ldflags=\"$XENO_BASE_LDFLAGS\""
    echo "        --posix-cflags=\"$XENO_POSIX_CFLAGS\""
    echo "        --posix-ldflags=\"`posix_ldflags`\""
    echo "        --library-dir=\"$XENO_LIBRARY_DIR\""
}

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

while test $# -gt 0; do
    case "$1" in
        --v|--verbose)
            verbose $*
            exit 0
            ;;
        --version)
            echo ${XENO_VERSION}
            ;;
        --cc)
            echo $XENO_CC
            ;;
        --arch)
            echo $XENO_TARGET_ARCH
            ;;
        --prefix)
            echo $XENO_PREFIX
            ;;
        --lib*-dir|--libdir|--user-libdir)
            echo $XENO_LIBRARY_DIR
            ;;
	--skin)
	    skin="$2"
	    shift
	    ;;
	--skin=*)
	    skin=`expr "$1" : '--skin=\(.*\)'`
	    ;;
	--cflags)
	    case "$skin" in
		native|rtdm)
		    echo $XENO_BASE_CFLAGS
		    ;;
		psos|psos+|posix|rtai|uitron|vrtx|vxworks)
		    if [ "x$skin" = "xpsos" ]; then
			skin="psos+"
		    fi
		    echo $XENO_BASE_CFLAGS -I$XENO_INCLUDE_DIR/$skin
		    ;;
		"")
		    echo skin not set, please pass --skin before --cflags 1>&2
		    exit 1
		    ;;
		*)
		    echo skin $skin unknown 1>&2
		    exit 1
		    ;;
	    esac
	    ;;

	--ldflags)
	    case "$skin" in
		posix)
		    posix_ldflags
		    echo
		    ;;
		native|psos|psos+|rtai|uitron|vrtx|vxworks)
		    if [ "x$skin" = "xpsos+" ]; then
			skin="psos"
		    fi
		    echo -l$skin $XENO_BASE_LDFLAGS
		    ;;
		"")
		    echo skin not set, please pass --skin before --cflags 1>&2
		    exit 1
		    ;;
		*)
		    echo skin $skin unknown 1>&2
		    exit 1
		    ;;
	    esac
	    ;;

        --xeno-cflags)
	    echo "$1" is deprecated, use --skin=name --cflags instead 1>&2 
            echo $XENO_BASE_CFLAGS
            ;;
        --xeno-ldflags)
	    echo "$1" is deprecated, use --skin=name --ldflags instead 1>&2 
            echo $XENO_BASE_LDFLAGS
            ;;
        --posix-cflags)
	    echo "$1" is deprecated, use --skin=posix --cflags instead 1>&2 
	    echo $XENO_BASE_CFLAGS -I$XENO_INCLUDE_DIR/posix
            ;;
        --posix-ldflags)
	    echo "$1" is deprecated, use --skin=posix --ldflags instead 1>&2 
            posix_ldflags
            echo
            ;;
        --help)
            usage 0 1>&2
            ;;
        *)
         usage 1 1>&2
         ;;
    esac
    shift
done
