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

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

# TERMUX_VERSION env variable has been exported since v0.107 and PATH was being set to following value in <0.104. Last playstore version was v0.101.
if tty >/dev/null 2>&1 && [ $# = 0 ] && [ -f /data/data/com.termux/files/usr/etc/motd-playstore ] && [ -z "$TERMUX_VERSION" ] && [ "$PATH" = "/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets" ]; then
	printf '\033[0;31m'; cat /data/data/com.termux/files/usr/etc/motd-playstore; printf '\033[0m'
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

# TERMUX_APP_PACKAGE_MANAGER should be exported by termux-app v0.119.0+ itself
if [ -z "${TERMUX_APP_PACKAGE_MANAGER-}" ]; then
	if { [ -n "$(command -v dpkg)" ] && dpkg --compare-versions "$TERMUX_VERSION" lt 0.119.0; } ||   # apt
		{ [ -n "$(command -v vercmp)" ] && [ "$(vercmp "$TERMUX_VERSION" 0.119.0)" = "-1" ]; }; then # pacman
		# For the correct operation of scripts that work with the package manager
		export TERMUX_MAIN_PACKAGE_FORMAT="debian"
	fi
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 "coreutils --coreutils-prog=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 "coreutils --coreutils-prog=true" > /dev/null 2>&1 || unset LD_PRELOAD
fi

if [ -f /data/data/com.termux/files/usr/etc/termux-login.sh ]; then
	. /data/data/com.termux/files/usr/etc/termux-login.sh
fi

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