# bash_completion for molecuilder 
#
# gives a list of all available actions, gives list of options for the last action

function _molecuilder_option()
{
	local cmd="${1##*/}"
	local word=${COMP_WORDS[COMP_CWORD]}
	local line=${COMP_LINE}
	option=`echo $line | awk -F"--" {'print $NF'} | tr -d \ `
	previousoption=`echo $line | awk -F"--" {'print $(NF-1)'} | tr -d \ `
	#echo "current option is '$word', line is '$line', and cmd is '$cmd', last option is '$option', and last but one is '$previousoption'."
	COMPREPLY=()
	# if current option is molecuilder, give all available actions
	if test x"$option" = x"molecuilder"; then
		# "molecuilder "
		COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
	else
		# if current argument is void, look at previous (if its not molecuilder)
		if test x"$option" = x""; then
			if test x"$previousoption" = x"molecuilder"; then
				# "molecuilder --"
				COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
			else
				# "molecuilder --some-action --"
				COMPREPLY=($($cmd --help | grep "^'$previousoption.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
				if  test ${#COMPREPLY[*]} -eq 0; then
					# "molecuilder --some-option --"
					COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
				else
					COMPREPLY=($($cmd --help --actionname "$previousoption" | grep Option\  | awk -F"'" {'print "--"$2'} | tr -d \'))
					# if action has no options, give list of actions
					if  test ${#COMPREPLY[*]} -eq 0; then
						COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
					fi
				fi
			fi
		elif test -z `echo "$word" 2>/dev/null | grep "\-\-"`; then
			# "molecuilder somefile/value"
			COMPREPLY=( $( compgen -W "$( ls --color=n -1 ${word}*.in ${word}*.pdb ${word}*.pcp ${word}*.psi ${word}*.data ${word}*.xyz 2>/dev/null| sed -e 's/ /\\ /g' )" -- $cur ))
		else
			# "molecuilder --something"
			COMPREPLY=($($cmd --help | grep "^'$option.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
			# check whether its an action
			if test ${#COMPREPLY[*]} -eq 1; then
				# check whether it's same as option
				if test x"${COMPREPLY[0]}" = x"--$option"; then
					# "molecuilder --an-action"
					#echo "Giving help for option $option"
					COMPREPLY=($($cmd --help --actionname "$option" | grep Option\  | awk -F"'" {'print "--"$2'} | tr -d \'))
					# if action has no options, give list of actions
					if  test ${#COMPREPLY[*]} -eq 0; then
						COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
					fi
				fi
			fi
			# if the "action" is unknown, it must be actually an option (segment), give all available options from previous action
			if test ${#COMPREPLY[*]} -eq 0; then
				# "molecuilder --an-action --opt.."
				COMPREPLY=($($cmd --help --actionname "$previousoption" | grep Option\ | grep $option | awk -F"'" {'print "--"$2'} | tr -d \'))
				# if action has no options, give list of actions
				if  test ${#COMPREPLY[*]} -eq 0; then
					COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
				else
					#if we have a complete option, give again all available actions
					if test x"${COMPREPLY[0]}" = x"--$option"; then
						# "molecuilder --an-action --one-of-its-options"
						COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \'))
					fi
				fi
			fi
		fi
	fi
	#echo "returning ${COMPREPLY[*]}."
	return 0
}

complete -F _molecuilder_option molecuilder
