#
# 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

PKG_NAME:=eapcontroller
PKG_VERSION:=3

QUOTE:="
EMPTY:=
PRODUCT_NAME:=$(subst $(QUOTE),$(EMPTY),$(CONFIG_PRODUCT_NAME))
PKG_SOURCE:=$(PKG_NAME)_$(PRODUCT_NAME).tar.gz
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
PKG_FILES_DIR:=files

include $(INCLUDE_DIR)/package.mk

define Package/eapcontroller
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=TP-Link EAP controller
  URL:=http://www.tp-link.com
  DEPENDS:=+libX11 +libXext +libXi +libXrender +libXtst +libfreetype +libpthread +libz +alsa-lib +libasound
endef

define Package/eapcontroller/description
 TP-Link EAP controller
endef

define Build/Configure/Default
endef

define Build/Compile
	mkdir -p $(PKG_BUILD_DIR)/src
	$(CP) ./src/* $(PKG_BUILD_DIR)/src/
	$(MAKE) -C $(PKG_BUILD_DIR)/src \
		CC="$(TARGET_CC)" \
		CFLAGS="$(TARGET_CFLAGS) -Wall" \
		LDFLAGS="$(TARGET_LDFLAGS)"
endef


define Package/eapcontroller/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) $(PKG_FILES_DIR)/etc/init.d/eapcontroller $(1)/etc/init.d/eapcontroller
	$(INSTALL_BIN) $(PKG_FILES_DIR)/etc/init.d/mount_appdata $(1)/etc/init.d
	$(INSTALL_BIN) $(PKG_FILES_DIR)/etc/init.d/zzzzcgroups_set $(1)/etc/init.d

	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-reset $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-swap $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-unbind $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-proxy $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-log-clean $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-flush $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-downgrade $(1)/bin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/bin/eapcontroller-cgroups-set $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/eapcontroller-detect $(1)/bin

	$(INSTALL_DIR) $(1)/sbin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/sbin/private-data $(1)/sbin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/sbin/factoryreset $(1)/sbin

	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_BIN) $(PKG_FILES_DIR)/etc/system-release $(1)/etc

	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_FILES_DIR)/usr/sbin/quick_setup_check $(1)/usr/sbin

	$(INSTALL_DIR) $(1)/usr/share/fonts
	$(INSTALL_BIN) $(PKG_FILES_DIR)/usr/share/fonts/arial.ttf $(1)/usr/share/fonts

	$(INSTALL_DIR) $(1)/opt/tplink/EAPController/
	$(INSTALL_DIR) $(1)/opt/tplink/temp/
	$(CP) -a $(PKG_BUILD_DIR)/build/* $(1)/opt/tplink/temp/

	#delete non-use jre
	$(RM) -rf $(1)/opt/tplink/temp/jre

	# make factory dir
	$(INSTALL_DIR) $(1)/opt/tplink/temp/factory
	$(CP) -a $(1)/opt/tplink/temp/data/* $(1)/opt/tplink/temp/factory/

	# update mongox
	if [ -f $(1)/opt/tplink/temp/omadacVersion ]  && [ `cat $(1)/opt/tplink/temp/omadacVersion | grep -o  -e "^[0-9]"` -ge 4 ] ; then \
		$(INSTALL_DIR) $(1)/opt/tplink/temp/bin ; \
	else \
		$(RM) $(1)/opt/tplink/temp/bin/mongo ; \
		$(RM) $(1)/opt/tplink/temp/bin/mongod ; \
		$(SED) s/"${eap.home}\/logs"/"${eap.home}\/data\/logs"/g $(1)/opt/tplink/temp/properties/log4j2.properties ; \
	fi;
	
	$(LN) -s /usr/bin/mongo $(1)/opt/tplink/temp/bin/mongo
	$(LN) -s /usr/bin/mongod $(1)/opt/tplink/temp/bin/mongod

	# update properties files
	$(SED) s/"rootLogger.level.*"/"rootLogger.level = info"/g $(1)/opt/tplink/temp/properties/log4j2.properties
	$(SED) s/"appender.rolling.policies.size.size.*"/"appender.rolling.policies.size.size = 10MB"/g $(1)/opt/tplink/temp/properties/log4j2.properties
	$(SED) s/"appender.rolling.strategy.action.condition.nested_condition.exceeds.*"/"appender.rolling.strategy.action.condition.nested_condition.exceeds = 50MB"/g $(1)/opt/tplink/temp/properties/log4j2.properties
	
	if [ -f $(1)/opt/tplink/temp/properties/mongodb.properties ]; then \
		$(SED) s/"${eap.home}\/logs"/"${eap.home}\/data\/logs"/g $(1)/opt/tplink/temp/properties/mongodb.properties ; \
		$(SED) s/"--logappend"/"--smallfiles"/g $(1)/opt/tplink/temp/properties/mongodb.properties ; \
	fi;

	if [ -f $(1)/opt/tplink/temp/properties/jetty.properties ]; then \
		$(SED) s/"${eap.home}\/work"/"${eap.home}\/data\/work"/g $(1)/opt/tplink/temp/properties/jetty.properties ; \
		$(SED) s/"https.connector.port=8043"/"https.connector.port=443"/g $(1)/opt/tplink/temp/properties/jetty.properties ; \
		$(SED) s/"http.connector.port=8088"/"http.connector.port=80"/g $(1)/opt/tplink/temp/properties/jetty.properties ; \
	fi;
endef

$(eval $(call BuildPackage,eapcontroller))
