: /usr/pkg/etc/etsh.etshrc - " Modify to taste. "
:
: "  The author of this file, J.A. Neitzel <jan (at) etsh (dot) nl>,  "
: "  hereby grants it to the public domain.                           "
:
: "  From:  https://etsh.nl/examples  "
:

trap : 1 2 3 13 14 15 ; : " Ignore HUP, INT, QUIT, PIPE, ALRM, and TERM. "
trap : 18 21 22 ;       : " Ignore job-control signals: TSTP, TTIN, TTOU "

set F /usr/pkg/etc/etsh.etshrc

: fd2 -e echo "debug: Executing `$F' now..."

if { printenv TERM >/dev/null } goto TermIsSet
	fd2 -e echo "$F: TERM unset: defaulting to TERM=vt100"
	setenv	TERM	vt100
	: fallthrough
: TermIsSet
printenv TERM | wc -c | tr -d ' ' | grep '^1$' >/dev/null
if $? -eq 1 goto TermIsSet1
	fd2 -e echo "$F: TERM invalid: defaulting to TERM=vt100"
	setenv	TERM	vt100
	: fallthrough
: TermIsSet1

: verbose true
set D /tmp/etsh-$u-$$
if ! { mkdir -m 0700 $D } goto HOST+H-Done
	uname -n | sed 's,\([^.]*\).*,setenv HOST \1,' >$D/HOST
	. $D/HOST
	( echo -n set H \" ; \
	  printenv HOST | tr -d \\n ; echo \" ) >$D/SetH
	. $D/SetH
	rm -rf $D
: HOST+H-Done
: verbose false

: " ==== Begin PATH ==== "

set   X "/usr/pkg" ; : " etsh install PREFIX "

:
: " The following sets up the user's PATH, searching for and prepending - "
: "	$h/bin, (/usr/local|$h)/v6bin, and $X/libexec/$v/etsh             "
: " - if found.  $v == etsh version; $h == HOME (aka ~).                  "
:

unset Z S D C B A
set   A '' ; set   B '' ; set C ''
: fd2 -e echo "1: \$p == $p"
set Z $p

:
: " Add $h/bin if it exists as a searchable directory. "
:
set D $h/bin
echo $Z | grep $D: >/dev/null
if $? -eq 0 goto Jump
if ! \( -n "$h" -a -d "$h" -a -d $D -a -x $D \) goto Jump
	set C $D
	: fallthrough
: Jump
	: fallthrough

:
: " Add (/usr/local|$h)/v6bin if it exists as a searchable directory. "
:
set D /usr/local/v6bin
echo $Z | grep $D: >/dev/null
if $? -eq 0 goto Jump1
if ! \( -d $D -a -x $D \) goto Jump1
	set B $D
	goto Jump1-Done
: Jump1
set D $h/v6bin
echo $Z | grep $D: >/dev/null
if $? -eq 0 goto Jump1-Done
if ! \( -n "$h" -a -d "$h" -a -d $D -a -x $D \) goto Jump1-Done
	set B $D
	: fallthrough
: Jump1-Done
	: fallthrough

:
: " Add $X/libexec/$v/etsh if it exists as a searchable directory. "
: " Otherwise, print an error message.                             "
:
set D $X/libexec/$v/etsh
echo $Z | grep $D: >/dev/null
if $? -eq 0 goto Jump2
if ! \( -d $D -a -x $D \) goto LibexecError
	set A $D
	: fallthrough
: Jump2
	goto Continue
: LibexecError - " In short, this means the installed etsh package is broken. "
	fd2 -e echo "$F: $D: not found as a searchable directory"
	: fallthrough

: Continue - " ==== PATH - The following can turn into a big no-op. ==== "

set S ''
if -z "$A" goto NoA
	set S $A:
	: fallthrough
: NoA
if -z "$B" goto NoB
	set S $S$B:
	: fallthrough
: NoB
if -z "$C" goto NoC
	set S $S$C:
	: fallthrough
: NoC
if -z "$S" goto NoS
	set Z $S$p
	: fallthrough
: NoS
: fd2 -e echo "-: \$Z == $Z"

setenv PATH $Z
: fd2 -e echo "2: \$p == $p"
: fd2 -e echo "debug: PATH == $p"
unset A B C D F S X Z

: " ==== End PATH ==== "

trap - 1 2 3 13 14 15 ; : " Reset the ignored, non-job-control signals. "
