#!/bin/sh

X11=-lX11
Xext=-lXext
Xinerama=-lXinerama
Xxf86vm=-lXxf86vm
dl=-ldl

TARGET=oyranos

VERSION=0.1.7
VERSION_L=107
VERSION_A=0
VERSION_B=1
VERSION_C=7

prefix=/usr
exec_prefix=/usr
bindir=/usr/bin
sbindir=/usr/sbin
includedir=/usr/include
datadir=/usr/share
mandir=/usr/share/man
syscolordir=/usr/share/color
usercolordir=~/.color
iccdirname=icc
settingsdirname=settings
pixmapdir=/usr/share/pixmaps
desktopdir=/usr/share/applications
ELEKTRA_VERSION=
oyranos_add="$dl"
oyranos_x_add="$X11_PATH $X11 $Xext $Xinerama"

ELEKTRA_VERSION=`pkg-config --modversion elektra`
export ELEKTRA_VERSION
libdir=`pkg-config x11 --variable libdir`
export libdir

if [ -n "$PKG_CONFIG_PATH" ]; then
  PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$libdir/pkgconfig
else
  PKG_CONFIG_PATH=$libdir/pkgconfig
fi
export PKG_CONFIG_PATH


if [ $# -eq 0 ]; then
  echo "$TARGET"
fi


while test $# -gt 0 
do
    case "$1" in
	-*=*) 
	    optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
	    ;;
	*)
	    optarg=
	    ;;
    esac

    case $1 in
	--version)
	    echo $VERSION
	    ;;
	--num-version)
	    echo $VERSION_L
	    ;;
	--api-version)
	    echo $VERSION_A
	    ;;
	--cflags)
	    echo_cflags=yes
	    ;;
	--ldflags)
	    echo_ldflags=yes
	    ;;
	--ldstaticflags)
	    echo_ldstaticflags=yes
	    ;;
	--ld_x_flags)
	    echo_ld_x_flags=yes
	    ;;
	--ld_x_staticflags)
	    echo_ld_x_staticflags=yes
	    ;;
	--syscolordir)
	    echo_syscolordir=yes
	    ;;
	--usercolordir)
	    echo_usercolordir=yes
	    ;;
	--iccdirname)
	    echo_iccdirname=yes
	    ;;
	--settingsdirname)
	    echo_settingsdirname=yes
	    ;;
	*)
	    echo_help=yes
	    ;;
    esac
    shift
done

if [ "$echo_help" = "yes" ]; then
  echo "usage:"
  echo "  $0 --version           get official version"
  echo "  $0 --api-version       get version of API"
  echo "  $0 --num-version       get version as a simple number"
  echo "  $0 --cflags            compiler flags"
  echo "  $0 --ldflags           dynamic link flags"
  echo "  $0 --ldstaticflags     static linking flags"
  echo "  $0 --ld_x_flags        dynamic link flags with monitor support"
  echo "  $0 --ld_x_staticflags  static link flags with monitor support"
  echo "  $0 --syscolordir       path to system main color directory"
  echo "  $0 --usercolordir      path to users main color directory"
  echo "  $0 --iccdirname        ICC profile directory name"
  echo "  $0 --settingsdirname   Oyranos settings directory name"
fi

if [ -f liboyranos.so ]; then
if [ -z $ELEKTRA_VERSION ]; then
  elektra_dyn="`pkg-config --libs elektra`"
  if [ -f $libdir/libelektra.a ]; then
    elektra="$libdir/libelektra.a"
    if [ -f $libdir/libelektra-filesys.a ]; then
      elektra="$elektra $libdir/libelektra-filesys.a"
    fi
    if [ -f $libdir/elektra/libelektra-filesys.a ]; then
      elektra="$elektra $libdir/elektra/libelektra-filesys.a"
    fi
  else
    elektra="$elektra_dyn"
  fi
else
  if [ -f $ELEKTRA_VERSION/src/libelektra/libelektra.a ]; then
    elektra="$ELEKTRA_VERSION/src/libelektra/libelektra.a $dl $iconv $intl"
  else
    elektra="$libdir/lib${TARGET}_elektra.a $dl $iconv $intl"
  fi
fi
fi

if [ "$echo_cflags" = "yes" ]; then
  echo "-I$prefix/include"
fi

if [ "$echo_ldflags" = "yes" ]; then
  if [ -f liboyranos.so ]; then
    echo "-L. -loyranos $elektra_dyn $oyranos_add"
  else
    pkg-config --libs oyranos
  fi
fi

if [ "$echo_ldstaticflags" = "yes" ]; then
  if [ -f liboyranos.a ]; then
    echo "liboyranos.a $elektra $oyranos_add"
  else
    echo "$libdir/liboyranos.a $elektra $oyranos_add"
  fi
fi

if [ "$echo_ld_x_flags" = "yes" ]; then
  if [ -f liboyranos.so ]; then
    echo "-L. -loyranos -loyranos_moni $elektra_dyn $oyranos_add"
  else
    pkg-config --libs oyranos_monitor
  fi
fi

# only standard paths are here supported
if [ -f $libdir/libX11.so ]; then
  X11_PATH=-L$libdir
fi
if [ -f /usr/local/lib/libX11.so ]; then
  X11_PATH=-L/usr/local/lib
fi
if [ -f /usr/X11R6/lib/libX11.so ]; then
  X11_PATH=-L/usr/X11R6/lib
fi

if [ "$echo_ld_x_staticflags" = "yes" ]; then
  if [ -f liboyranos.a ]; then
    echo "liboyranos.a liboyranos_moni.a $elektra $oyranos_add $oyranos_x_add"
  else
    echo "$libdir/liboyranos.a $libdir/liboyranos_moni.a $elektra $oyranos_add $oyranos_x_add"
  fi
fi

if [ "$echo_syscolordir" = "yes" ]; then
  echo "$syscolordir"
fi
if [ "$echo_usercolordir" = "yes" ]; then
  echo "$usercolordir"
fi
if [ "$echo_iccdirname" = "yes" ]; then
  echo "$iccdirname"
fi
if [ "$echo_settingsdirname" = "yes" ]; then
  echo "$settingsdirname"
fi

