#
# Copyright (C) 2019 Lucian Cristian <lucian.cristian@gmail.com>
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=uacme
PKG_VERSION:=1.8.1
PKG_RELEASE:=3

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/ndilieto/uacme/tar.gz/upstream/$(PKG_VERSION)?
PKG_HASH:=de7588577f8298dcb0d42dfaa9452a918fa692c4e165060207ac22f72fb0425d

PKG_MAINTAINER:=Lucian Cristian <lucian.cristian@gmail.com>
PKG_LICENSE:=GPL-3.0-or-later
PKG_LICENSE_FILES:=COPYING

PKG_BUILD_DIR:=$(BUILD_DIR)/uacme-upstream-$(PKG_VERSION)
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1

PKG_CONFIG_DEPENDS:= \
  CONFIG_LIBCURL_GNUTLS \
  CONFIG_LIBCURL_MBEDTLS \
  CONFIG_LIBCURL_OPENSSL \
  CONFIG_LIBCURL_WOLFSSL \
  CONFIG_LIBCURL_NOSSL \
  CONFIG_UACME_UALPN

include $(INCLUDE_DIR)/package.mk


define Package/uacme/Default
  SECTION:=net
  CATEGORY:=Network
  DEPENDS:=+uacme
  TITLE:=lightweight client for ACMEv2
  URL:=https://github.com/ndilieto/uacme
endef

define Package/uacme
  $(call Package/uacme/Default)
  SECTION:=net
  CATEGORY:=Network
  DEPENDS:=+libcurl +LIBCURL_WOLFSSL:libmbedtls +acme-common
  TITLE:=lightweight client for ACMEv2
  Menu:=1
  PROVIDES:=@acme
endef

define Package/uacme-ualpn
  $(call Package/uacme/Default)
  DEPENDS:=+uacme +PACKAGE_uacme-ualpn:libev
  TITLE:=ualpn for uacme
  URL:=https://github.com/ndilieto/uacme
endef

define Package/uacme-dnsapi-adapter
  $(call Package/uacme/Default)
  DEPENDS:= +uacme +acme-acmesh-dnsapi +curl
  TITLE:=adapter script for use acme.sh dnsapi with uacme
endef

define Package/uacme/Default/description
  lightweight client for the RFC8555 ACMEv2 protocol, written in plain C code
  with minimal dependencies (libcurl and one of GnuTLS, OpenSSL or mbedTLS).
  The ACMEv2 protocol allows a Certificate Authority (https://letsencrypt.org
  is a popular one) and an applicant to automate the process of verification
  and certificate issuance. The protocol also provides facilities for other
  certificate management functions, such as certificate revocation.
endef

define Package/uacme/config
if PACKAGE_uacme && LIBCURL_WOLFSSL
  comment "libcurl uses WolfSSL; uacme will install mbedtls"
  comment "Choose another SSL lib in libcurl to avoid this"
endif
if PACKAGE_uacme && LIBCURL_NOSSL
  comment "libcurl is built without ssl; uacme needs ssl support"
  comment "Choose another SSL lib in libcurl to avoid this"
endif
if PACKAGE_uacme && LIBCURL_MBEDTLS
  comment "uacme will install mbedtls"
endif
endef
TARGET_LDFLAGS += -Wl,--gc-sections,--as-needed

CONFIGURE_ARGS+= \
	--disable-maintainer-mode \
	--disable-docs \
	$(if $(CONFIG_PACKAGE_uacme-ualpn),--with-ualpn,--without-ualpn) \
	$(if $(CONFIG_LIBCURL_GNUTLS),--with-gnutls --without-mbedtls --without-openssl,) \
	$(if $(CONFIG_LIBCURL_MBEDTLS),--without-gnutls --with-mbedtls --without-openssl,) \
	$(if $(CONFIG_LIBCURL_OPENSSL),--without-gnutls --without-mbedtls --with-openssl,) \
	$(if $(CONFIG_LIBCURL_WOLFSSL),--without-gnutls --with-mbedtls --without-openssl,)

define Package/uacme/conffiles
/etc/config/acme
/etc/acme
endef

define Package/uacme/install
	$(INSTALL_DIR) \
	 $(1)/usr/sbin \
	 $(1)/etc/acme \
	 $(1)/usr/share/uacme \
	 $(1)/usr/lib/acme/client

	$(INSTALL_BIN) ./files/hook.sh $(1)/usr/lib/acme/hook
	$(INSTALL_BIN) ./files/httpchalhook.sh $(1)/usr/lib/acme/client/httpchalhook.sh
	$(INSTALL_BIN) ./files/dns_persist.sh $(1)/usr/lib/acme/client/dns_persist.sh
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/uacme $(1)/usr/sbin/uacme
endef

define Package/uacme-ualpn/install
	$(INSTALL_DIR) \
	 $(1)/usr/sbin \
	 $(1)/usr/share/uacme \
	 $(1)/usr/lib/acme/client

	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/ualpn $(1)/usr/sbin/ualpn
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ualpn.sh $(1)/usr/lib/acme/client/ualpn.sh
endef

define Package/uacme-dnsapi-adapter/install
	$(INSTALL_DIR) \
	 $(1)/usr/lib/acme/client

	$(INSTALL_BIN) ./files/dnschalhook.sh $(1)/usr/lib/acme/client/dnschalhook.sh
	$(INSTALL_BIN) ./files/dnsapi_helper.sh $(1)/usr/lib/acme/client/dnsapi_helper.sh
endef

$(eval $(call BuildPackage,uacme))
$(eval $(call BuildPackage,uacme-ualpn))
$(eval $(call BuildPackage,uacme-dnsapi-adapter))
