#   zeta_completion - some Zeta specific additions to 
#                     bash_completion
#
#   Copyright (C) 2004, Francois Revol <revol@free.fr>
#   Copyright (C) 2004, yellowtab GbmH
#

# that's only for BeOS and Zeta
[ -z "$UNAME" ] && UNAME="$(uname -s)"
[ $UNAME != BeOS ] && return

# force things
dirnames="-o dirnames"


# our ps command is rather crude, without options (yet),
# and doesn't follow the unix layout at all, plus we both
# have team and thread ids (pid == thread id).
# team = term for Unix process

# here are some placeholders

# list team ids
# ps | sed "/^[^\/k]/ d;s/.*(team //;s/).*//"
# list team names
# ps | sed "/^[^\/k]/ d;s/ .*//;s,.*/,,"
# list pids
# ps | awk "BEGIN {end = 0}; NR>4 && end<2 && /^[^\/]/ { print \$1 } /^\$/ {end++}"
# list interfaces
# ifconfig /dev/net/ne2k/0 | sed '/^[[:space:]]/ d;/^$/ d;s/[[:space:]].*//g'
# list mount points *broken*
# df -a | sed '2d;s/[[:space:]].*//'
# list raw devices:
# mountvolume -dd | sed '/^s/d;s/.*dev://;s/ .*//'
# list mountable volumes:
#



#
# some replacements for functions in bash_completion
#

# This function completes on process IDs
#
_pids()
{
	COMPREPLY=( $( compgen -W '$( command ps | awk "BEGIN {end = 0}; NR>4 && end<2 && /^[^\/]/ { print \$1 } /^\$/ {end++}" | sed 1d )' -- $cur ) )
}

# This function completes on process group IDs
#
_pgids()
{
    # unfortunately BeOS doesn't tell you pgids...
    _pids
}

# This function completes on configured network interfaces
#
_configured_interfaces()
{
	COMPREPLY=( $( sh -c 'source /etc/network.conf; for i in $IF_LIST; do eval echo \$IF_${i}_DEV; done' ) )
}

# This function completes on all available network interfaces
# -a: restrict to active interfaces only
# -w: restrict to wireless interfaces only
#
_available_interfaces()
{
#XXX: handle -a -w
	COMPREPLY=( $( command ifconfig \
		| sed '/^[[:space:]]/ d;/^$/ d;s/[[:space:]].*//g' ) )
}
#XXX:REMOVE
_interfaces()
{
	_available_interfaces
}

# XXX: override or not ?
# This function completes on user IDs
#
#_uids()
#{
#}

# This function completes on group IDs
#
#_gids()
#{
#}

#
# Additions to bash_completion own functions,
# for use in custom completions
#


# This function completes on team IDs
#
_teamids()
{
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command ps | sed "/^[^\/k]/ d;s/.*(team //;s/).*//" )' -- $cur ) )
}

# This function completes on team NAMEs
#
_teamnames()
{
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command ps | sed "/^[^\/k]/ d;s/ .*//;s,.*/,," )' -- $cur ) )
}

# This function completes on running app signatures
#
_appsigs()
{
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command roster | sed "1d;2d;s/.* (//;s/)\$//" )' -- $cur ) )
}

# This function completes both teamnames and app sigs
#
_teamnameandappsig()
{    
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command ps | sed "/^[^\/]/ d;s/ .*//;s,.*/,," )' -- $cur ) \
		    $( compgen -W '$( command roster | sed "1d;2d;s/.* (//;s/)\$//" )' -- $cur ) )
}

# This function completes on filesystem indices
#
_fsindices()
{
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command lsindex )' -- $cur ) )
}



#
# BeOS specific completions
#

# Zeta's ps accepts a team name to list only
# that team's threads.
complete -F _teamnames ps

# some teamid based commands
complete -F _teamids listarea listimage listport listsem

# quit sends an app the B_QUIT_REQUESTED message
complete -F _appsigs quit

# rmindex -- note completion breaks with colons
complete -F _fsindices rmindex

# WRITEME _iroster
# complete -F _iroster iroster

# This function completes on finddir tokens
# requires Haiku finddir for -l option
#
_finddir()
{
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command finddir -l )' -- $cur ) )
}
complete -F _finddir finddir

# This function completes on app_server decors
#
_setdecor()
{
	local cur

	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $( compgen -W '$( command setdecor -l )' -- $cur ) )
}
complete -F _setdecor setdecor

# This function completes on system sounds
# inspired from _rpm_groups
#
_systemsounds()
{
	local cur
	local IFS=$'\t'

	cur="${COMP_WORDS[COMP_CWORD]}"

	# remove trailing backslash, or grep will complain
	cur=${cur%"\\"}
	COMPREPLY=( $( installsound --list | sed "s/:.*//" | grep "^$cur" ) )
	# backslash escape spaces and translate newlines to tabs
	COMPREPLY=( $( echo ${COMPREPLY[@]} | sed 's/ /\\ /g' | tr '\n' '\t' ) )
}
complete -F _systemsounds beep


# This function completes on driver names
#
_rescan()
{
	local cur sysdrv usrdrv

	cur=${COMP_WORDS[COMP_CWORD]}
	sysdrv="$(ls /boot/beos/system/add-ons/kernel/drivers/bin/)"
	usrdrv="$(ls /boot/home/config/add-ons/kernel/drivers/bin/)"
	COMPREPLY=( $( compgen -W '$( command echo $sysdrv $usrdrv )' -- $cur ) )
}
complete -F _rescan rescan

# This function completes on email addresses form People files
#
_emailfrompeople()
{
	COMPREPLY=( $( compgen -W '$( command query -a "(META:email==\*@\*)"|xargs catattr META:email|sed "s/.*: string : //" )' -- $cur ) )
}


#complete -F _BeMail BeMail



# WRITEME _ifwconfig

# unmount completion.
# Note BeOS has uNmount, not umount.
#
_unmount()
{
	local cur

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}

	COMPREPLY=( $( compgen -W '$( df -a | sed "1d;2d;s/[[:space:]].*//" )' -- $cur ) )
	# FIXME: df is broken here...

	return 0
}
complete -F _unmount $dirnames unmount

#umount... we don't have that, so trash it
complete -r umount
unset _umount

# mount... BeOS/Zeta doesn't have any fstab we can use, we just disable that for now...
# mount [-ro] [-t fstype] device directory
_mount()
{       local cur prev sysfs usrfs opts mabledevs

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	if [ "$prev" = "-t" ]; then
		sysfs="$(ls /boot/beos/system/add-ons/kernel/file_systems/)"
		usrfs="$(ls /boot/home/config/add-ons/kernel/file_systems/)"
		COMPREPLY=( $( compgen -W '$( command echo $sysfs $usrfs )' -- $cur ) )
		return 0
	elif [ "${prev:0:1}" = "/" ]; then
		_filedir -d 
		return 0
	else
		opts="-ro -t"
		mabledevs="$(find /dev/disk/ -type c)"
#		if compgen -f -- "$cur" >/dev/null; then
#		#if [ -n "$cur" -a -e ./$cur* ]; then
#			COMPREPLY=( $( compgen -f -- $PWD/$cur ) )
#			return 0
#		fi
		COMPREPLY=( $( compgen -f -W '$( command echo $opts $mabledevs )' -- $cur ) )
		return 0
	fi
	_filedir -d
	return 0
}
complete -F _mount mount

# not sure why spaces in the names are failing even when they have escapes in them...
_Preferences()
{	
	COMPREPLY=( $( compgen -W '$(/bin/ls --quoting-style=escape /boot/home/config/ZetaMenu/Preferences/)' -- ${COMP_WORDS[COMP_CWORD]} ) )
}
complete -F _Preferences Preferences

# setdev allows switching dev kits (gcc versions)
_setdev()
{
	COMPREPLY=( $( compgen -W '$(setdev -L)' -- ${COMP_WORDS[COMP_CWORD]} ) )
}
complete -F _setdev setdev

# FIXME: enable _man
# FIXME: check _renice





###  Local Variables:
###  mode: shell-script
###  End:




