#!/bin/sh

prefix="/usr/"

# Guess the prefix from the shell script's path if not set
if test "x$prefix" = "x"; then
    prefix=`echo $0 | sed -e 's|//*[^/]*$||' -e 's|//*[^/]*$||'`
fi

system_win=no
system_unix=yes

have_glut=yes
have_qt=yes
have_qt4=no
have_tif=no
have_jpg=yes
have_jasper=yes
have_png=yes
have_imf=no
have_mng=yes
have_gif=no  
have_freetype1=no
have_text=yes
have_sepia=no

lib_arch=

if test "$system_win" = "yes"; then
	prefix=`cygpath -w $prefix`
fi

exec_prefix=${prefix}
exec_prefix_set=no

if test "$system_win" = "yes"; then
    linkdir_pre="-LIBPATH:\""
    linkdir_suf="\""

    incdir_pre="-I\""
    incdir_suf="\""

    link_pre=
    link_suf=.lib
else
    linkdir_pre=-L
    linkdir_suf=

    incdir_pre=-I
    incdir_suf=

    link_pre=-l
    link_suf=
fi

link_flags=""
link_flags_opt="-O2 "
link_flags_dbg="-g "

inc_add_path=''
lib_add_path=''

gl_libs="-lGLU -lGL -lXmu -lXi -lXt -lX11"
lib_gl_path=
inc_gl_path=

win_sys_libs=$gl_libs
unix_sys_libs=$gl_libs

sepia_libs=""
lib_sepia_path=
inc_sepia_path=

glut_libs="-lglut"
lib_glut_path=
inc_glut_path=

qt_libs="-lqt-mt"
lib_qt_path=/usr//lib
inc_qt_path=/usr/include/qt3
cflags_qt=""

qt4_libs=""
lib_qt4_path=
inc_qt4_path=
cflags_qt4=""

tif_libs=
lib_tif_path=
inc_tif_path=

jpg_libs=-ljpeg
lib_jpg_path=
inc_jpg_path=

jasper_libs=-ljasper
lib_jasper_path=
inc_jasper_path=

png_libs="-lpng -lz"
lib_png_path=
inc_png_path=

imf_libs=
lib_imf_path=
inc_imf_path=

mng_libs=-lmng
lib_mng_path=
inc_mng_path=

gif_libs=
lib_gif_path=
inc_gif_path=

freetype1_libs=
lib_freetype1_path=
inc_freetype1_path=

text_libs="-lfreetype -lfontconfig"
lib_text_path=
inc_text_path=/usr/include/freetype2

stlport_libs=
lib_stlport_path=
inc_stlport_path=

stlport_defs=

image_libs=
image_libs_path=
image_incs_path=
image_defs=

if test "$system_win" = "yes"; then
    compiler_libs_in=
    sys_libs_in=""

    sys_libs=" $win_sys_libs"

    if test "$compiler_libs_in"X != X; then 
        sys_libs=-LIBPATH:'"'$compiler_libs_in'" '$sys_libs 
    fi

    if test "$sys_libs_in"X != X; then 
        sys_libs=-LIBPATH:'"'$sys_libs_in'" '$sys_libs 
    fi
else
    sys_libs=$unix_sys_libs
fi;

if test "yes$inc_gl_path" != "yes"; then
    inc_gl_path="$incdir_pre$inc_gl_path$incdir_suf"
fi

if test "yes$lib_gl_path" != "yes"; then
    lib_gl_path="$linkdir_pre$lib_gl_path$linkdir_suf"
fi

if test "$stlport_libs"X != X; then
    stl_libs=$stlport_libs

    if test "yes$lib_stlport_path" != "yes"; then
        lib_stl_path="$linkdir_pre$lib_stlport_path$linkdir_suf"
    fi
    if test "yes$inc_stlport_path" != "yes"; then
        inc_stl_path="$incdir_pre$inc_stlport_path$incdir_suf"
    fi
    stlport_defs="-DOSG_WITH_STLPORT"
fi


if test "$have_tif" = "yes"; then
    image_libs="$image_libs $tif_libs"

    if test "yes$lib_tif_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_tif_path$linkdir_suf"
    fi
    if test "yes$inc_tif_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_tif_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_TIF"
fi
if test "$have_jpg" = "yes"; then
    image_libs="$image_libs $jpg_libs"
    if test "yes$lib_jpg_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_jpg_path$linkdir_suf"
    fi
    if test "yes$inc_jpg_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_jpg_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_JPG"
fi
if test "$have_jasper" = "yes"; then
    image_libs="$image_libs $jasper_libs"
    if test "yes$lib_jasper_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_jasper_path$linkdir_suf"
    fi
    if test "yes$inc_jasper_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_jasper_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_JASPER"
fi
if test "$have_png" = "yes"; then
    image_libs="$image_libs $png_libs"
    if test "yes$lib_png_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_png_path$linkdir_suf"
    fi
    if test "yes$inc_png_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_png_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_PNG"
fi
if test "$have_imf" = "yes"; then
    image_libs="$image_libs $imf_libs"
    if test "yes$lib_ifm_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_imf_path$linkdir_suf"
    fi
    if test "yes$inc_imf_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_imf_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_IMF"
fi
if test "$have_mng" = "yes"; then
    image_libs="$image_libs $mng_libs"
    if test "yes$lib_mng_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_mng_path$linkdir_suf"
    fi
    if test "yes$inc_mng_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_mng_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_MNG"
fi
if test "$have_gif" = "yes"; then
    image_libs="$image_libs $gif_libs"
    if test "yes$lib_gif_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_gif_path$linkdir_suf"
    fi
    if test "yes$inc_gif_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_gif_path$incdir_suf"
    fi
    image_defs="$image_defs -DOSG_WITH_GIF"
fi
if test "$have_freetype1" = "yes"; then
    image_libs="$image_libs $freetype1_libs"

    if test "yes$lib_freetype1_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_freetype1_path$linkdir_suf"
    fi
    if test "yes$inc_freetype1_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_freetype1_path$incdir_suf"
    fi
#    image_defs="$image_defs -DOSG_WITH_FREETYPE1"
fi
if test "$have_sepia" = "yes"; then
    sepia_libs="$sepia_libs $glut_libs"
    if test "yes$lib_sepia_path" != "yes"; then
        sepia_libs_path="$linkdir_pre$lib_sepia_path$linkdir_suf $lib_glut_path"
    fi
    if test "yes$inc_sepia_path" != "yes"; then
        sepia_incs_path="$incdir_pre$inc_sepia_path$incdir_suf @inc_glut_path"
    fi
    sepia_defs="-DOSG_WITH_SEPIA"
fi
if test "$have_text" = "yes"; then
    image_libs="$image_libs $text_libs"

    if test "yes$lib_text_path" != "yes"; then
        image_libs_path="$image_libs_path $linkdir_pre$lib_text_path$linkdir_suf"
    fi
    if test "yes$inc_text_path" != "yes"; then
        image_incs_path="$image_incs_path $incdir_pre$inc_text_path$incdir_suf"
    fi
fi

usage()
{
    cat <<EOF
Usage: osg-config OPTIONS [LIBRARIES]
Options:
    [--prefix[=DIR]]
    [--exec-prefix[=DIR]]
    [--version]
    [--compiler]
    [--compiler-id]
    [--moc]
    [--uic]
    [--moc4]
    [--uic4]
    [--exe-linker]
    [--libs]
    [--cflags]
    [--lflags]
    [--llibs]
    [--opt]
    [--dbg]
Libraries:
    Base
    System
    Win32
    X
    GLUT
    QT
    QT4
    Sepia
    Contrib
EOF
    exit $1
}

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

lib_base=yes

valid_print=no
lib_suf=

if test "$system_win" = "yes"; then
    dbg_char=D
else
    dbg_char=
fi

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

  case $1 in
    --prefix=*)
      prefix=$optarg
      if test $exec_prefix_set = no ; then
        exec_prefix=$optarg
      fi
      ;;
    --prefix)
      echo_prefix=yes
      valid_print=yes
      ;;
    --exec-prefix=*)
      exec_prefix=$optarg
      exec_prefix_set=yes
      ;;
    --exec-prefix)
      echo_exec_prefix=yes
      valid_print=yes
      ;;
    --version)
      echo 1.8.0
      valid_print=yes
      ;;
    --compiler)
      echo "g++"
      exit 0
      ;;
    --compiler-id)
	  echo g++
	  exit 0
	 ;;
    --moc)
      echo "/usr//bin/moc"
	  exit 0
	  ;;
    --uic)
      echo "/usr//bin/uic"
	  exit 0
	  ;;
    --moc4)
      echo "moc"
          exit 0
          ;;
    --uic4)
      echo "uic"
          exit 0
          ;;
    --exe-linker)
      echo "g++"
	  exit 0
	  ;;
    --exe-compiler-shared)
      echo "g++ -shared"
	  exit 0
	  ;;
    --cflags)
      echo_cflags=yes
      valid_print=yes
      ;;
    --lflags)
      echo_lflags=yes
      valid_print=yes
      ;;
    --llibs)
      echo_llibs=yes
      valid_print=yes
      ;;
    --libs)
      echo_libs=yes
      valid_print=yes
      ;;
    --opt)
      lib_suf=
      dbg_char=
      ;;
    --dbg)
      lib_suf=
      if test "$system_win" = "yes"; then
        dbg_char=D
      fi
      ;;
    Base)
      lib_Base=yes
      ;;
    System)
      lib_Base=yes
      lib_System=yes
      ;;
    Win32)
      lib_Base=yes
      lib_System=yes
      lib_Win32=yes
      ;;
    X)
      lib_Base=yes
      lib_System=yes
      lib_X=yes
      ;;
    GLUT)
      lib_Base=yes
      lib_System=yes
      lib_GLUT=yes
      ;;
    QT)
      lib_Base=yes
      lib_System=yes
      if test "$system_win" = "yes"; then
        lib_Win32=yes
      else
        lib_X=yes
      fi;
      lib_QT=yes
      ;;
    QT4)
      lib_Base=yes
      lib_System=yes
      if test "$system_win" = "yes"; then
        lib_Win32=yes
      else
        lib_X=yes
      fi;
      lib_QT4=yes
      ;;

    Sepia)
      lib_Base=yes
      lib_System=yes
      lib_Sepia=yes
      ;;
    Contrib)
      lib_Base=yes
      lib_System=yes
      lib_Contrib=yes
      ;;
    *)
      usage 1 1>&2
      ;;
  esac
  shift
done

if test "$valid_print" = "no"; then
    usage 1 1>&2
    exit
fi;

if test "$echo_prefix" = "yes"; then
    echo $prefix
fi

if test "$echo_exec_prefix" = "yes"; then
    echo $exec_prefix
fi

opensg_libs=
system_libs=

base_libs="-lpthread -ldl -lm  -L/usr/X11R6/lib64 "
#"-lpthread -ldl -lm"

system_libs_path=
system_incs_path=
system_defs=

sys_incl_dir_in=""
if test "yes$sys_incl_dir_in" != "yes"; then
	system_incs_path="$system_incs_path $incdir_pre$sys_incl_dir_in$incdir_suf"
fi

if test "$lib_suf" = "dbg"; then
    link_flags="$link_flags $link_flags_dbg"
else
    link_flags="$link_flags $link_flags_opt"
fi

sys_cflags="-D_GNU_SOURCE -DQT_CLEAN_NAMESPACE -DOSG_WITH_GLUT -DOSG_WITH_QT -DOSG_WITH_JPG -DOSG_WITH_JASPER -DOSG_WITH_PNG -DOSG_WITH_MNG -D_OSG_HAVE_CONFIGURED_H_  -DQT_NO_XINERAMA -DQT_NO_XRENDER -DQT_NO_XFTFREETYPE -DQT_NO_XKB -DQT_NO_SM_SUPPORT -DQT_NO_IMAGEIO_MNG -DQT_NO_IMAGEIO_JPEG -DQT_NO_STYLE_AQUA -DQT_NO_STYLE_MAC -DQT_NO_STYLE_INTERLACE -DQT_NO_STYLE_COMPACT -ansi  -use_readonly_const -ftemplate-depth-100 -fPIC "
sys_cflags_dbg="-g -DOSG_DEBUG"
sys_cflags_opt="-O2"

if test "$lib_suf" = "dbg"; then
    sys_cflags="$sys_cflags $sys_cflags_dbg"
else
    sys_cflags="$sys_cflags $sys_cflags_opt"
fi

if test "$lib_QT" = "yes"; then
    if test "$have_qt" = "yes"; then
        opensg_libs="$opensg_libs ${link_pre}OSGWindowQT${dbg_char}${link_suf}"
        system_libs="$system_libs $qt_libs"
        if test "yes$lib_qt_path" != "yes"; then
            system_libs_path="$system_libs_path $linkdir_pre$lib_qt_path$linkdir_suf"
        fi
        if test "yes$inc_qt_path" != "yes"; then
            system_incs_path="$system_incs_path $incdir_pre$inc_qt_path$incdir_suf"
        fi
        system_defs="$system_defs -DOSG_WITH_QT $cflags_qt"
    else
        echo "Warning QT not configured, ignoring request"
    fi;
fi;

if test "$lib_QT4" = "yes"; then
    if test "$have_qt4" = "yes"; then
        opensg_libs="$opensg_libs ${link_pre}OSGWindowQT4${dbg_char}${link_suf}"
        system_libs="$system_libs $qt4_libs"
        if test "yes$lib_qt4_path" != "yes"; then
            system_libs_path="$system_libs_path $linkdir_pre$lib_qt4_path$linkdir_suf"
        fi
        if test "yes$inc_qt4_path" != "yes"; then
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path$incdir_suf"
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path/Qt3Support$incdir_suf"
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path/Qt$incdir_suf"
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path/QtCore$incdir_suf"
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path/QtGui$incdir_suf"
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path/QtOpenGL$incdir_suf"
            system_incs_path="$system_incs_path $incdir_pre$inc_qt4_path/QtXml$incdir_suf"
        fi
        system_defs="$system_defs -DOSG_WITH_QT -DQT3_SUPPORT -DQT_THREAD_SUPPORT $cflags_qt4"
    else
        echo "Warning QT4 not configured, ignoring request"
    fi;
fi;

if test "$lib_GLUT" = "yes"; then
    if test "$have_glut" = "yes"; then
        opensg_libs="$opensg_libs ${link_pre}OSGWindowGLUT${dbg_char}${link_suf}"
        system_libs="$system_libs $glut_libs"
        if test "yes$lib_glut_path" != "yes"; then
            system_libs_path="$system_libs_path $linkdir_pre$lib_glut_path$linkdir_suf"
        fi
        if test "yes$inc_glut_path" != "yes"; then
            system_incs_path="$system_incs_path $incdir_pre$inc_glut_path$incdir_suf"
        fi
        system_defs="$system_defs -DOSG_WITH_GLUT"
    else
        echo "Warning GLUT not configured, ignoring request"
    fi;
fi;

if test "$lib_Sepia" = "yes"; then
    if test "$have_sepia" = "yes"; then
        sepia_libs="$sepia_libs"
        system_libs="$system_libs $sepia_libs"
        if test "yes$lib_sepia_path" != "yes"; then
            system_libs_path="$system_libs_path $linkdir_pre$lib_sepia_path$linkdir_suf"
        fi
        if test "yes$inc_sepia_path" != "yes"; then
            system_incs_path="$system_incs_path $incdir_pre$inc_sepia_path$incdir_suf"
        fi
        system_defs="$system_defs -DOSG_WITH_SEPIA"
    else
        echo "Warning SEPIA not configured, ignoring request"
    fi;
fi;

if test "$lib_X" = "yes"; then
    if test "$system_unix" = "yes"; then
        opensg_libs="$opensg_libs ${link_pre}OSGWindowX${dbg_char}${link_suf}"
    else
        echo "Warning X not available on win32 plattforms, ignoring request"
    fi;
fi;

if test "$lib_Win32" = "yes"; then
    if test "$system_win" = "yes"; then
        opensg_libs="$opensg_libs ${link_pre}OSGWindowWin32${dbg_char}${link_suf}"
    else
        echo "Warning Win32 not available on unix plattforms, ignoring request"
    fi;
fi;

img_libs_path=
img_incs_path=
img_libs=
img_defs=
sys_defs=

if test "$lib_Contrib" = "yes"; then
    opensg_libs="$opensg_libs ${link_pre}OSGContrib${dbg_char}${link_suf}"
    system_libs="$system_libs $base_libs"
fi;

if test "$lib_System" = "yes"; then
    opensg_libs="$opensg_libs ${link_pre}OSGSystem${dbg_char}${link_suf}"
    system_libs="$system_libs $sys_libs"
    img_libs_path=$image_libs_path
    img_incs_path=$image_incs_path
    img_libs=$image_libs
    img_defs=$image_defs
    sys_defs=$system_defs
fi;

if test "$lib_Base" = "yes"; then
    opensg_libs="$opensg_libs ${link_pre}OSGBase${dbg_char}${link_suf}"
    system_libs="$system_libs $base_libs"
fi;


if test "$system_win" = "yes"; then
    opensg_lib_path=${linkdir_pre}${exec_prefix}/lib${linkdir_suf}
else
    opensg_lib_path=${linkdir_pre}${exec_prefix}/lib${lib_arch}/${lib_suf}${linkdir_suf}
fi

if test "$echo_lflags" = "yes"; then

    echo $link_flags $img_libs_path $system_libs_path $lib_stl_path $lib_gl_path $opensg_lib_path $lib_add_path

fi      

if test "$echo_llibs" = "yes"; then

    echo $opensg_libs $img_libs $system_libs $stl_libs

fi      

if test "$echo_libs" = "yes"; then

    echo $link_flags $img_libs_path $system_libs_path $lib_stl_path $lib_gl_path $opensg_lib_path $lib_add_path $opensg_libs $img_libs $system_libs $stl_libs

fi      

if test "$echo_cflags" = "yes"; then

    opensg_inc_path=${incdir_pre}${prefix}/include${incdir_suf}

    echo $sys_cflags $sys_defs $stlport_defs $inc_stl_path $img_defs $img_incs_path $system_incs_path $opensg_inc_path $inc_gl_path $inc_add_path

fi




