# Contributor: Stuart Cardall <developer@it-offshore.co.uk>
# Maintainer: Stuart Cardall <developer@it-offshore.co.uk>

setvar _flavor = ${FLAVOR:-hardened}
setvar _kpkg = "linux-$_flavor"
setvar _kver = '4.9.54'
setvar _kpkgrel = '0'

# when changing _ver we *must* bump _mypkgrel
# we must also match up _toolsrel with wireguard-tools
setvar _ver = '0.0.20171011'
setvar _mypkgrel = '2'
setvar _toolsrel = '0'
setvar _name = 'wireguard'

# verify the kernel version before entering chroot
setvar _kapkbuild = "../../linux-${_flavor}/APKBUILD"
if test -f $_kapkbuild {
	source $_kapkbuild
	setvar pkgname = "$_name-$_flavor"
	test $_kver != $pkgver && die "please update _kver to $pkgver"
	test $_kpkgrel != $pkgrel && die "please update _kpkgrel to $pkgrel"
}

setvar _kpkgver = ""$_kver-r$_kpkgrel""
setvar _toolsver = ""$_ver-r$_toolsrel""
setvar _abi_release = "${_kver}-${_kpkgrel}-${_flavor}"

setvar pkgname = "${_name}-${_flavor}"
setvar pkgver = "$_kver"
setvar pkgrel = $(($_kpkgrel + $_mypkgrel))
setvar pkgdesc = ""Next generation secure network tunnel: kernel modules for $_flavor""
setvar arch = ''x86 x86_64 armhf''
setvar url = ''https://www.wireguard.com''
setvar license = ""GPLv2""
setvar depends = ""linux-${_flavor}=${_kpkgver}""
setvar makedepends = ""linux-${_flavor}-dev=$_kpkgver libmnl-dev""
setvar install_if = ""wireguard-tools=$_toolsver linux-hardened=$_kpkgver""
setvar options = ""!check""
setvar source = ""https://git.zx2c4.com/WireGuard/snapshot/WireGuard-$_ver.tar.xz""
setvar provides = ""${_name}-grsec=${_kver}-r${pkgrel}""
setvar builddir = ""$srcdir"/WireGuard-$_ver"

proc build {
	cd $builddir
	# only building module: see wireguard-tools for userspace
	make -C src/ \
		KERNELDIR=/lib/modules/${_abi_release}/build \
		module
}

proc package {
	cd "$builddir/src"

	local module=
	for module in *.ko {
		install -v -D -m644 ${module} \
			"$pkgdir/lib/modules/$_abi_release/extra/${module}"
	}
}

setvar sha512sums = ""7ec5959becf96d214b0b6a0f2c638c986c7b330ce24c323a77f06a2ab853affb85c3cc6ab54d01ece525aef9bfd627bf4b69f21167ee86b16442659d202fcd77  WireGuard-0.0.20171011.tar.xz""