#!/bin/sh

# launches SSH server for use in SFTP transfers
# generates usage instructions to make connecting easier

port=8022
user=$(whoami)
ip=$(ip -o addr | grep -F 'wlan' | grep -Eo '([0-9]{1,3}\.?){4}' | head -n 1)

# get path to external sdcard if it exists
# paths take the form /storage/XXXX-XXXX
int_path="$(readlink ~/storage/shared)"
ext_path="$(readlink ~/storage/external* | head -n 1)"
if [ ! -z "$ext_path" ]; then
	while [ "$(echo "$ext_path" | tr -cd '/' | wc -c)" -gt 2 ]; do
		ext_path="${ext_path%/*}"
	done
fi

# splash screen
cat <<- EOF
	$(printf '\33[1m%s\33[0m' "${0##*/}")
	usage: ssh -C -p $port $user@${ip:-localhost (no WLAN connection)}

	internal: $int_path
	external: $ext_path

	$(printf '\33[1m%s\33[0m' '^C to exit.')

EOF

# release wakelock and return 0 on SIGINT
quit() {
	termux-wake-unlock &
	:
}

# acquire wakelock
# run in foreground, android hates forking daemons
stty -echo
trap quit 2
termux-wake-lock &
sshd -p $port -D -e 2>&1 | ts '[%a %b %_d %_H:%M:%S]'