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

include $(TOPDIR)/rules.mk

PKG_NAME:=fastd
PKG_VERSION:=18
PKG_RELEASE:=1

PKG_MAINTAINER:=Matthias Schiffer <mschiffer@universe-factory.net>
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=https://projects.universe-factory.net/attachments/download/86
PKG_MD5SUM:=e53236d3049f64f7955ad9556da099eb

PKG_LICENSE:=BSD-2-Clause
PKG_LICENSE_FILES:=COPYRIGHT

PKG_CONFIG_DEPENDS:=\
	CONFIG_FASTD_ENABLE_METHOD_CIPHER_TEST \
	CONFIG_FASTD_ENABLE_METHOD_COMPOSED_GMAC \
	CONFIG_FASTD_ENABLE_METHOD_COMPOSED_UMAC \
	CONFIG_FASTD_ENABLE_METHOD_GENERIC_GMAC \
	CONFIG_FASTD_ENABLE_METHOD_GENERIC_POLY1305 \
	CONFIG_FASTD_ENABLE_METHOD_GENERIC_UMAC \
	CONFIG_FASTD_ENABLE_METHOD_NULL \
	CONFIG_FASTD_ENABLE_METHOD_XSALSA20_POLY1305 \
	CONFIG_FASTD_ENABLE_CIPHER_AES128_CTR \
	CONFIG_FASTD_ENABLE_CIPHER_NULL \
	CONFIG_FASTD_ENABLE_CIPHER_SALSA20 \
	CONFIG_FASTD_ENABLE_CIPHER_SALSA2012 \
	CONFIG_FASTD_ENABLE_MAC_GHASH \
	CONFIG_FASTD_ENABLE_MAC_UHASH \
	CONFIG_FASTD_WITH_CAPABILITIES \
	CONFIG_FASTD_WITH_CMDLINE_USER \
	CONFIG_FASTD_WITH_CMDLINE_LOGGING \
	CONFIG_FASTD_WITH_CMDLINE_OPERATION \
	CONFIG_FASTD_WITH_CMDLINE_COMMANDS \
	CONFIG_FASTD_WITH_DYNAMIC_PEERS \
	CONFIG_FASTD_WITH_STATUS_SOCKET


PKG_BUILD_DEPENDS:=nacl libuecc

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/fastd
  SECTION:=net
  CATEGORY:=Network
  DEPENDS:=+kmod-tun +librt +libpthread +FASTD_WITH_STATUS_SOCKET:libjson-c +FASTD_WITH_CAPABILITIES:libcap
  TITLE:=Fast and Secure Tunneling Daemon
  URL:=https://projects.universe-factory.net/projects/fastd
  SUBMENU:=VPN
endef

define Package/fastd/config
  source "$(SOURCE)/Config.in"
endef

TARGET_CFLAGS += -ffunction-sections -fdata-sections
TARGET_LDFLAGS += -Wl,--gc-sections

CMAKE_OPTIONS += \
	-DCMAKE_BUILD_TYPE:STRING=MINSIZEREL \
	-DWITH_METHOD_CIPHER_TEST:BOOL=FALSE \
	-DWITH_METHOD_COMPOSED_GMAC:BOOL=FALSE \
	-DWITH_METHOD_COMPOSED_UMAC:BOOL=FALSE \
	-DWITH_METHOD_GENERIC_GMAC:BOOL=FALSE \
	-DWITH_METHOD_GENERIC_POLY1305:BOOL=FALSE \
	-DWITH_METHOD_GENERIC_UMAC:BOOL=FALSE \
	-DWITH_METHOD_NULL:BOOL=FALSE \
	-DWITH_METHOD_XSALSA20_POLY1305:BOOL=FALSE \
	-DWITH_CIPHER_AES128_CTR:BOOL=FALSE \
	-DWITH_CIPHER_NULL:BOOL=FALSE \
	-DWITH_CIPHER_SALSA20:BOOL=FALSE \
	-DWITH_CIPHER_SALSA2012:BOOL=FALSE \
	-DWITH_MAC_GHASH:BOOL=FALSE \
	-DWITH_MAC_UHASH:BOOL=FALSE \
	-DWITH_CAPABILITIES:BOOL=FALSE \
	-DWITH_CMDLINE_USER:BOOL=FALSE \
	-DWITH_CMDLINE_LOGGING:BOOL=FALSE \
	-DWITH_CMDLINE_OPERATION:BOOL=FALSE \
	-DWITH_CMDLINE_COMMANDS:BOOL=FALSE \
	-DWITH_DYNAMIC_PEERS:BOOL=FALSE \
	-DWITH_STATUS_SOCKET:BOOL=FALSE \
	-DENABLE_SYSTEMD:BOOL=FALSE \
	-DENABLE_LIBSODIUM:BOOL=FALSE \
	-DENABLE_LTO:BOOL=TRUE


ifeq ($(CONFIG_FASTD_ENABLE_METHOD_CIPHER_TEST),y)
CMAKE_OPTIONS += -DWITH_METHOD_CIPHER_TEST:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_COMPOSED_GMAC),y)
CMAKE_OPTIONS += -DWITH_METHOD_COMPOSED_GMAC:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_COMPOSED_UMAC),y)
CMAKE_OPTIONS += -DWITH_METHOD_COMPOSED_UMAC:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_GENERIC_GMAC),y)
CMAKE_OPTIONS += -DWITH_METHOD_GENERIC_GMAC:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_GENERIC_POLY1305),y)
CMAKE_OPTIONS += -DWITH_METHOD_GENERIC_POLY1305:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_GENERIC_UMAC),y)
CMAKE_OPTIONS += -DWITH_METHOD_GENERIC_UMAC:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_NULL),y)
CMAKE_OPTIONS += -DWITH_METHOD_NULL:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_METHOD_XSALSA20_POLY1305),y)
CMAKE_OPTIONS += -DWITH_METHOD_XSALSA20_POLY1305:BOOL=TRUE
endif


ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_AES128_CTR),y)
CMAKE_OPTIONS += -DWITH_CIPHER_AES128_CTR:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_NULL),y)
CMAKE_OPTIONS += -DWITH_CIPHER_NULL:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_SALSA20),y)
CMAKE_OPTIONS += -DWITH_CIPHER_SALSA20:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_SALSA2012),y)
CMAKE_OPTIONS += -DWITH_CIPHER_SALSA2012:BOOL=TRUE
endif


ifeq ($(CONFIG_FASTD_ENABLE_MAC_GHASH),y)
CMAKE_OPTIONS += -DWITH_MAC_GHASH:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_ENABLE_MAC_UHASH),y)
CMAKE_OPTIONS += -DWITH_MAC_UHASH:BOOL=TRUE
endif


ifeq ($(CONFIG_FASTD_WITH_CAPABILITIES),y)
CMAKE_OPTIONS += -DWITH_CAPABILITIES:BOOL=TRUE
endif


ifeq ($(CONFIG_FASTD_WITH_CMDLINE_USER),y)
CMAKE_OPTIONS += -DWITH_CMDLINE_USER:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_WITH_CMDLINE_LOGGING),y)
CMAKE_OPTIONS += -DWITH_CMDLINE_LOGGING:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_WITH_CMDLINE_OPERATION),y)
CMAKE_OPTIONS += -DWITH_CMDLINE_OPERATION:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_WITH_CMDLINE_COMMANDS),y)
CMAKE_OPTIONS += -DWITH_CMDLINE_COMMANDS:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_WITH_DYNAMIC_PEERS),y)
CMAKE_OPTIONS += -DWITH_DYNAMIC_PEERS:BOOL=TRUE
endif

ifeq ($(CONFIG_FASTD_WITH_STATUS_SOCKET),y)
CMAKE_OPTIONS += -DWITH_STATUS_SOCKET:BOOL=TRUE
endif


define Package/fastd/description
 Fast and secure tunneling daemon, which is optimized on small code size and few dependencies
endef

define Package/fastd/conffiles
/etc/config/fastd
endef

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

	$(INSTALL_DIR) $(1)/etc/init.d/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/doc/examples/openwrt/fastd.init $(1)/etc/init.d/fastd
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) $(PKG_BUILD_DIR)/doc/examples/openwrt/fastd.config $(1)/etc/config/fastd
	$(INSTALL_DIR) $(1)/etc/fastd
	$(INSTALL_DIR) $(1)/lib/upgrade/keep.d
	$(INSTALL_DATA) files/fastd.upgrade $(1)/lib/upgrade/keep.d/fastd
endef

$(eval $(call BuildPackage,fastd))
