# Contributor: Stuart Cardall <developer@it-offshore.co.uk>
# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=tinyssh
pkgver=20240101
pkgrel=2
pkgdesc="Small SSH server using NaCl / TweetNaCl (no dependency on OpenSSL)"
url="https://tinyssh.org/"
license="CC0-1.0"
arch="all"
_openrc_deps="ucspi-tcp6"
makedepends="libsodium-dev"
options="!check"
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/janmojzis/tinyssh/archive/refs/tags/$pkgver.tar.gz
	gcc14-packet-put-add-missing-include.patch
	$pkgname.initd
	$pkgname.confd
	"

# secfixes:
#   20230101-r3:
#     - CVE-2023-48795

build() {
	export LIBS="-lsodium"
	export CFLAGS="$CFLAGS -I/usr/include/sodium"
	export LDFLAGS="$LDFLAGS -L/usr/lib"
	make
}

check() {
	make check
}

package() {
	make install DESTDIR="$pkgdir"

	mkdir -p "$pkgdir"/etc/$pkgname
	install -m755 -D "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/tinysshd
	install -m644 -D "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/tinysshd

	# post-install message
	mkdir -p "$pkgdir/usr/share/doc/$pkgname"
	cat > $pkgdir/usr/share/doc/$pkgname/README.alpine <<EOF
TinySSH can be run in 3 different ways:
---------------------------------------
(1) /etc/init.d/tinysshd (requires tcpserver & takes care of key generation)

(2) via busybox (you will need to 'tinysshd-makekeys <dir>' manually):

busybox tcpsvd 0.0.0.0 22 tinysshd -v /etc/tinyssh/sshkeys &

(3) via inetd (you will need to 'tinysshd-makekeys <dir>' manually):

echo '22 stream tcp nowait root /usr/sbin/tinysshd tinysshd -l -v /etc/tinyssh/sshkeys' >> /etc/inetd.conf
rc-service inetd start
EOF
}

openrc() {
	default_openrc
	depends="$_openrc_deps"
}

sha512sums="
b48561cfc11bb6d2e9b1c805c9dfc36be5f1bbbf04a455b8db3f02b5b8df15e420fcd93d58fb23526baaf0fd70e9969deca261152a656015f12a433a61092e90  tinyssh-20240101.tar.gz
6d7931fdc0c58a6b3a624800e1509fe3f7ae7aea68a0d6e4a2a92086faeed5cae7c92d19f53c5ff230ecbb4cf25678f20c0469f5d6414b9541e7d3981db94edb  gcc14-packet-put-add-missing-include.patch
ee3b43f86239825daf320853c6da1f289e11e66ab8fc69ff181327dffcd71a5e8f10e151f717b5a8c65befaf89fea49e75ca0e78cd57b6d3868e2ac0ee258d94  tinyssh.initd
7c6282a6ae972d83d3e624530cac4958adee1e2313d0e44aff38c94bde1a3f549a536ec80e594d44c29a6a981919dd30322e8d8511626fdb6493c98587047392  tinyssh.confd
"
