#!/bin/sh
# tester.sh - 
# author : dave w capella     dave@grox.net
# date     : Wed Mar 19 11:34:27 PDT 2008
############################################################
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/X11R6/bin
PRG=`basename $0`
NOW=`date`

############################################################
# read multiple variables from each line of a file or command output
# eg., /etc/hosts:
#	127.0.0.1       localhost
#	::1     ip6-localhost ip6-loopback
#	fe00::0 ip6-localnet
#	ff00::0 ip6-mcastprefix
#	ff02::1 ip6-allnodes
#	ff02::2 ip6-allrouters
#
#while { read ip name ; [ -n "$ip" -a -n "$name" ] ; } ; do
#	echo "ip: $ip  name: $name"
#done < /etc/hosts


#############################################################
## colorized output
##
#esc='\0033'
#norm="${esc}[m"
#lo="0"
#hi="1"
#prefix="${esc}["
## 0-7 == black, red, green, yellow, blue, magenta, cyan, white
#bg="4"
#fg="7"
#whiteonblue="${prefix}${lo};3${fg};4${bg}m"
#echo -n -e $whiteonblue
#echo "------------------------------------------"
#ls
#echo -n -e $norm
#echo "------------------------------------------"
#ls


############################################################
# find example to skip dir's
#
#    find . -name '*xslt' -not -path .svn ; exit
#
# and another method:
# find all top-level dirs except /home/ARCHIVED & don't descend other filesystems
#
# cd /home
# find . -xdev -mindepth 1 -maxdepth 1 -type d -path ./ARCHIVED -prune -o -print


# retain multi-line output when used in a loop
# this works. 1/14/10
#
dosearch() {
  search=$1
  OLDIFS=$IFS
  IFS=$'\n'
  query=
  for f in `getcolumns` ; do
    if [ -n "$query" ] ; then
      query="$query or $f like '%$search%'"
    else
      query="$f like '%$search%'"
    fi
  done
  IFS=$OLDIFS
  query="\
.mode line
select oid, * from contacts where
$query ;
"
    # use here doc to preserve line breaks
    #sqlite $rolofile <<EOM
    cat <<EOM
$query
EOM
}


# this works
#
#ls /tmp
#[ $? ] && echo success || echo failure
#exit

# this works. uses eval to get var names
#
ANS=
REPLY=
cmds="summary description location start end alarm repeat view help quit"
summary=
description=
location=
start=
end=
alarm=
repeat=
event=
select ANS in $cmds ; do
  case $ANS in
    summary|description|location|start|end|alarm|repeat)
      echo type the ${ANS} as a single line. type ENTER at the next prompt.
      eval "read $ANS"
      eval name=$`echo $ANS`
      if [ -n "$event" ] ; then
        event="$event
$ANS : $name"
      else
        event="$name : $ANS"
      fi
      ;;
    view)
      cat <<EOM
$event
EOM
      ;;
    help)
      dohelp
      ;;
    quit)
      if [ -n "$summary" ] ; then
        break
      else
        echo summary field cannot be empty.
      fi
      ;;
  esac
done

# $$ - pid of the shell
# $! - pid of last background command
# $? - exit status of last command
#
#exec aucat -i /home/dave/sound/spots/Rob1.wav &
#echo "shell: $$  aucat: $!"
#echo
#exit

#exec > tester.log 2>&1
#if ! touch /root/foo ; then
#	echo "cannot touch /root/foo" >&2
#	exit 1
#fi

############################################################
WED_DOW="3"              # `date +%u` for wednesday == 3
TODAY_DOW=`date +%u`
if expr $TODAY_DOW ">" $WED_DOW >/dev/null ; then
	echo greater
elif expr $TODAY_DOW "=" $WED_DOW >/dev/null ; then
	echo equal
else
	echo less
fi
exit
############################################################

( alias cp && unalias cp
	alias rm && unalias rm
	alias mv && unalias mv
) >/dev/null 2>&1
exit

#( DISPLAY=:0.1 ; xterm & )

tst="foo"
tst1="bar"
expr $tst : ".*o" >/dev/null && echo '.*o' || echo 'NO: .*o' 
expr $tst : "o" >/dev/null && echo 'o' || echo 'NO: o' 

exit

#echo "--------------------------"
#echo $*
#echo "--------------------------"
#echo $@
#exit

p2(){
	echo MYA=hello
	echo MYB=world
}

eval `p2`
echo "MYA: $MYA   MYB: $MYB " 
exit

eval `python -c "print 'FOO=bar ; BAR=baz'"`
echo "foo: $FOO   bar: $BAR"
exit

#ishost() {
#	host "$1" >/dev/null 2>&1
#}
#ishost "$1"  && echo y || echo n
#exit

# single quotes doesn't stop var expansion. below works.
#echo " PRG: '$PRG'  NOW: '$NOW' " ; exit

# passes stdin to children
#tr '[a-z]' '[A-Z]' ; exit

# hangs if no input provided on command line
#QUERY=`cat` ; echo "query: $QUERY" ; exit

# does not work - broken pipe
#while [ -n "$1" ] ; do
#	case "$1" in
#		y) echo $1 ;;
#		n) echo $1 ;;
#		*) echo blah ;;
#	esac
#	shift
#done
#set -- ; echo "#: $#" ; exit

tee tst <<-EOM
	this is a test
	this is only a test
	EOM
exit

#TESTDIR=/tmp/testdir
#OUTDIR=/tmp/outdir
#CGZ=/tmp/tst.cgz
#OUTCGZ=/tmp/out.cgz
#[ -d $TESTDIR ] && rm -rf $TESTDIR/* || mkdir $TESTDIR
#cd $TESTDIR
#echo aaaaaaaaaa > a
#echo bbbbbbbbbb > b
#echo cccccccccc > c
#find . | cpio -o -H newc | gzip -9 > $CGZ
#
#if [ -d $OUTDIR ] ; then
#	echo "$OUTDIR exists. i quit."
#	exit 1
#else
#	mkdir $OUTDIR
#fi
#cd $OUTDIR
#echo "need to su now..."
#su -c "gzip -dc $CGZ | cpio -iumd
#mkdir adir bdir cdir
#install -o 0 -g 0 -m 755 $TESTDIR/a adir
#install -o 0 -g 0 -m 755 $TESTDIR/b bdir
#install -o 0 -g 0 -m 755 $TESTDIR/b cdir
#find . | cpio -o -H newc | gzip -9 > $OUTCGZ
#chown dave:dave $OUTCGZ
#cd ..
#rm -rf $OUTDIR
#"
#ls -l /tmp
#exit

cd /sys/block
for f in [hs]d? ; do
	test "`cat $f/removable`" = "1" && continue
	awk '{ m=$0/2000 ; print int( (m+512)/1000 ); }' $f/size
done
exit

############################################################
PASS=
echo -n "ssh password: "
stty -echo
read PASS
stty echo

expect <<EOM
set timeout 3
spawn ssh -o "PreferredAuthentications password" grox.net ls
expect "assword: "
send -- "$PASS\n"
expect eof
EOM

exit
############################################################

MAXI=3
i=0
cat <<EOM |
aaaa bbbbb cccccc
dddd eeee ffff
gggg hhhh iiii
jjj kkk lll
mmm nnn ooo
EOM
while [ $i -lt $MAXI ] ; do
	i=`expr $i "+" 1`
	read a b c
	[ -n "$a" ] || break
	[ -n "$b" ] || break
	[ -n "$c" ] || break
	echo "i: $i   a: $a   b: $b   c: $c"
done
echo "i: $i"
exit

echo domain: `hostname|cut -d. -f2-`
exit

#TST="arg, arg1,	arg2, arg3,		arg4,arg5"
#TST1=`echo $TST | tr -d ' \t' | tr -s ',' ' '`
#echo "'$TST1'"
#exit

echo '0:e0:81:4a:04:4' |
	awk -F: '{
		for(i=1;i<6;i++){
			printf("%02s:",$i)
		}
		printf("%02s",$i)
	}'

exit

cleanup(){
	rm -f /tmp/tst
	echo "ls /tmp/tst:"
	ls /tmp/tst
	exit 0
}

argtest(){
	echo "args: $#"
}

listifs_Linux(){
	( cd /sys/class/net; echo eth* )
}

eval listifs_`uname`

#echo 'argtest with no args:'
#argtest
#echo 'argtest with empty arg:'
#argtest ""
#echo 'argtest with one arg:'
#argtest "one"
#exit

## doesn't work
#[ `hostname` = "*danger*" ] && echo "if: at work" || echo "if: elsewhere"
#case `hostname` in
#	*danger*) echo "case: at work" ;;
#	       *) echo "case: elsewhere" ;;
#esac

#[ -e /tmp/tst ] || touch /tmp/tst
#trap cleanup TERM QUIT INT
#while true ; do sleep 60 ; done

#rm -f /tmp/tst
#FOO=`ls /tmp/tst 2>/dev/null`
#echo "before: $FOO"
#FOO=`cd ; touch /tmp/tst ; cd /tmp ; ls tst`
#echo "after: $FOO"

#echo "args: $#"
#
#HOSTS=`cut -f1 $HOME/danger/DAILYSI00,01-to-SE1SI00,01/macs.se1`
#
#count=0
#for f in $HOSTS ; do 
#	count=`expr $count "+" 1`
#	echo "count: $count   host: $f"
#done
#
#cat <<EOM
#=============================================
#HOSTS:
#=============================================
#EOM
#echo $HOSTS

######################################################################
# eof: tester.sh

