#
# Copyright (C) 2016-2019 Jason A. Donenfeld <Jason@zx2c4.com>
# Copyright (C) 2016 Baptiste Jonglez <openwrt@bitsofnetworks.org>
# Copyright (C) 2016-2017 Dan Luedtke <mail@danrl.com>
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=wireguard-trb5

PKG_VERSION:=1.0.20200611
PKG_RELEASE:=2

PKG_SOURCE_VERSION:=1.0.20200611
PKG_SOURCE:=wireguard-linux-compat-$(PKG_SOURCE_VERSION).tar.xz

PKG_SOURCE_URL:=https://git.zx2c4.com/wireguard-linux-compat/snapshot/
PKG_HASH:=9b0478c3b1f3a7b488916e632e2fcbb1383bb1a2ef294489858ce2ba1da3246d

PKG_LICENSE:=GPL-2.0-only
PKG_LICENSE_FILES:=COPYING

PKG_BUILD_DIR:=$(KERNEL_BUILD_DIR)/wireguard-linux-compat-$(PKG_VERSION)
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=no-mips16

# WireGuard's makefile needs this to know where to build the kernel module
export KERNELDIR:=$(LINUX_DIR)

include $(INCLUDE_DIR)/package.mk

define Package/wireguard-trb5/Default
  SECTION:=net
  CATEGORY:=Network
  SUBMENU:=VPN
  URL:=https://www.wireguard.com
  MAINTAINER:=Jason A. Donenfeld <Jason@zx2c4.com> \
              Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
endef

define Package/wireguard-trb5/Default/description
  WireGuard is a novel VPN that runs inside the Linux Kernel and utilizes
  state-of-the-art cryptography. It aims to be faster, simpler, leaner, and
  more useful than IPSec, while avoiding the massive headache. It intends to
  be considerably more performant than OpenVPN.  WireGuard is designed as a
  general purpose VPN for running on embedded interfaces and super computers
  alike, fit for many different circumstances. It uses UDP.
endef

define Package/wireguard-trb5
  $(call Package/wireguard-trb5/Default)
  TITLE:=WireGuard-trb5 meta-package
  DEPENDS:=+wireguard-tools +kmod-wireguard-trb5
endef

include $(INCLUDE_DIR)/kernel-defaults.mk
include $(INCLUDE_DIR)/package-defaults.mk

define Build/Compile
	$(MAKE) $(KERNEL_MAKEOPTS) M="$(PKG_BUILD_DIR)/src" modules
endef

define Package/wireguard-trb5/install
  true
endef

define Package/wireguard-trb5/description
  $(call Package/wireguard-trb5/Default/description)
endef

define KernelPackage/wireguard-trb5
  SECTION:=kernel
  CATEGORY:=Kernel modules
  SUBMENU:=Network Support
  TITLE:=WireGuard-trb5 kernel module
  DEPENDS:=+IPV6:kmod-udptunnel6 +kmod-udptunnel4
  FILES:= $(PKG_BUILD_DIR)/src/wireguard.$(LINUX_KMOD_SUFFIX)
  AUTOLOAD:=$(call AutoProbe,wireguard-trb5)
endef

define KernelPackage/wireguard-trb5/description
  $(call Package/wireguard/Default/description)

  This package provides the kernel module for WireGuard.
endef

#Unload WireGuard kernel module
define Package/wireguard-trb5/prerm
	#!/bin/sh

	. /lib/functions.sh

	clean_config() {
		local section_id="$$1"
		local proto=""
		local cfg=""
		config_get proto "$$section_id" proto ""

		if [[ "$$proto" == "wireguard" ]]; then
			uci_remove network "$$section_id"
		else
			cfg=$$(uci_get network "$$section_id" | grep wireguard_)
			if [[ ! -z "$$cfg" ]]; then
				uci_remove network "$$section_id"
			fi
		fi
	}

	config_load 'network'
	config_foreach clean_config
	uci_commit network
endef

define Package/wireguard-trb5/install
	true
endef

$(eval $(call BuildPackage,wireguard-trb5))
$(eval $(call KernelPackage,wireguard-trb5))
