#!/data/data/com.bai.diple.ide/files/usr/bin/sh
# shellcheck shell=dash

is_executable_file() {
	test -f "$1" -a -x "$1"
}

set_default_shell() {
	for file in "/data/data/com.bai.diple.ide/files/usr/bin/bash" "/data/data/com.bai.diple.ide/files/usr/bin/sh" "/system/bin/sh"; do
		if is_executable_file "$file"; then
			SHELL="$file"
			break
		fi
	done
}

if tty >/dev/null 2>&1 && [ $# = 0 ] && [ ! -f ~/.hushlogin ] && [ -z "$TERMUX_HUSHLOGIN" ]; then
	if [ -f ~/.config/termux/motd-dynamic ]; then
		# Use user defined dynamic motd file if it exists
		[ ! -x ~/.config/termux/motd-dynamic ] && chmod u+x ~/.config/termux/motd-dynamic
		~/.config/termux/motd-dynamic
	elif [ -f ~/.config/termux/motd ]; then
		# Use user defined static motd file if it exists
		cat ~/.config/termux/motd
	elif [ -f ~/.termux/motd.sh ]; then
		# Same as termux-tools in F-Droid build does:
		[ ! -x ~/.termux/motd.sh ] && chmod u+x ~/.termux/motd.sh
		~/.termux/motd.sh
	elif [ -f /data/data/com.bai.diple.ide/files/usr/etc/motd ]; then
		# Default to termux-tools package provided static motd file if it exists
		cat /data/data/com.bai.diple.ide/files/usr/etc/motd
	fi
else
	# This variable shouldn't be kept set.
	unset TERMUX_HUSHLOGIN
fi

unset SHELL
SHELL_FILE=~/.termux/shell

if [ -L "$SHELL_FILE" ]; then
	# Set the symlink's value as the shell target.
	SHELL_TARGET="$(readlink "$SHELL_FILE")"
	if is_executable_file "$SHELL_TARGET" &&
		[ ! "$SHELL_TARGET" -ef "/data/data/com.bai.diple.ide/files/usr/bin/login" ]; then
		SHELL="$SHELL_TARGET"
	fi
fi

if [ -z "${SHELL:-}" ]; then
	set_default_shell
fi

export SHELL

if [ -f ~/.config/termux/login.sh ]; then
	. /data/data/com.bai.diple.ide/files/usr/etc/termux-login.sh
fi

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