#
# Copyright (C) 2017 Roman Savochenko
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=openscada
PKG_VERSION:=0.9+r2517
PKG_RELEASE:=1
PKG_LICENSE:=GPL-2.0+

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=http://ftp.oscada.org/OpenSCADA/Work/
#PKG_MD5SUM:=71d1196ca9ce2afb2d970f1d6e8b0461

PKG_FIXUP:=autoreconf

include $(INCLUDE_DIR)/package.mk

define Package/openscada
  SECTION:=auto
  CATEGORY:=Automation
  DEPENDS:=+libiconv-full +libintl-full +librt +libstdcpp +zlib +libpcre +libsqlite3 +libopenssl +fftw3
  TITLE:=Open Supervisory Control And Data Acquisition system
  URL:=http://oscada.org/
  MAINTAINER:=Roman Savochenko <rom_as@oscada.org>
endef

define Package/openscada/description
	OpenSCADA system is an open implementation of
	SCADA (Supervisory Control And Data Acquisition) and
	HMI (Human-Machine Interface) systems.
endef

TARGET_CFLAGS += $(FPIC)

CONFIGURE_VARS += CFLAGS="-O2" CXXFLAGS="-O2 -I$(STAGING_DIR)/usr/lib/libiconv-full/include -I$(STAGING_DIR)/usr/lib/libintl-full/include" \
	LDFLAGS="-L$(STAGING_DIR)/usr/lib/libiconv-full/lib -L$(STAGING_DIR)/usr/lib/libintl-full/lib"

CONFIGURE_ARGS += --prefix=/usr --sysconfdir=/etc --disable-AllModuls \
	--enable-SQLite=incl --enable-DBF=incl \
	--enable-Sockets=incl --enable-Serial=incl --enable-SSL \
	--enable-SelfSystem=incl --enable-UserProtocol=incl --enable-HTTP=incl \
	--enable-JavaLikeCalc=incl --enable-LogicLev=incl --enable-BlockCalc=incl --enable-System --enable-OPC_UA --enable-DAQGate=incl --enable-ModBus=incl --enable-DCON=incl \
	--enable-FSArch=incl --enable-DBArch=incl \
	--enable-WebCfg --enable-WebCfgD --enable-WebUser \
	--enable-FLibSYS=incl --enable-SystemTests --enable-FLibMath --enable-FLibComplex1

define Build/Compile
	+$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS)
	mkdir -p $(PKG_INSTALL_DIR)
	$(MAKE) -C $(PKG_BUILD_DIR) DESTDIR="$(PKG_INSTALL_DIR)" install
endef

define Package/openscada/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/openscada $(1)/usr/bin/
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/liboscada.so.* $(1)/usr/lib/
	$(INSTALL_DIR) $(1)/usr/lib/openscada
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/openscada/*.so $(1)/usr/lib/openscada/
	$(INSTALL_DIR) $(1)/usr/share/locale
	$(CP) -R $(PKG_INSTALL_DIR)/usr/share/locale/* $(1)/usr/share/locale/
	$(INSTALL_DIR) $(1)/usr/share/openscada/icons
	$(CP) -R ./files/icons/* $(1)/usr/share/openscada/icons/
	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_CONF) ./files/oscada.xml $(1)/etc/
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/openscada $(1)/etc/init.d/
endef

$(eval $(call BuildPackage,openscada))
