#! /bin/sh
# **************************************************************************
# soqt-config -- a utility for retrieving configuration information about
# the installed SoQt library.
#
# Authors:
#   Lars J. Aas <larsa@coin3d.org>
#   Morten Eriksen <mortene@coin3d.org>

program="$0"
me=`echo "$0" | sed 's,^.*[/\\],,g'`
wd=`echo "$0" | sed 's,/[^/]*$,,'`
prefix=`cd "$wd"/..; pwd` 

hello=`echo hello | fmt -t -w70 2>&1`
if test x"$hello" = x"hello"; then
  # 'fmt -t' works
  preformat="fmt -t -w76"
  sedformat='$ q; s/$/ \\/'
else
  # sed script replacing "fmt -w76 | sed '2,$ s/^/  /g; $ q; 1,$ s/$/ \\/'"
  preformat="cat"
  sedformat='/^............................................................................./ {
  G
  s/^\([^\
]\{1,76\}\) \(.*\)\(\
\)/\1 \\\3  \2/
  P
  D
}'
  if test xhello != x"`echo hello | sed -e $sedformat 2>&1`"; then
    sedformat="s/a/a/"
  fi
fi


usage="\
Usage: $me [OPTIONS]
Options:
  --alternate=<string>
  --cppflags
  --cflags
  --cxxflags
  --ldflags
  --libs
  --msvcrt
  --datadir
  --includedir
  --frameworkdir
  --ac-subst <string> | <sourcefile.in>
  --ac-desubst <sourcefile>
  --compile <sourcefile> [<sourcefile>]...
  --link <program> <object> [<object>]...
  --build <program> <sourcefile> [<sourcefile>]...
  --version
  --usage | --help"

alternate=default
for arg
do
  case $arg in
  --alternate=*) alternate=`echo $arg | cut -d= -f2-` ;;
  --debug) alternate=debug ;;
  esac
done

if test -f $prefix/share/Coin/conf/soqt-$alternate.cfg; then
  configfile="$prefix/share/Coin/conf/soqt-$alternate.cfg"
  . $configfile
elif test -f $prefix/share/Coin/conf/soqt-default.cfg; then
  configfile="$prefix/share/Coin/conf/soqt-default.cfg"
  . $configfile
else
  echo >&2 "$me: no default SoQt config available in $prefix/share/Coin/conf/"
  exit 1
fi

while test $# -gt 0
do
  case $1 in
    --usage | --help)  echo "$usage"         ;;
    --version)         echo "$version"       ;;
    --cppflags)        echo "$cppflags"      ;;
    --cflags)          echo "$cflags"        ;;
    --cxxflags)        echo "$cxxflags"      ;;
    --ldflags)         echo "$ldflags"       ;;
    --libs)            echo "$libs"          ;;
    --msvcrt)          echo "$msvcrt"        ;;
    --datadir)         echo "$datadir"       ;;
    --includedir)      echo "$includedir"    ;;
    --frameworkdir)    echo "$frameworkdir"  ;;
    --ac-subst)
      shift
      if test $# -eq 1; then
        case $1 in
          *\.in)
            echo $me --ac-subst $1
            outfile=`echo $1 | sed -e 's/\.in$//'`
            sedscript=/tmp/ac_subst_$$.sed
            grep "^AC_SUBST_" $configfile \
              | sed -e 's/^AC_SUBST_\([^=]*\)="\([^"]*\)"$/s\/@\1@\/\2\/g;/g' \
              >$sedscript
            rm -f $outfile
            case `echo "$compiler" | sed 's/ .*//g'` in
            gcc | g++)
              echo "# 1 \"$1\"" >$outfile
              ;;
            wrapmsvc | wrapmsvc.exe | msvccc)
              echo "#line 1 \"$1\"" >$outfile
              ;;
            esac
            sed -f $sedscript <$1 >>$outfile
            rm -f $sedscript
            ;;
          *)
            eval "VAL=\${AC_SUBST_$1}"
            echo "$VAL"
            ;;
        esac
      else
        for arg in $@; do
          $program --alternate="$alternate" --ac-subst $arg
          retval=$?
          test $retval -ne 0 && exit $retval
        done
      fi
      exit 0
      ;;
    --ac-desubst)
      shift
      echo $me --ac-desubst $1
      outfile="$1.in"
      sedscript=/tmp/ac_subst_$$.sed
      grep "^AC_SUBST_" $configfile \
        | sed -e 's/^AC_SUBST_\([^=]*\)="\([^"]*\)"$/s\/\2\/@\1@\/g;/g' \
        >$sedscript
      sed -f $sedscript <$1 >$outfile
      rm -f $sedscript
      ;;
    --compile)
      shift
      if test $# -eq 1; then
        case $1 in
          *\.in)
            outfile=`echo $1 | sed -e 's/\.in$//'`
            $program --alternate="$alternate" --ac-subst $1
            $program --alternate="$alternate" --compile $outfile
            retval=$?
            test $retval -ne 0 && exit $retval
            ;;
          *)
            basename=`echo "$1" | sed -e 's/\.[^\.]*$//'`  # strip off extension
            echo $compiler $CPPFLAGS $cppflags $CXXFLAGS $cxxflags -c $1 -o $basename.o \
              | $preformat | sed "$sedformat"
            $compiler \
              $CPPFLAGS $cppflags \
              $CXXFLAGS $cxxflags \
              -c $1 -o $basename.o
            retval=$?
            test $retval -ne 0 && exit $retval
            ;;
        esac
      else
        for arg in $@; do
          $program --alternate="$alternate" --compile $arg
          retval=$?
          test $retval -ne 0 && exit $retval
        done
      fi
      exit 0
      ;;
    --link)
      shift
      progname=$1
      shift
      echo $compiler $LDFLAGS $ldflags -o $progname $@ $libs $LIBS \
        | $preformat | sed "$sedformat"
      $compiler \
        $LDFLAGS $ldflags \
        -o $progname $@ \
        $libs $LIBS
      retval=$?
      test $retval -ne 0 && exit $retval
      case $host in
      *-darwin*)
        echo "$me: making Mac OS X application"
        mkdir $progname.app 2>/dev/null
        mkdir $progname.app/Contents 2>/dev/null
        mkdir $progname.app/Contents/MacOS 2>/dev/null
        if test -d $progname.app/Contents/MacOS &&
          mv -f $progname $progname.app/Contents/MacOS/ &&
          echo "APPL????" >$progname.app/Contents/PkgInfo; then
          :
        else
          echo >&2 "$me: failed to make Mac OS X application"
          echo >&2 " - problem is probably user permission or disk space related"
          exit 1
        fi
        ;;
      esac
      exit 0
      ;;
    --build)
      shift
      progname=$1
      objs=
      extraldflags=
      extralibs=
      shift
      for arg in $@; do
        case $arg in
          *.h.in)
            $program --alternate="$alternate" --ac-subst $arg
            ;;
          *.c.in | *.cpp.in | *.cxx.in | *.cc.in)
            outfile=`echo "$arg" | sed -e 's/\.in$//'`
            basename=`echo "$outfile" | sed -e 's/\.[^\.]*$//'`  # strip off extension
            $program --alternate="$alternate" --ac-subst $arg
            $program --alternate="$alternate" --compile $outfile
            retval=$?
            test $retval -ne 0 && exit $retval
            objs="$objs $basename.o"
            ;;
          *.c | *.cpp | *.cxx | *.cc)
            basename=`echo "$arg" | sed -e 's/\.[^\.]*$//'`  # strip off extension
            $program --alternate="$alternate" --compile $arg
            retval=$?
            test $retval -ne 0 && exit $retval
            objs="$objs $basename.o"
            ;;
          -L*)
            extraldflags="$extraldflags $arg"
            ;;
          -l*)
            extralibs="$extralibs $arg"
            ;;
        esac
      done
      if test -z "$objs"; then
        echo >&2 "$usage"
        echo >&2 ""
        echo >&2 "$me: no object files"
        exit 1
      fi
      echo $compiler $CPPFLAGS $cppflags $CXXFLAGS $cxxflags \
        $LDFLAGS $ldflags $extraldflags \
        -o $progname $objs $extralibs $libs $LIBS \
        | $preformat | sed "$sedformat"
      $compiler \
        $CPPFLAGS $cppflags \
        $CXXFLAGS $cxxflags \
        $LDFLAGS $ldflags $extraldflags \
        -o $progname $objs \
        $extralibs $libs $LIBS
      retval=$?
      test $retval -ne 0 && exit $retval
      case $host in
      *-darwin*)
        echo "$me: making Mac OS X application"
        mkdir $progname.app 2>/dev/null
        mkdir $progname.app/Contents 2>/dev/null
        mkdir $progname.app/Contents/MacOS 2>/dev/null
        if test -d $progname.app/Contents/MacOS &&
          mv -f $progname $progname.app/Contents/MacOS/ &&
          echo "APPL????" >$progname.app/Contents/PkgInfo; then
          :
        else
          echo >&2 "$me: failed to make Mac OS X application"
          echo >&2 " - problem is probably user permission or disk space related"
          exit 1
        fi
        ;;
      esac
      exit 0
      ;;
    # ignore some options
    --alternate=*) ;;
    --debug) ;;
    # args not catched until now are invalid
    *)
      echo >&2 "$me: Invalid option: \"$1\""
      echo >&2 "$usage"
      exit 1
      ;;
  esac
  shift
done

exit 0
