#	.kshrc_bash - a script file to be shared between ksh and bash

#  Henry Grebler    28 Jun 10  Accommodate WEHI.
#  Henry Grebler    06 Apr 10  Change lesh.
#  Henry Grebler    05 Apr 10  Change help.
#  Henry Grebler    15 Jan 10  Fix gt_doit.
#  Henry Grebler    30 Oct 01  Fix gttvu.
#  Henry Grebler    28 Aug 01  Fix/improve dsdd.
#  Henry Grebler    10 Aug 01  Make the gzip stuff do bzip2 too.
#  Henry Grebler     6 Aug 01  Add dsdd - list in directory order.
#  Henry Grebler    30 May 01  Improve df.
#  Henry Grebler     9 May 01  New printer.
#  Henry Grebler    29 Mar 01  Expand locate.
#  Henry Grebler    11 Jan 01  Add locate.
#  Henry Grebler    15 Dec 00  Add -vall to truss.
#  Henry Grebler     2 Oct 00  Improve dsd.
#  Henry Grebler    ?? Mar 00  Improve loc. Add locl.
#  Henry Grebler    16 May 00  More to truss.
#  Henry Grebler     3 May 00  Add some zip functions.
#  Henry Grebler    25 Jan 00  Modify mcd.
#  Henry Grebler    21 Oct 99  Add urls.
#  Henry Grebler     3 Sep 99  Add cvtslash.
#  Henry Grebler    18 Aug 99  Accept stdin for printl.
#  Henry Grebler    29 Jul 99  On Linux, use 'more' as PAGER for apropos.
#  Henry Grebler    21 Apr 99  Add gttvu.
#  Henry Grebler    20 Apr 99  Create lesh.
#  Henry Grebler    16 Mar 99  Upgrade truss to use strace on Linux.
#  Henry Grebler    28 Jan 99  Upgrade help.
#  Henry Grebler    13 Jan 99  Augment truss. Add pp. Augment help.
#  Henry Grebler    15 Oct 98  Create mcd.
#  Henry Grebler    28 Sep 98  Augment help.
#  Henry Grebler    21 Sep 98  Augment lest.
#  Henry Grebler     7 Sep 98  Provide arg to dth.
#  Henry Grebler     7 Sep 98  Provide protection to dc.
#  Henry Grebler    31 Aug 98  Upgrade printl.
#  Henry Grebler    12 Aug 98  Add help.
#  Henry Grebler    10 Aug 98  Fix truss.
#  Henry Grebler    24 Jul 98  Add showplain.
#  Henry Grebler    15 Jul 98  Send truss output to stderr.
#  Henry Grebler    14 Jul 98  Create functions dsd and dsdc.
#  Henry Grebler    22 Jun 98  Create function truss.
#  Henry Grebler    19 May 98  Augment lest.
#  Henry Grebler    15 May 98  Change nc from alias to function.
#  Henry Grebler    28 Apr 98  Add lest.
#  Henry Grebler    16 Feb 98  Add log.
#  Henry Grebler     7 Nov 97  Add watch.
#  Henry Grebler     7 Nov 97  Add mangl.
#  Henry Grebler    29 Oct 97  Add John's gnatsq.
#  Henry Grebler    20 Oct 97  Fix printl.
#  Henry Grebler    17 Jul 97  Add cvtether to convert from : notation.
#  Henry Grebler    11 Sep 96  Improve gt functions.
#  Henry Grebler    21 Aug 96  Fix nice for the umpteenth time.
#  Henry Grebler    19 Jul 96  Add ping for SunOS 5. Start to keep functions
#				in alphabetical order.
#  Henry Grebler    24 Nov 95  Add printl.
#  Henry Grebler    23 Nov 95  Have I finally got nice correct?
#  Henry Grebler     4 May 95  Improve gt routines (gty gtx gtxv gttv).
#  Henry Grebler    15 Dec 94  Avoid loops in nice and nloc.
#  Henry Grebler    21 Sep 94  Add gttv, gtxv, gtx.
#  Henry Grebler    21 Sep 94  Add nloc as function nice loc.
#  Henry Grebler    12 Sep 94  Add links.

#	Usage:	At the bottom of .kshrc and .bashrc there will be 
#		commands to invoke this script. Commands will only go in 
#		here if they can be phrased in a compatible way. Other
#		commands will remain in the individual files.

#		I do not propose to try to be optimal. In many cases 
#		commands will exist in the calling script(s) and this 
#		script. Eventually, I may sort that out.

#		Format is that of ksh since bash is less restrictive

function tyc {
	cat -v $* | more
}

function links {
	ls -la $* | grep "^l" | more
}

if [ "`uname`" = Linux ]
then
function apropos () {
	PAGER=more command apropos $*
}
fi


#	Convert eg 8:0:b:0:0:40 -> 08000B000040

function cvtether () {
	for i
	do
		echo $i | awk -F: '{printf "%02s%02s%02s%02s%02s%02s\n", \
			$1, $2, $3, $4, $5, $6}' | tr a-z A-Z
	done
}

#----------------------------------------------------------------------#
cutf () 
{ 
    awk '{print $'$1'}'
}

#----------------------------------------------------------------------#
function cvtslash () {
backslash='\\'
	xxx1=`echo $* | tr '/' "$backslash"`
	xxx2=`echo $* | tr "$backslash" '/'`
	echo "$xxx1"
	echo "$xxx2"
	if [ "$xxx1" = "$xxx2" ]
	then
		cat <<EOF
I detected no difference. Perhaps you tried to convert a DOS path
to Unix and didn't wrap it in quotes. The backslashes would have
been eaten by the shell and I would not have seen them.
EOF
	fi
}


function dc {
	if [ $# -eq 1 -a -d "$1" ]
	then
		/bin/echo '\007'"you probably mean 'cd' not 'dc'"
		cd $1
	else
		command dc $*
	fi
}

#------------------------------------------------------------------------
# Conditional definition of df

# Note aix and hpux are not great.

	dfx=`alias df 2> /dev/null | cut -d\' -f2`
	if [ "$dfx" = "" ]
	then
		dfx=df
	else
		unalias df
	fi
	
#------------------------------------------------------------------------
function dirs {
	ls -la $* | grep "^d" | more
}

function dirsl {
	ls -laF $* | grep "/$" | more
}

function dth {
	case $1 in
		+*)	lines=-`echo $1 | cut -c2-`;shift;;
	esac
	ls -lat "$@" | head $lines
}

#------------------------------------------------------------------------
# Conditional definition of dsd

	if [ `uname` = SunOS ]
	then
		if [ `uname -r | cut -d. -f1` -lt 5 ]
		then
			platform=sunos4
		fi
	fi

	if [ "$platform" = sunos4 ]
	then	#--------------------------------------------------------

function dsd {
	F=Ag
	ls -l$F "$@" | grep -v '~$' | cut -c1-28,34- | more
	stty cs8
}

	else	#--------------------------------------------------------

function dsd {
	F=A
	ls -l$F "$@" | grep -v '~$' | more
}
	fi
#------------------------------------------------------------------------

function dsdc {
	dsdc_today="`date '+%b %e'`"
	dsd $* | grep "$dsdc_today"
}

# List directory in directory order
function dsdd {
	while test $# -gt 0
	do
		case "$1" in
			-*)	args="$args $1";;
			*)	break;;
		esac
		shift
	done
	if test $# -eq 0
	then
		p1=.
	else
		p1="$*"
	fi
	find $p1 -exec ls -lad $args {} \; \( -exec test {} != $p1 \; -a \
		-type d \) -prune
}

# List directory with full date

function dsdl {
	dsd --time-style=long-iso "$@"
}

# Translation: find in <supplied_args> or current dir
#		if 
#------------------------------------------------------------------------
function files {
	ls -la $* | grep "^-" | grep -v "~$" | more
}

function d {
	cd ./$*
}

function gnatsq () { 
    query-pr -c "$*" -s open -q | cut -c5-9,37-38,47-48,56,67-
}

#------------------------------------------------------------------------
function enable () {
	echo 'For bash enable, use "bash_enable"'
	path=`type -a enable | command grep '^enable is /' | \
		head -1 | awk '{print $NF}'`
	if [ "$path" = "" ]
	then
		echo Could not find "enable" in your path
		return 1
	fi
	$path $*
}

function bash_enable () {
	builtin enable $*
}

#------------------------------------------------------------------------
function help {
# Assume no arg means standard help

	if [ "$1" = "" ]
	then
		builtin help
		return
	fi
	echo 'For bash help, use builtin help'

# avoid backup files:
	files=`ls ~/help*/help_*${1}* 2>/dev/null | grep -v "~$"`
	if [ "$files" = '' ]
	then			# no matches: try for bash help
		builtin help $1
		return
	fi
	echo "$files"
	echo 'ls ~/help*/help_*'${1}'* | grep -v "~$"'
	less $files
}

#------------------------------------------------------------------------
# This is now old help.

#------------------------------------------------------------------------
function helpo {
# Assume no arg means standard help

	if [ "$1" = "" ]
	then
		builtin help
		return
	fi
	echo 'For bash help, use builtin help'
#	(set -x; less ~/help/help_*${1}*)
# avoid backup files:	
	(set -x; less `ls ~/help/help_*${1}* | grep -v '~$'`)
	if [ $? -ne 0 ]
	then			# try for bash help
		builtin help $1
	fi
}

#------------------------------------------------------------------------
#	lesh - less the most recent file(s)

# Usage:	lesh [+n] [args]
# 	n	number of files (default: 1)

# Other args	become args of ls so eg -c

function lesh () {
	lines=-1
	case $1 in
		+*)	lines=-`echo $1 | cut -c2-`;shift;;
	esac
	if [ $# -eq 0 ]
	then
		args=.
	else
		args="$*"
	fi
	__files=`find $args -maxdepth 1 -type f`
	less `ls -trd $__files | tail $lines`
	unset lines __files
}

#------------------------------------------------------------------------
#	lesh - less the most recent file(s)

# Usage:	lesh [+n] [args]
# 	n	number of files (default: 1)

# Other args	become args of ls so eg -c

function lesho2 () {
	lines=-1
	case $1 in
		+*)	lines=-`echo $1 | cut -c2-`;shift;;
	esac
	__files=`find $* -maxdepth 1 -type f`
	less `ls -trd $__files | tail $lines`
	unset lines __files
}

#------------------------------------------------------------------------
#	lesho - previous version

# Usage:	lesh [+n] [args]
# 	n	number of files (default: 1)

# Other args	become args of ls so eg -c

function lesho () {
	lines=-1
	case $1 in
		+*)	lines=-`echo $1 | cut -c2-`;shift;;
	esac
#	less `ls -t $* | head $lines`
# the following gives the last few in chronological order; previous reverses.
	less `ls -trd $* | tail $lines`
	unset lines
}

#------------------------------------------------------------------------
# type ls:	ls is hashed (/usr/bin/ls)
# type uue:	uue is aliased to `/work/home/henryg/scripts/uuencode.sh'
# type laserpipe.sh	laserpipe.sh is /work/home/henryg/scripts/laserpipe.sh
function lest () {
	x='`'
	ans=`type $1`
	if [ $? -eq 0 ]
	then
		less `type $1	| cut -d\( -f2 | cut -d\) -f1 \
			| cut -d$x -f2 | cut -d"'" -f1 | awk '{print $NF}'`
	fi
	unset x ans
}

#------------------------------------------------------------------------
function loc  {
	dir=.
	case $# in
		0)	echo Needs at least one arg
			return
			;;
		1)	;;
		2)	dir="$1"
			shift
			;;
		*)	echo Too many args
			return
			;;
	esac
	(set -x; command nice find $dir $xdev -name \*$1\* -print)
}

# locl = locate locally

function locl {
	xdev='-xdev'
	loc "$@"
}

#------------------------------------------------------------------------
function locate_on_optim1 () {

# nd = no directory
# f  = file only (must be the last component only)

	no_dir=false
	file_only=false
	while [ $# -gt 0 ]
	do
		case "$1" in
			-nd)	no_dir=true;;
			-f)	file_only=true;;
			*)	break;;
		esac
		shift
	done
	if "$no_dir"
	then
		xxx="|/[^/]*$1[^/]*/"
	fi
	filter="egrep -v '^/backup$xxx'"
	if "$file_only"
	then
		filter="$filter | grep '/[^/]*$1[^/]*$'"
	fi
	command locate $* | eval $filter
	unset xxx no_dir file_only
}

function locate () {
	if [ `uname` = Linux -o `uname` = FreeBSD -o `uname` = Darwin  ]
	then
		command locate "$@"
	elif [ `uname -n` = optim1 ]
	then
		locate_on_optim1 "$@"
	else
		/work/sysadmin/local/bin_scripts/locate.sh $*
	fi
}

#------------------------------------------------------------------------
function lynx () {
	if [ "$1" = "-f" ]
	then
		lynx_arg='-force_html'
		shift
	fi
	command lynx -cfg=~/lynx.cfg $lynx_arg $*
}

#------------------------------------------------------------------------
function mangl () {
	nroff -man $* | less -is
}

#------------------------------------------------------------------------
# Allow 'mcd -p a/b/c'
function mcd () {
	mkdir $*
	n=`expr $# - 1`
	shift $n
	cd $1
}

#------------------------------------------------------------------------
function nc () {
	if [ $# -le 1 ]
	then
		niscat.sh $1
	else
		niscat.sh $1 | grep $2
	fi
}

function nloc  {
	command nice find . -name \*$1\* -print
}
function nice {
	tt=`type $1 | grep 'is a'`
	if [ $? = 0 ]
	then
		command nice $SHELL -ic "$@"
	else
		command nice "$@"
	fi
}

if [ `uname` = SunOS ]; then if [ `uname -r | cut -f1 -d.` -ge 5 ]
then
	function ping {
		command ping -s $1 56 1
	}
fi;fi


if [ `uname` = Linux -o `uname` = FreeBSD -o `uname` = Darwin ]
then
	function ping {
		command ping -c 1 $1
	}
fi

function pp_ () {
	dir=`builtin dirs |tr " " "\012" | grep "$1"`
	if [ `echo "$dir" | wc -l` -eq 1 ]
	then
		pushd +`bdx | grep $1 | awk '{print $1}'`
	else
		echo "Ambiguous '$dir'"
	fi
}

function pp () {
	case "$1" in
		"")	pushd;;
		+*|-*)	pushd $1;;
		*)	pp_ $1;;
	esac
}

#	gty is gcat | more or ty output of gcat 

function gty  {
	command nice gzip -vdc $* | more
}

export GNUTAR=`$HOME/.ENV./set_gnutar.sh`
echo GNUTAR set to $GNUTAR. Use export GNUTAR=xxx to change.

function gt_doit {
	case $p1 in
		*.bz2)	gzip=bzip2;;
	esac
	command nice $gzip -$garg $p1 | $GNUTAR -$targ - $*
}

function gttv  {
	if echo $1 | grep '\.uu$' > /dev/null 2>&1
	then
		precmd="uudecode -p $1 | "
		shift
	fi
	gzip=gzip; garg=vdc; targ=tvf; p1=$1; shift
	eval $precmd gt_doit| $PAGER
}

# gttvu doesn't need the file to have a .uu filetype

function gttvu  {
	if [ `uname` = Linux ]
	then
		precmd="uudecode -o /dev/stdout $1  | "
	else
		precmd="uudecode -p $1 | "
	fi
	gzip=gzip; garg=vdc; targ=tvf; p1=; shift
#	cmd='command nice gzip -vdc | /bin/nice $GNUTAR -tvf - | $PAGER'
#	eval $precmd $cmd
	eval $precmd gt_doit | $PAGER
}

function gtxv  {
	p1=$1
	shift
	gzip=gzip; garg=vdc; targ=xvpf; gt_doit $*
#		/bin/nice gzip -vdc $p1 | /bin/nice $GNUTAR -xvpf - $*
}

function gtx  {
	p1=$1
	shift
	gzip=gzip; garg=dc; targ=xpf; gt_doit $*
#		command nice gzip -dc $p1 | /bin/nice $GNUTAR -xpf - $*
}

function printl_one {
	if [ "$PRINTL_DEFAULT" = "" ]
	then
		mp -o -l $1 | lp
	else
		mp -o -l $1 | $PRINTL_DEFAULT
	fi
}

function printl {
# Accept stdin
	if [ $# -eq 0 ]
	then
		printl_one
		return
	fi
	for file in $*
	do
		printl_one $file
	done
}

function log {
	if [ $# -eq 0 ]
	then
		read entry
	else
		entry="$*"
	fi
	echo `date "+%y%m%d %H%M"` "$entry" >> $HOME/hmg/times/Log
}

#------------------------------------------------------------------------
sc () {
	if pwd | fgrep -q /gst/
	then
		echo 'Using sc_6.21 to avoid problems'
		sc_6.21 $*
	else
		echo 'Using default sc (sc_hmg, sc_7.16) with strange rounding'
		command sc $*
	fi
}

#------------------------------------------------------------------------
function showplain {
    ( export NOMHNPROC=;
    show $* )
}

#------------------------------------------------------------------------
function truss {
	if [ $# -lt 1 ]
	then
		cat >&2 <<XXX
Usage (for function): truss <command>
which creates /tmp/<command>.truss
Otherwise use `command -v truss`
XXX
		return
	fi
	if [ "$1" = '-p' ]
	then
		output=/tmp/pid_$2.truss
	else
		p1=`basename $1`
		output=/tmp/$p1.truss
	fi
	echo "Use less '+/execve|open.*= [^-]' $output" >&2
	if [ "`uname`" = SunOS ]
	then
		command truss -o $output -fall -rall -wall -vall "$@"
	elif [ "`uname`" = FreeBSD ]
	then
		command truss -o $output -fae "$@"
	else
# at the moment means linux
		strace -o $output -f "$@"
	fi
}
#------------------------------------------------------------------------
function unzipl {
	unzip -p $1 "*$2*" | less
}

function unziplR {
	unzip -p $1 "*README*" | less
}

function urls {
	less `ls -t $HOME/help/URLs/URLs.* | head -1`
}

function watch { 
    x=$1;
    shift;
    command watch $x -no_xterm_title $*
}
