#!/bin/bash
#
# License: 3-clause BSD *except for the soft font* - that has no particular
#	   license specified.
#
# This is a quick script to show off some of Kermit 95s terminal features.
# For everything to work right, the GUI version of K95 3.0 beta 8 is required,
# and K95 should be set to use the "k95" terminal type (SET TERM TYPE K95).
# It assumes an 80x24 terminal with the status line on (these are the defaults
# for the k95 terminal type), but it should work for larger sizes too. It won't
# attempt to turn the status line off if it wasn't on when the script was run.
#
# When run in other terminal emulators the things that are most likely to not
# work include:
#  * Host programmable status line - rarely supported feature
#  * 24-bit and indexed colors (this script uses the correct ':' syntax, while
#    while some terminals only support the older and incorrect ';' syntax. K95
#    does support the older incorrect syntax for compatibility with other
#    terminals)
#  * Blinking text attribute - often not supported
#  * Double hight lines - often not supported
#

# Top Banner
VERSION=" 3 . 0  B E T A  8"
BANNER_FMT="  K E R M I T - 9 5 \x1b[3m%s\x1b[0m\n"

# Features - some aren't supported by K95 yet, and none of them are available
#            in releases prior to beta 8.
F_TRUE_COLOR=1     # New in beta 8
F_STRIKETHROUGH=1  # New in beta 8
F_RULED_LINES=0    # -- not supported --
F_EXTENDED_UL=0    # -- not supported --
F_SOFT_FONT=0      # -- not supported --
F_VT420_FEATURES=1 # Rectangular area operations mostly present in 2.1 (2002)
                   # Text macros new in beta 8

# If F_VT420_FEATURES=1, then:
# Eventually:  "rectangular area operations, page memory, L/R margins"
VT420_FEATURES='rectangular area operations'
#              "|------Max Length----------------------------------------|"

# Eventually: "PCTERM, VTNT, win32 and emacs keyboard modes"
KB_MODES="PCTERM and VTNT direct keyboard modes"
#        "|------Max Length-----------------------------|"

SPACE=6

# Clear the screen
printf '\x1b[2J'

# Normal attributes
printf '\x1b[0m'

# Go to 1,1
printf '\x1b[1;1f'

# Lines 1 and 2: - the heading
printf '\x1b#3'  # DECDHL ON - Top Half
printf "$BANNER_FMT" "$VERSION"
printf '\x1b#4'  # DECDHL ON - Bottom Half
printf "$BANNER_FMT" "$VERSION"

# IF we support ruled lines, put one under the heading.
if [ "$F_RULED_LINES" = "1" ]; then
	# Start column 2, 78 columns wide (columns 2-79)
	# On line 2, only one line high
	# Only bottom border
	printf '\x1b[1;2;78;2;1,r'
fi

# Line 3 - some space
printf '\n'

# Line 4 - Standard Attributes
printf ' * \x1b[3mOptional\x1b[0m true attribute support:'
printf ' \x1b[2mDim\x1b[0m'
printf ' \x1b[1mBold\x1b[0m'
printf ' \x1b[3mItalic\x1b[0m'
printf ' \x1b[4mUnderline\x1b[0m'
if [ "$F_STRIKETHROUGH" = "1" ]; then
	printf ' \x1b[9mCrossed-out\x1b[0m'
fi
printf ' \x1b[5mBlink\x1b[0m'
printf '\n'

# Line 5 - Extended Underline Attributes
if [ "$F_EXTENDED_UL" = "1" ]; then
	printf ' * Extended underline styles:'
	printf ' \x1b[4:2mDouble\x1b[0m'
	printf ' \x1b[4:4mDotted\x1b[0m'
	printf ' \x1b[4:5mDashed\x1b[0m'
	printf ' \x1b[4:3mCurly\x1b[0m'
	printf ' \x1b[3;4:3;58:2:240:143:104mWith colour!\x1b[0m\n'

	SPACE="$(($SPACE-1))"
fi

# Line 4 or 5: 24-bit colour
if [ "$F_TRUE_COLOR" = "1" ]; then
	# K95 supports both the correct format (below), and the old incorrect
	# semicolon-delimited color specifier used by some other terminals.
	printf ' * Full '
	printf '\x1b[38:2:63:158:82m2'
	printf '\x1b[38:2:175:201:147m4'
	printf '\x1b[38:2:242:0:72m-'
	printf '\x1b[38:2:160:24:149mb'
	printf '\x1b[38:2:29:193:136mi'
	printf '\x1b[38:2:150:106:138mt '
	printf '\x1b[38:2:192:247:226mc'
	printf '\x1b[38:2:93:173:247mo'
	printf '\x1b[38:2:27:80:160ml'
	printf '\x1b[38:2:108:181:108mo'
	printf '\x1b[38:2:211:93:57mu'
	printf '\x1b[38:2:27:226:107mr'
	printf '\x1b[38:2:164:229:13m!'
	printf '\x1b[0m\n'

	SPACE="$(($SPACE-1))"
fi

# Line 6 or 7
if [ "$F_RULED_LINES" = "1" ]; then
	LINE=6
	if [ "$F_EXTENDED_UL" = "1" ]; then
		LINE=7
	fi

	printf ' * DECterm Ruled Lines\n'

	# This should put a box around the "Ruled Lines" text.
	printf '\x1b[15;12;10;%d;1,r' $LINE
	SPACE="$(($SPACE-1))"
fi

if [ "$F_SOFT_FONT" = "1" ]; then

	printf ' * VT220 '

	# Download the Jetpac font by Paul Flo Williams
	# https://vt100.net/dec/vt320/fonts (no license stated)
	# This is really a VT320 font, not a VT520 font like we *should* be
	# using.
	printf '\x1bP0;1;1;0;0;2;0;0;{B???????www?????/???????JJJ?????;
	????KKK???KKK??/???????????????;??__ooo__ooo__?/??CCNNNCCNNNCC?;
	??__oOOwwOOO???/????DDDNNDDFAA?;??OwgwO?_oWG???/????GKEB@CMIMC?;
	????OwgggWO?__?/??EFNHGGLFAMLH?;?????GGKCC?????/???????????????;
	?????_owGCC????/?????BFNGOO????;?????CCGwo_????/?????OOGNFB????;
	???___?oo?___??/???AAA@FF@AAA??;???????ooo?????/???@@@@FFF@@@@?;
	???????????????/????__oo[[KK???;???????????????/????@@@@@@@@@??;
	???????????????/??????KKKK?????;????????_ow[KC?/???OW[MFB@?????;
	??wwGGGGGGGGww?/??NNGGGGGGNNNN?;???????ww??????/?????NNNN??????;
	??GGGGGGGGGGww?/??NNNNHHHHHHHH?;??GGGGGGGGww???/??HHHHHHHHNNNN?;
	??ww????????ww?/??@@@@@@@@NNNN?;??wwGGGGGGGGGG?/??HHHHHHHHNNNN?;
	??wwGGGGGGGGGG?/??NNNNHHHHHHNN?;??GGGGGGGGGGww?/??????????NNNN?;
	????wwGGGGww???/??NNNNHHHHHHNN?;??wwGGGGGGGGww?/??HHHHHHHHNNNN?;
	??????oooo?????/??????KKKK?????;???????oooo????/???__oo[[KK????;
	??????__oOWGG??/????@@BAECKGG??;????_________??/????AAAAAAAAA??;
	???GGWOo__?????/???GGKCEAB@@???;???OOWGGGGwwo??/??????IIJ@@????;
	??owwKCssS[{wo?/??BFFKGHJIIJJ@?;????wwGGGGww???/??NNNN@@@@@@NN?;
	??wwGGGGGGww???/??NNNNHHHHHHNN?;??wwGGGGGGGGWW?/??NNNNGGGGGGKK?;
	??wwGGGGGGGGww?/??NNNNGGGGGGNN?;??wwGGGGGGGGGG?/??NNNNHHHHHHHH?;
	??wwGGGGGGGGGG?/??NNNN@@@@@@@@?;??wwGGGGGGGGWW?/??NNNNGGGGHHNN?;
	??ww????????ww?/??NNNN@@@@@@NN?;??????ww???????/??????NNNN?????;
	??????????ww???/??KKGGGGGGNNNN?;??ww??????ww???/??NNNN@@@@@@NN?;
	??ww???????????/??NNNNGGGGGGGG?;?wwGGGGGwwGGGww/?NNNN???NN???NN;
	??wwGGGGGGGGww?/??NNNN??????NN?;??wwGGGGGGwwww?/??NNGGGGGGGGNN?;
	??wwGGGGGGGGww?/??NNNN@@@@@@@@?;??wwGGGGGGGGww?/??NNGGGGGGNNNN?;
	??wwGGGGGGww???/??NNNN@@@@@@NN?;??wwGGGGGGGGWW?/??LLHHHHHHNNNN?;
	??GGGGGwwGGGGG?/???????NNN?????;??ww????????ww?/??NNGGGGGGNNNN?;
	??Ww_??????_wW?/???@FNNMGGMF@??;?ww?????ww???ww/?NNNNGGGNNGGGNN;
	??ww????????ww?/??MMNN@@@@@@MM?;??ww????????ww?/??@@@@NNNN@@@@?;
	??WWGGGGGGGGww?/??NNNNHHHHHHLL?;?????wwwwGG????/?????NNNNGG????;
	???GWwwo_??????/??????@BFNMKG??;?????GGwwww????/?????GGNNNN????;
	????_owWGWwo_??/???????????????;???????????????/??GGGGGGGGGGGG?;
	?????CCKGG?????/???????????????;??__________???/??MMIIIIIINNMM?;
	??ww__________?/??NNMMGGGGGGNN?;??____________?/??NNMMGGGGGGLL?;
	??__________ww?/??NNMMGGGGGGNN?;??____________?/??NNMMIIIIIIJJ?;
	?????wwGGGGGG??/???@@NNNN@@????;??____________?/??nnggggggmm~~?;
	??ww__________?/??NNMM??????NN?;??????gg???????/??????NNMM?????;
	??????????gg???/??oo______~~}}?;??ww??????__???/??NNMMAAAABBMM?;
	?????ww????????/?????NNNN??????;??____________?/??NNMM??NN??NN?;
	??____________?/??NNMM??????NN?;??____________?/??NNMMGGGGGGNN?;
	??____________?/??~~wwGGGGGGNN?;??____________?/??NNGGGGGGww~~?;
	??____________?/??NNMM?????????;??____________?/??JJIIIIIIMMMM?;
	????ww______???/????NNMMGGGG???;??__????????__?/??NNGGGGGGMMNN?;
	??__????????__?/???@BEMMKKEB@??;??__????__??__?/??NNMMGGNNGGNN?;
	??__????????__?/??LLMMAAAAAALL?;??__????????__?/??nnmmgggggg~~?;
	??____________?/??MMMMIIIIIIJJ?;???????owGKCC??/????@@@EMGWOO??;
	???????{{{?????/???????^^^?????;???CCKGwo??????/???OOWGME@@@???;
	??_owWWwo__wwW?/??@@@???@@@@???\x1b\\'

	# Switch to ISO-2002 mode
	printf '\x1b%%@'

	# Switch to the soft character set
	printf '\x1b(P'

	# Output some text in the soft character set
	printf 'soft character sets\n'

	# Switch back to US ASCII
	# (this doesn't seem to work in Windows Terminal 1.22.11141.0)
	printf '\x1b(B'

	# And back to UTF-8 mode
	printf '\x1b%%G'

	SPACE="$(($SPACE-1))"
fi

printf ' * VT320 host-programmable status line (see the bottom of the terminal)\n'

if [ "$F_VT420_FEATURES" = "1" ]; then
	# Define a text macro with ID 0. If it works, "text macros" should
	# appear in the list of features!
	printf '\x1bP0;0;0!ztext macros, \x1b\\'

	# Output the VT420 features list
	printf ' * VT420 \x1b[0*z%s\n' "$VT420_FEATURES"
	SPACE="$(($SPACE-1))"
fi


printf ' * Dozens of other emulations:\tADDS25\tADM3A\tADM5\tAIXTERM\tANNARBOR'
printf '\n\tANSI-BBS\tAT386\tBA80\tBETERM\tDG200\tDG210\tDG217\tHEATH19'
printf '\n\tHFT\tHP2621A\tHPTERM\tHZ1500\tIBM3151\tLINUX\tQANSI\tQNX\tSCOANSI'
printf '\n\tSNI-97801\tSUN\tTTY\tTVI910+\tTVI925\tTVI950\tVC404\tVIP7809'
printf '\n\tVT52\tVT100\tVT102\tVT220\tVT320\tVTNT\tWY30\tWY50\tWY60'
printf '\n\tWY160\tWY360\t\t\x1b[3m(completeness varies, see manual for details)\x1b[0m'
printf '\n'

printf ' * Full keyboard remapping, plus %s\n' "$KB_MODES"
printf ' * Connect via SSH, Serial, Modem, Named Pipe, Telnet, Rlogin, PTY, RFC2271...\n'
printf ' * Fully scriptable with Kermit script and REXX\n'
printf ' * In-band file transfer with complete Kermit protocol implementation '
printf 'including\n   server mode, plus X/Y/Z-MODEM\n'
printf ' * Available for 32bit and 64bit Microsoft Windows on all CPU '
printf 'architectures,\n   with reduced feature builds for vintage Windows '
printf 'and 32bit IBM OS/2 Systems\n'

# -------------------------------------------------------------------
# NOTE: No more space for more lines of text if all feature variables
# 	(eg, $F_TRUE_COLOR) are enabled up the top
# -------------------------------------------------------------------

# Status line type to host-writable
printf '\x1b[2$~'

# Move to the status line
printf '\x1b[1$}'

# Move to the start of the status line
printf '\x1b[0`'

# Output some demo text to the status line
if [ "$F_TRUE_COLOR" = "1" ]; then
	for ((i = 255 ; i > 0; i -= 37)); do printf '\x1b[48:2:0:0:%dm ' $i; done
	printf '\x1b[0mHost-programmable status line!'
	for ((i = 0 ; i <= 255; i += 6)); do printf '\x1b[48:2:0:0:%dm ' $i; done
else
	printf ' >>> Host-programmable status line! <<<'
fi

printf '\x1b[0m'

# Move back to the terminal screen
printf '\x1b[0$}'

# Put a blank line between the feature list and the "press any key" prompt if
# there is space available to do so.
if [ $SPACE -gt 0 ]; then
	printf '\n'
fi

# Make the prompt blink, and center it a little, so its easier to spot at the
# bottom of the feature list. Important if there isn't any visual separation
# between it and the list. And turn the cursor off.
printf '\x1b[5m\t\t\t'

# Cursor off
printf '\x1b[?25l'

read -n 1 -s -r -p "Strike any key to continue..."
# Blinking off
printf '\x1b[0m\n'

# Cursor back on
printf '\x1b[?25h'

# Status line back to indicator, and ensure attributes are back off.
printf '\x1b[1$~\x1b[0m\x1b[25h'

if [ "$F_SOFT_FONT" = "1" ]; then
	# Erase font buffers
	printf '\x1bP0;0;2{ @\x1b\\'
fi

# Done!



