#!/data/data/com.termux/files/usr/bin/sh

if [ $# = 0 ] && [ -f /data/data/com.termux/files/usr/etc/motd ] && [ ! -f ~/.hushlogin ] && [ -z "$TERMUX_HUSHLOGIN" ]; then
	cat /data/data/com.termux/files/usr/etc/motd
else
	# This variable shouldn't be kept set.
	unset TERMUX_HUSHLOGIN
fi

if [ -G ~/.termux/shell ]; then
	export SHELL="`realpath ~/.termux/shell`"
else
	for file in /data/data/com.termux/files/usr/bin/bash /data/data/com.termux/files/usr/bin/sh /system/bin/sh; do
		if [ -x $file ]; then
			export SHELL=$file
			break
		fi
	done
fi

# Export `libtermux-exec-ld-preload.so` for `termux-exec`
# package version `>= 2.0.0`.
# Some devices may not support setting `$LD_PRELOAD`.
# - https://github.com/termux/termux-packages/issues/2066
# - https://github.com/termux/termux-packages/commit/1ec6c042
# - https://github.com/termux/termux-packages/commit/6fb2bb2f
if [ -f "/data/data/com.termux/files/usr/lib/libtermux-exec-ld-preload.so" ]; then
	export LD_PRELOAD="/data/data/com.termux/files/usr/lib/libtermux-exec-ld-preload.so"
	$SHELL -c "busybox true" > /dev/null 2>&1 || unset LD_PRELOAD
elif [ -f "/data/data/com.termux/files/usr/lib/libtermux-exec.so" ]; then
	export LD_PRELOAD="/data/data/com.termux/files/usr/lib/libtermux-exec.so"
	$SHELL -c "busybox true" > /dev/null 2>&1 || unset LD_PRELOAD
fi

if [ -n "$TERM" ]; then
	exec "$SHELL" -l "$@"
else
	exec "$SHELL" "$@"
fi
