#!/data/data/com.termux/files/usr/bin/env etsh
:
: etsh - " Force sh(1), csh(1), and other shells to exit w/ error! " <'' ;;;
:
: "  @(#)$Id: history,v 1.3 2018/12/15 22:28:21 jneitzel Exp $  "
:
: "  The author of this file, J.A. Neitzel <jan (at) etsh (dot) nl>,  "
: "  hereby grants it to the public domain.                           "
:

: " Script initialization "
trap	:		13
umask	77
set	H		$h/.etsh.history; : " user's etsh history file "
set	N		history		; : " script name              "
: set
alias	fd2lncat	"fd2 -f /dev/null awk '{ print NR "\"\\t\"" \$0 }'"
alias	lncat		"awk '{ print NR "\"\\t\"" \$0 }'"
: alias

: " Check for correct usage. "
: >>$0'' <'' ; : >>$1'' <'' ; : >>$2'' <''
if ! { expr //$0 : '.*/\(.*\)' = $N >/dev/null } kill -9 $$
if \( \
	$# = 0 -o X$1 = X-l -o X$1 = X-n -o X$1 = X-p \
   \) -a ! \( \
	-f $H -a -r $H -a -w $H \
   \) if { exit } fd2 -e echo "$N: $H: cannot open" ; false
if $# = 0 goto -a
if \( \( X$1 = X-c -o X$1 = X-l -o X$1 = X-r \) -a $# = 1 \) -o \
   \( \( X$1 = X-n -o X$1 = X-p              \) -a $# = 2 -a X$2 != X \) goto $1
if X$1 = X-h -a $# = 1 goto $1
if { exit } fd2 -e \
echo "usage: $N [-c | -h | -l | -n number1[,number2] | -p pattern | -r]" ; false

: -a
	wc -l <$H | tr -d ' \t' | \
		sed 's,.*,expr & \\>= 1 >/dev/null,' | etsh
	if $? != 0 if { exit } false
	echo 'Number	Command Line' ; echo '------	------------'
	fd2lncat <$H ; exit

: -c
	( : ) >$H ; exit

: -h
	fd2 -e echo "$N: Paging history help..."
	if ! { printenv PAGER } goto more-s >/dev/null
	printenv PAGER | wc -c | tr -d ' \t' | grep '^1$' >/dev/null
	if $? = 0 goto more-s
		printenv PAGER | sed 's,.*,exec & '$0'.help,' | etsh ; exit
	: more-s
		more -s $0.help ; exit

: -l
	wc -l <$H | tr -d ' \t' | \
		sed 's,.*,expr & \\>= 11 >/dev/null,' | etsh
	if $? != 0 if { exit } false
	echo 'Number	Command Line' ; echo '------	------------'
	lncat <$H | tail -11 | head -10 ; exit

: -n
	fd2lncat <$H | fd2 -f /dev/null -- sed -n $2p | \
		wc -l | tr -d ' \t' | grep '^0$' >/dev/null
	if $? = 0 if { exit } false
	echo 'Number	Command Line' ; echo '------	------------'
	lncat <$H | sed -n $2p ; exit

: -p
	lncat <$H | egrep -- $2 | grep -v 'history -p' | \
		wc -l | tr -d ' \t' | grep '^0$' >/dev/null
	if $? = 0 if { exit } false
	echo 'Number	Command Line' ; echo '------	------------'
	lncat <$H | egrep -- $2 | grep -v 'history -p' ; exit

: -r
	rm -f $H
