ICL_PC=@PKG_CONFIG_FILE_NAME_NO_PC@

if [ ! "$1" ] ; then
    echo "$1 is the compilation target e.g. test for compiling test.cpp" ;
    exit -1 ;
fi

if [ "$1" = "--help" ] ; then
    echo "icl-make applies the following command:" ;
    echo 'g++ $CXXFLAGS $(pkg-config --cflags ${ICL_PC}) -I. -o ARG_1 ARG_1.[cpp|cc|cxx] $LDFLAGS $(pkg-config --libs ${ICL_PC})' ;
    exit 0 ;
fi

if [ "`pkg-config --exists ${ICL_PC} && echo yes || echo no`" == "no" ] ; then
    echo "${ICL_PC}.pc could not be found in your PKG_CONFIG_PATH" ;
    exit -1 ;
fi

SRC=
if [ -e $1.cpp ] ; then
   SRC=$1.cpp ;
elif [ -e $1.cxx ] ; then
   SRC=$1.cxx ;
elif [ -e $1.cc ] ; then
   SRC=$1.cc ;
else
   echo unable to find source file $1.cpp or $1.cxx or $1.cc
   exit -1;
fi

PFX=@CMAKE_INSTALL_PREFIX@

LDFLAGS="$LDFLAGS $(pkg-config --libs ${ICL_PC} | sed 's|-Wl,-rpath\\|-Wl,-rpath|g')"
LDFLAGS="-L${PFX}/lib -Wl,-rpath=${PFX}/lib ${LDFLAGS}"

CXXFLAGS="$CXXFLAGS $(pkg-config --cflags ${ICL_PC}) -I."

if [ "$2" = "-v" ] || [ "$2" = "--v" ] || [ "$2" = "-verbose" ] || [ "$2" = "--verbose" ] ; then
   echo "g++ $CXXFLAGS -o $1 $SRC $LDFLAGS" ;
fi

g++ $CXXFLAGS -o $1 $SRC $LDFLAGS