#
# Autoconf macros fixing various ROS issues
#

# --- AG_FIX_ROS_LIBS(variable) --------------------------------------------
#
# - work out the rpath for -Ldirs found in ros .pc
# - clean up -l: syntax
#
AC_DEFUN([AG_FIX_ROS_LIBS],[
  ros_libs=
  for arg in [$]$1; do
    case "$arg" in
      -l:/*/lib*.so*|/*/lib*.so*)
        name=${arg##*/lib}; name=${name%.so*}
        path=${arg#-l:}; path=${path%/lib*.so*}
        arg="-L$path -R$path -l$name";;
      -l:*) AC_MSG_FAILURE([unsupported flag $arg in ROS .pc files]);;

      -L*) arg="$arg -R${arg#-L}";;
    esac
    for a in $arg; do
      case " $ros_libs " in
        *" $a "*) ;;
        *) ros_libs="$ros_libs $a";;
      esac
    done
  done
  $1=${ros_libs# }
])

# --- AG_ROS_LDFLAGS(roslib) -----------------------------------------------
#
# check if ros libraries have RUNPATH/RPATH - otherwise, we need to
# --disable-new-dtags to make sure to create binaries with RPATH and not
# RUNPATH in order to resolve indirect ros dependencies.
AC_DEFUN([AG_ROS_LDFLAGS],[
  AC_CHECK_TOOL([READELF], [readelf], [:])

  # check runpath
  AC_CACHE_CHECK([if ROS libraries have RUNPATH], [ag_cv_sys_ros_runpath],[
    ag_cv_sys_ros_runpath=no
    roslib="$1"
    rospath=.
    rosdynsec=
    for arg in $ROS_LIBS; do
      case "$arg" in
        -L*) rospath="$rospath ${arg#-L}";;
        /*$roslib.so)
          if test -r "$arg"; then
            rosdynsec=$($READELF -d $arg)
            break
          fi;;
        -l$roslib)
          for p in $rospath; do
            if test -r "$p/lib$roslib.so"; then
              rosdynsec=$($READELF -d "$p/lib$roslib.so" ||:)
              break 2
            fi
          done;;
      esac
    done
    case "$rosdynsec" in
      *\(RUNPATH\)*|*\(RPATH\)*) ag_cv_sys_ros_runpath=yes;;
    esac])

  # check --disable-new-dtags
  if test "$ag_cv_sys_ros_runpath" != "yes"; then
    LT_PATH_LD

    AC_CACHE_CHECK([$LD accepts --disable-new-dtags], [ag_cv_sys_ld_dtags],[
      ag_cv_sys_ld_dtags=no
      case "`$LD --help 2>&1`" in *--disable-new-dtags*)
        ag_cv_sys_ld_dtags=yes
      esac])

    if test "$ag_cv_sys_ld_dtags" = "yes"; then
      AC_MSG_NOTICE([forcing the linker to use RPATH to link with ROS])
      AC_SUBST(ROS_LDFLAGS,
        [${ROS_LDFLAGS:+$ROS_LDFLAGS }-Wl,--disable-new-dtags])
    fi
  fi
])


# --- AG_ROS_PKGPREFIX(pkg) ------------------------------------------------
#
# check for installation prefix
AC_DEFUN([AG_ROS_PKGPREFIX],[
  _PKG_CONFIG([$1], [variable=prefix], [$2])
  AC_SUBST([$1], [$pkg_cv_[$1]])
])


# --- AG_ROS_PYPKG(pkg ...) ------------------------------------------------
#
# check if ros python packages are present
AC_DEFUN([AG_ROS_PYPKG],[
  AM_PATH_PYTHON([2.7])
  AC_ARG_VAR([PYTHONPATH], [directories to search for python packages])
  for pkg in $@; do
    AC_MSG_CHECKING([python package $pkg])
    if `$PYTHON -c "import $pkg" >/dev/null 2>&1`; then
      AC_MSG_RESULT([yes])
    else
       AC_MSG_RESULT([no])
       errs=`$PYTHON -c "import $pkg" 2>&1`
       AC_MSG_ERROR([
$errs

Python package $pkg was not found in the search path.
Consider adjusting PYTHONPATH and/or PYTHON. Current values are shown below:
PYTHON=$PYTHON
PYTHONPATH=$PYTHONPATH])
    fi
  done
])
