#!/usr/bin/env bash
# isterm -- Check whether the terminal is being emulated by PuTTY/Mosh/JuiceSSH
#
# - PuTTY detection is needed for things like Neovim, which strictly adhere
#   to terminfo data and do not recognize (or rather, incorrectly recognize)
#   the Home/End keys, which PuTTY sends as \e[1~ and \e[4~ whereas terminfo
#   (xterm-256color) says khome/kend are \eOH and \eOF respectively.
#
# - Mosh/JuiceSSH detection is also used for Neovim to tell it whether to avoid
#   'tgc' truecolor mode (which Mosh doesn't support, but apparently Neovim
#   detects it as capable regardless).
#
# Note: Strictly speaking, 'checkterm juicessh' is written in a way that it
# primarily detects Mosh, with the actual JuiceSSH detection being only a side
# effect, but the use case is identical for both.
#
# XXX: Neovim 0.10 uses XTGETTCAP and SGR + DECRQSS to detect termguicolor
#      support (see runtime/lua/vim/_defaults.lua), so technically we no longer
#      need to do any of this for it anymore.

usage() {
	echo "Usage: ${0##*/} {juicessh|mosh|putty}"
}

if (( $# != 1 )); then
	usage >&2
	exit 2
elif [[ $1 != @(juicessh|mosh|putty) ]]; then
	echo "${0##*/}: Unknown terminal '$1'" >&2
	exit 2
fi

arg_wanted=$1

if [[ ! -t 0 || ! -t 1 ]]; then
	exit 1
elif [[ $TERM == @(putty|putty-*) && $arg_wanted == putty ]]; then
	# Shortcut if ~/.bashrc has already changed TERM
	exit 0
elif [[ $TERM != @(xterm|xterm-*) ]]; then
	# Don't bother checking when inside tmux or something else
	exit 1
fi

trap "stty echo" EXIT
stty -echo

# Request Primary DA to detect JuiceSSH (which fails to parse Secondary DA
# query and leaves the 'c' visible on screen).
if [[ ! $COLORTERM ]]; then
	# Request also the Xterm "Report background color" OSC, to rule out
	# e.g. Nvim :terminal (which reports the same Primary DA).
	printf '\e]11;?\e\\'
	printf '\e[c'
	if read -r -d '?' -t 2 ans1 && read -r -d 'c' -t 2 ans2; then
		ans="${ans1}?${ans2}c"
		if [[ $ans == *$'\e[?1;2c' && $ans != *$'\e]11;'* ]]; then
			# If we get this Primary DA answer *and* no background
			# color report, it's most likely JuiceSSH, so let's exit
			# immediately without doing a Secondary DA query.
			if [[ $arg_wanted == juicessh ]]; then
				exit 0
			else
				exit 1
			fi
		fi
	else
		echo "${0##*/}: did not receive Primary DA reply from terminal" >&2
		exit 4
	fi
fi

# Request Secondary DA (and Primary DA, to avoid hang on terminals that fail to
# answer the former).
# (XXX: Could also use DSR '\e[5n' => '\e[0n' for the latter purpose.)
printf '\e[>c\e[c'
if read -r -d '?' -t 2 ans1 && read -r -d 'c' -t 2 ans2; then
	ans="${ans1}?${ans2}c"
	if [[ $ans == *$'\e[>0;136;0c\e[?6c' && $arg_wanted == putty ]]; then
		exit 0
	elif [[ $ans == *$'\e[>1;10;0c\e[?62c' && $arg_wanted == @(mosh|juicessh) ]]; then
		# This actually detects Mosh, but both have the same lack of
		# truecolor support so it does the job well enough.
		exit 0
	else
		exit 1
	fi
else
	echo "${0##*/}: did not receive Primary DA reply from terminal" >&2
	exit 4
fi
