# Copyright 2019 University of Warsaw
# The package contains files based on others' works. The contributions
# are stated in those files.

include $(TOPDIR)/rules.mk

PKG_NAME:=log-upload
PKG_VERSION:=1.0.2
PKG_RELEASE:=5
PKG_LICENSE:=GPL-2.0-only
PKG_LICENSE_FILES:=License

include $(INCLUDE_DIR)/package.mk

define Package/log-upload
	SECTION:=heni
	CATEGORY:=HENI
	TITLE:=HENI client
	DEPENDS:=+libprotobuf-c +libconfig +libpthread
endef

define Package/log-upload/description
  Client for UDP communication with HENI server
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
endef

define Build/Compile
  protoc-c -Iproto/ --c_out=src proto/com.proto
	$(TARGET_CC) $(TARGET_CFLAGS) -std=gnu99 -o $(PKG_BUILD_DIR)/log-upload src/*.c -lprotobuf-c -pthread -lconfig
endef

define Package/log-upload/install
	$(INSTALL_DIR) $(1)/usr
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/log-upload $(1)/usr/bin/log-upload
	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/etc/init.d/log-upload $(1)/etc/init.d/log-upload
	$(INSTALL_DATA) ./files/etc/log-upload.cfg $(1)/etc/log-upload.cfg
endef

define Package/log-upload/postinst
#!/bin/sh
if [ -z "$${IPKG_INSTROOT}" ]; then
        echo "Enabling rc.d symlink for log-upload"
        /etc/init.d/log-upload enable
fi
exit 0
endef

define Package/log-upload/prerm
#!/bin/sh
if [ -z "$${IPKG_INSTROOT}" ]; then
        echo "Removing rc.d symlink for log-upload"
        /etc/init.d/log-upload disable
fi
exit 0
endef

$(eval $(call BuildPackage,log-upload))
