#-----------------------------
# GENERIC SHELL UTILITIES 
#-----------------------------
# Please, include this file in your scripts
# with . /etc/utils 
# (C) M. Andreoli, for muLinux


em()
{
echo -e "${BRIGHT}$@${NORMAL}"| tr -d '\012'
}

abort()
{
echo -e "${BRIGHT}${RED}$@${NORMAL}"
exit 1
}

#----------------------------
# return first un-used ramdisk
#----------------------------

find_ramdisk()
{
for n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
cat /etc/mtab | rgrep /dev/ram${n} > /dev/null
[ $? -ne 0 ] && echo $n && return 0
done
echo 15
}


#------------------------------------------
# check if a mount-point is read-only
#-------------------------------------------

is_ro()
{
mount | rgrep $1 > /dev/null

if [ "$?" -eq 0 ]; then
	mount | rgrep $1 | rgrep "(ro" >/dev/null
	return $?
elif [ -d $1 ]; then	
	mount | rgrep " / " | rgrep "(ro" >/dev/null
	return $?
fi
return 255
}

#----------------------
# non UNIX parser
#---------------------
# Syntax
#       SplitArgs x=1,2 y=a  z
# In this case, put in environment x="1,2", y="a" and z="true"
# Note x="a b" is not allowed.

SplitArgs()
{
local CMDLINE
local val
local var

 CMDLINE="$@"
  for Parameter in $CMDLINE; do
      Parameter=$( IFS='='; echo ${Parameter} )
          set -- $Parameter; var=$1; val=$2
          [ -z "$val" ] && val=true
          eval $var='$(echo $val)'
  done
}

#---------------------
# Array/tables
#----------------------
# syntax:    Array a,b,c,... n
# return n-th value of the list
# without n, return array size

Array()
{
array=$1; n=$2

save=$IFS; IFS=","
set -- $array

# n > 10 ?

if [ "$n" -gt 9 ] ; then
	r=`expr $n % 10`
	d=`expr $n - $r`
	shift $d
else
	r=$n
fi

if [ -z "$n" ]; then
        echo $#
else
        eval echo \$$r
fi
}




#----------------------
# rustic ping with nc
#----------------------
# (result is $?)

rustic_ping()
{
ping -qnr -c 1 $1 > /tmp/ping.out 2>/dev/null
(cat /tmp/ping.out | rgrep "100% packet loss" >/dev/null) && return 1
return 0 
}


#------------------------
# mantain a DB in file:
#--------------------------
# Usage add_entry file= ... key=.... table="x,y,z"
# Note: all entries contains "key" are replaced with
# a single  "table" entry. Use table="" to remove the entry.
 
add_entry()
{
SplitArgs $@
[ $# -eq 0 ] && echo "Usage: add_entry file= key= table=',,,,' [top]"
tmp=/tmp/e$$
cat $file | sed -n "/$key/!P" > $tmp
if [ "$table" = "true" ] ; then
	cat $tmp > $file
	rm -f $tmp
	return
fi
if [ "$top" = true ]; then
	(echo "$( IFS=,; echo $table)"; cat $tmp ) > $file
else
	(cat $tmp; echo "$( IFS=,; echo $table)" ) > $file
fi
rm -f $tmp
}


device_supported()
{
cat /proc/devices | rgrep "$1" > /dev/null
return $?
}


#---------------------------------
# get assigned IP to interface $1
#----------------------------------

get_ip()
{
local ip
interface=$1
[ -z "$1" ] && return
set -- `ifconfig $interface 2>/dev/null| rgrep "inet addr" | tr ':' ' '`
[ "$3" ] && [ "$3" != "0.0.0.0" ] && echo $3
}

get_mask()
{
local ip
interface=$1
[ -z "$1" ] && return
set -- `ifconfig $interface 2>/dev/null| rgrep "inet addr" | tr ':' ' '`
[ "$7" ] && echo $7
}

# remount UMSDOS as VFAT

remount_if_umsdos()
{
dev=$1
set -- `mount | rgrep $dev`
mp=$3; type=$5

if [ "$type" = umsdos ] ; then
        umount $dev
        echo -n "Remount $dev on $mp as VFAT ... "
        mount $dev $mp -t vfat && echo "done."
fi

}

extract_number()
{
echo $1 | sed "s/[^0-9]//g"
}

partition_number()
{
n=$(extract_number $1)
if [ "$n" ] ; then
	echo "(partition $n)"
fi
}

speak_dos()
{
case $1 in
*null*)    	echo NOTHING;;
*ttyS0*)        echo COM1;;
*ttyS1*)        echo COM2;;
*ttyS3*)        echo COM3;;
*ttyS4*)        echo COM4;;
*psaux*)        echo AUX;;
*/dev/hda*)
		echo "Primary Master $(partition_number $1)"
	;;
*/dev/hdb*)
		echo "Primary Slave $(partition_number $1)"
	;;
*/dev/hdc*)
		echo "Secondary Master $(partition_number $1)"
	;;
*/dev/hdd*)
		echo "Secondary Slave $(partition_number $1)"
	;;
*/dev/hd*)
		echo "IDE Hard-Disk $(partition_number $1)"
	;;	
esac
}

# strip out tty colors

strip_colors()
{
tr '\033' '%' | sed "s/%\[[^m]*m//g"
}

# get total RAM

ram_total()
{
set -- `cat /proc/meminfo`
echo $8
}

# include in the environment the 
# resource's parameters

include()
{
resource=$1
if [ -f /setup/cnf/$resource.cnf ] ; then
	. /setup/cnf/$resource.cnf
else
	. /setup/fun/$resource.fun ; default
fi
}
