TERMUX_PKG_HOMEPAGE=https://matt.ucc.asn.au/dropbear/dropbear.html
TERMUX_PKG_DESCRIPTION="Small SSH server and client"
TERMUX_PKG_LICENSE="MIT"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION="2026.91"
TERMUX_PKG_SRCURL=https://matt.ucc.asn.au/dropbear/releases/dropbear-${TERMUX_PKG_VERSION}.tar.bz2
TERMUX_PKG_SHA256=defa924475abf6bc1e74abc00173e46bfdc804bd47caafa14f5a4ef0cc76da34
TERMUX_PKG_AUTO_UPDATE=false
TERMUX_PKG_DEPENDS="termux-auth, zlib"
TERMUX_PKG_SUGGESTS="openssh-sftp-server, termux-services"
TERMUX_PKG_CONFLICTS="openssh"
TERMUX_PKG_BUILD_IN_SRC=true

TERMUX_PKG_EXTRA_CONFIGURE_ARGS="--disable-syslog --disable-utmp --disable-utmpx --disable-wtmp --disable-static"
# Avoid linking to libcrypt for server password authentication:
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_lib_crypt_crypt=no"
# BIonic is special case, as usuas.
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_func_htole64=yes"
# setresgid() is blocked by Android for non-root users, so disable it
TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" ac_cv_func_setresgid=no"
# build a multi-call binary & enable progress info in 'scp'
TERMUX_PKG_EXTRA_MAKE_ARGS="MULTI=1 SCPPROGRESS=1"

termux_step_pre_configure() {
	export LIBS="-ltermux-auth -llog"
}

termux_step_post_make_install() {
	ln -sf "dropbearmulti" "${TERMUX_PREFIX}/bin/ssh"

	mkdir -p "$TERMUX_PREFIX/var/run"
	echo "Dropbear needs this directory to put dropbear.pid in" > "$TERMUX_PREFIX/var/run/README.dropbear"

	# Setup termux-services scripts
	mkdir -p "$TERMUX_PREFIX/var/service/dropbear/log"
	ln -sf "$TERMUX_PREFIX/share/termux-services/svlogger" "$TERMUX_PREFIX/var/service/dropbear/log/run"
	sed "s%@TERMUX_PREFIX@%$TERMUX_PREFIX%g" "$TERMUX_PKG_BUILDER_DIR/sv/dropbear.run.in" > "$TERMUX_PREFIX/var/service/dropbear/run"
	chmod 700 "$TERMUX_PREFIX/var/service/dropbear/run"
	touch "$TERMUX_PREFIX/var/service/dropbear/down"
}

termux_step_create_debscripts() {
	{
	echo "#!$TERMUX_PREFIX/bin/sh"
	echo "mkdir -p $TERMUX_PREFIX/etc/dropbear"
	echo "for a in rsa ecdsa ed25519; do"
	echo "	KEYFILE=$TERMUX_PREFIX/etc/dropbear/dropbear_\${a}_host_key"
	echo "	test ! -f \$KEYFILE && dropbearkey -t \$a -f \$KEYFILE"
	echo "done"
	echo ""
	echo "echo \"You can enable dropbear to autostart\""
	echo "echo \"Run 'pkg i termux-services'\""
	echo "echo \"to install the ('runit') service manager\""
	echo "echo \"then using 'sv-enable dropbear'\""
	echo "exit 0"
	} > postinst
	chmod 0755 postinst
}
