#! /bin/sh
# ======================================================================
#  $Id: cldaq-config,v 1.4 2003/11/22 13:54:59 goiwai Exp $
#  $Name: CLDAQ-1-11-00 $
#  $Log: cldaq-config,v $
#  Revision 1.4  2003/11/22 13:54:59  goiwai
#  *** empty log message ***
#
#  Revision 1.3  2003/10/06 13:31:43  goiwai
#  cldaq-config
#
#  Revision 1.2  2003/07/30 16:12:50  goiwai
#  ファイルにコミットログをつけることにしました.
#
# ======================================================================
prog="cldaq-config"
projectname="CLDAQ"
rootdir="$CLDAQ_INSTALL"

# 見た目はそれっぽく動いてるけど
# このスクリプトはまだ不完全
# 複数の引数を受けつけなかったりする
# gtk-config を見習いましょう
usage()
{
cat <<EOF
Usage: cldaq-config [OPTIONS]
Options:
	[--prefix[=DIR]]
	[--cflags]
	[--libs]
	[--ldflags]
	[--ldlibs]
	[--version]
EOF
    exit 1
}

emergency()
{
    echo "set and export enviroment variable \"CLDAQ_INSTALL\""
    echo "e.g.) setenv CLDAQ_INSTALL /somewhere/CLDAQ"
    echo "e.g.) export CLDAQ_INSTALL=/somewhere/CLDAQ"
    echo
    echo "CLDAQ_INSTALL as \"$HOME/CLDAQ\"."

    rootdir="$HOME/CLDAQ"
}

ldlibs()
{
    echo "-l$projectname"
}

ldflags()
{
    d="$rootdir/lib"
    echo "-L$d"
}

libs()
{
    r=$(ldflags)
    r="$r $(ldlibs)"
    echo $r
}

cflags()
{
    base="$rootdir/source"
    i=""
    for p in analysis datarecord geometry modules random run commands event interfaces persistency readout system; do
	if [ "$i" = "" ]; then
	    i="-I$base/$p"
	else
	    i="$i -I$base/$p"
	fi
    done
    echo $i
}

version()
{
    h="$rootdir/source/system/Ttypes.h"
    if [ ! -f $h ]; then
	echo "no such a file \"$h\".";
	exit 1
    fi
    v=$(grep '^static const Tstring Tversion = "[0-9]\+\.[0-9]\+\.[0-9]\+";$' $h | cut --delimiter='"' --fields=2)
    echo $v
}

if [ "$CLDAQ_INSTALL" = "" ]; then
    emergency
fi
if [ $# -eq 0 ]; then
    usage
fi

while [ $# -gt 0 ]; do
    case "$1" in
	--prefix=*)
	    rootdir="$(echo $1 | cut --delimiter='=' --fields=2)"
	    shift
	    ;;
	--cflags)
	    cflags
	    shift
	    ;;
	--libs)
	    libs
	    shift
	    ;;
	--ldflags)
	    ldflags
	    shift
	    ;;
	--ldlibs)
	    ldlibs
	    shift
	    ;;
	--version)
	    version
	    shift
	    ;;
	*)
	    usage
	    ;;
    esac
done
exit 0
