#!/bin/sh
# ff-c++ `./ff-get-dep -ff metis.cpp`
wherelib=WHERE_LIBRARY
dircommand=`dirname $0`
if [   -f "$wherelib-download" ] ; then
    wherelib=WHERE_LIBRARY
elif [   -f "$dircommand/$wherelib-download" ] ; then
    wherelib=$dircommand/WHERE_LIBRARY
elif [ -f '/usr/lib64/freefem++/WHERE_LIBRARY-download' ] ; then
    wherelib='/usr/lib64/freefem++/WHERE_LIBRARY'
else
   echo " error no WHERE_LIBRARY  file "
   exit 1;
fi


case "$1" in
    -i*)
	shift;
	for i in $@ ; do
	#    echo "$1" 
	#    echo 	awk -v p="$1" -v m=INCLUDE ' ($1 == p) && ($2 == m) { for (i=3;i<=NF;++i) {print $i," ";}}' $wherelib  $wherelib-download
         awk -v p="$i" -v m=INCLUDE ' ($1 == p) && ($2 == m) && (!first){ first=1;for (i=3;i<=NF;++i) {print $i," ";}}' "$wherelib"  "$wherelib"-config "$wherelib"-download
	done
	
	;;
    -l*)
	shift;
	for i in $@ ; do   
	    awk -v p="$i" -v  m=LD ' ($1 == p) && ($2 == m) && (!first) {first=1; for (i=3;i<=NF;++i) {print $i," "};} 
          END  { if(!first) print "ERROR-missing-lib:" p;} ' "$wherelib"  "$wherelib"-config "$wherelib"-download 
	done
	;;
    -dlib)
	shift
	awk -F:  '/ff-c[+][+]-LIBRARY-dep/  { print $2}' $@
	;;
    -dcpp)
	shift
	awk -F:  '/ff-c[+][+]-cpp-dep/  { print $2}' $@
	echo $@
	;;
    -ff)
	shift;
	libs=`$0 -dlib $@`
	 cpp=`$0 -dcpp $@`
	 inclins=`$0 -i $libs` 
	 ldlibs=`$0 -l $libs`
	 if [ -n "$ldlibs" -o -z "$libs" ]; then
	     echo "$cpp" "$inclins" "$ldlibs"
	 else
	     echo " erreur find libs of $0 :  $libs " 1>&2
	 fi
	;;
  *)  
	for i in $@ ; do   
	    awk -v p="$i" ' ($1 == p) { k=0;  for (i=3;i<=NF;++i) {k=1;} print k}' "$wherelib"  "$wherelib"-config "$wherelib"-download   
	done
	
shift;;
esac

