#
# Copyright (C) 2006-2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=strace

ifeq ($(CONFIG_PACKAGE_STRACE_VERSION),"4.5.20")
PKG_VERSION:=4.5.20
PKG_RELEASE:=1
PKG_MD5SUM:=64dfe10d9db0c1e34030891695ffca4b

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2
PKG_SOURCE_URL:=@SF/$(PKG_NAME)

PKG_FIXUP:=autoreconf
PKG_INSTALL:=1
else
PKG_VERSION:=4.7
PKG_RELEASE:=1
PKG_MD5SUM:=6054c3880a00c6703f83b57f15e04642

USE_SOURCE_DIR:=./src
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=@SF/$(PKG_NAME)
PATCH_DIR:=./patches-4.7

PKG_LICENSE:=BSD-3c
PKG_LICENSE_FILES:=COPYRIGHT

PKG_FIXUP:=autoreconf
endif

include $(INCLUDE_DIR)/package.mk

define Package/strace
  SECTION:=TP-LINK
  CATEGORY:=TP-LINK iplatform apps
  SUBMENU:=Debug
  TITLE:=System call tracer
  URL:=http://strace.sourceforge.net/
endef

define Package/strace/description
A useful diagnostic, instructional, and debugging tool. Allows you to track what
system calls a program makes while it is running.
endef

define Package/strace/config
  config PACKAGE_STRACE_VERSION
    string "package strace compile version, 4.7, 4.5.20, 4.5.16"
    depends PACKAGE_strace
    default "4.7"
    help
      BCM6756 should choose 4.5.20
endef

# needed when compiling against Linux 2.4
CONFIGURE_VARS += \
	ac_cv_header_linux_netlink_h=yes

ifeq ($(CONFIG_PACKAGE_STRACE_VERSION),"4.5.20")

MAKE_FLAGS := \
 CCOPT="$(TARGET_CFLAGS)"

define Package/strace/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/strace $(1)/usr/bin/
endef

else

ifneq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  TARGET_CPPFLAGS += -I$(LINUX_DIR)/include
endif

MAKE_FLAGS := \
	CCOPT="$(TARGET_CFLAGS)"

define Build/Configure
	[ -f $(STAMP_CONFIGURED) ] || { \
		rm -f $(PKG_BUILD_DIR)/*.so.* $(PKG_BUILD_DIR)/*.a; \
		find $(PKG_BUILD_DIR) -name \*.o | xargs rm -f; \
	}
	cd $(PKG_BUILD_DIR); \
		./configure --target=$(ARCH)-openwrt-linux --host=$(ARCH)-openwrt-linux --build=$(OPTIMIZE_FOR_CPU)-linux-gnu --program-prefix="" --program-suffix="" --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/lib --sysconfdir=/etc --datadir=/usr/share --localstatedir=/var --mandir=/usr/man --infodir=/usr/info --disable-nls
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)
endef

define Build/Compile
    $(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS) LDFLAGS="$(TARGET_LDFLAGS)" CFLAGS="$(TARGET_CFLAGS)" CPPFLAGS="$(TARGET_CPPFLAGS)"
endef

define Package/strace/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/strace $(1)/usr/bin/
endef
endif

$(eval $(call BuildPackage,strace))
