diff --git a/tsu b/tsu new file mode 100755 index 0000000..d96c9b8 --- /dev/null +++ b/tsu @@ -0,0 +1,82 @@ +#!/data/data/com.termux/files/usr/bin/sh + +# tsu + +show_usage() { + echo 'tsu - a wrapper for su for Termux' + echo + echo 'Usage: tsu [-e|-p|-s ]' + echo + echo '-s []' + echo ' Use an alternate specified shell. `//usr` is expanded to $PREFIX.' + echo '-p' + echo ' Prepend /system/bin and /system/xbin to PATH.' + echo '-e' + echo ' Setup up some enviroment variables as when in Termux.' +} + +# parse the options +# TODO: validate command line arguments +while getopts ':ehps:' opt ; do +case $opt in + e) + SETUP_ENV=1 + ;; + + s) + USER_SHELL=$OPTARG + ;; + h) + show_usage + exit + ;; + p) + PREPEND_SYSTEM_PATH=1 + ;; +esac +done; + +# prefix check +test -z "$PREFIX" && PREFIX=/data/data/com.termux/files/usr + +# prepend path +if test -n "$PREPEND_SYSTEM_PATH"; then + PATH="/system/bin:/system/xbin:$PATH" +fi + +# export environment variables. +if test -n "$SETUP_ENV"; then + export HOME=/data/data/com.termux/files/home + TERMUX_PATHS=data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets + if test -n "$PREPEND_SYSTEM_PATH"; then + PATH="$PATH:$TERMUX_PATHS" + else + PATH="$TERMUX_PATHS:$PATH" + fi; +export PATH; +fi; + +# select shell +if test -z "$USER_SHELL"; then + # check if user has set shell + if test -x "$HOME/.termux/shell" ; then + ROOT_SHELL=$HOME/.termux/shell + # has installed bash? + elif test -x "$PREFIX/bin/bash" ; then + ROOT_SHELL="$PREFIX/bin/bash" +# no..fallback to default ash + else + ROOT_SHELL="$PREFIX/bin/ash" + fi +else + # expand //usr/ to /usr/ + USER_SHELL_EXPANDED=$(echo "$USER_SHELL" | sed "s|^//usr/|$PREFIX/|") + ROOT_SHELL="$USER_SHELL_EXPANDED" +fi + +# using the -c option of su allows to pass enviroment variables +for s in '/system/xbin/su' '/su/bin/su'; do + if [ -e "$s" ]; then + exec "$s" --preserve-environment -c "LD_LIBRARY_PATH=$PREFIX/lib $ROOT_SHELL" + fi; +done;