#-*- mode: shell-script;-*-

# hgl(1) completion
# Copyright 2013-2014 Heiko Schäfer <heiko@hgl.rangun.de>

have hgl &&
_hgl()
{
	local cur prev options ioplugins colors
  
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}
	options=$( hgl --help 2>&1 | gawk -F' ' '/^[[:space:]]+\-.*/ { print $1"\n"$2; }' | \
	  gawk -F'=' '{ print $1; }' | grep "^\-.*" | sed 's/,//' )
	ioplugins=$( hgl --list-imageoutput-plugins 2>&1 | gawk '/^[^;]+;/ { print $0; }' | cut -d';' -f1 )
	colors=$( hgl --list-color-names 2>&1 | gawk 'FNR == 2' | sed 's/,//g' )
  
	case $prev in
	--background|-b|--grid-color)
	  COMPREPLY=( $( compgen -W "$colors" | grep ^$cur ) )
	  ;;
	--output-format|-O)
	  COMPREPLY=( $( compgen -W "$ioplugins" | grep ^$cur ) )
	  ;;
	*)
	  COMPREPLY=( $(
		compgen -G "${cur}*.hglz"
		compgen -W "$options" | grep "^$cur"
	  ) )
	  ;;
	esac
  
	return 0
}

[ "$have" ] && complete -F _hgl -o filenames -o plusdirs hgl
