#  * Project: MediaTex
#  * Module:  configuration files
#  *
#  * This file is process by bash_completion.
# 
#  MediaTex is an Electronic Records Management System
#  Copyright (C) 2014 2015 2016 2017 Nicolas Roche
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

LONG_OPTION="\
--help \
--version \
--facility \
--severity \
--log-file \
--dry-run \	
--memory-limit \
--script-out \
--debug-lexer \
--conf-label \
--debug-script \
--conf-label \
--alone"
SHORT_OPTION="-h -v -f -s -l -n -m -S -L -c -a"
FACILITY="file local2"
SEVERITY="err warning notice info debug"

_mediatexd()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
        
    case "$cur" in
	--)
	    COMPREPLY=( $( compgen -W "$LONG_OPTION" -- $cur ) )
	    return 0
	    ;;
	-)
	    COMPREPLY=( $( compgen -W "$SHORT_OPTION" -- $cur ) )
	    return 0
	    ;;
    esac
}

_mediatex()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}
        
    case "$cur" in
	--)
	    COMPREPLY=( $( compgen -W "$LONG_OPTION" -- $cur ) )
	    return 0
	    ;;
	-)
	    COMPREPLY=( $( compgen -W "$SHORT_OPTION" -- $cur ) )
	    return 0
	    ;;
    esac
	    
    case "$prev" in
	-f)
	    COMPREPLY=( $( compgen -W "$FACILITY" -- $cur ) )
	    return 0
	    ;;
	-s)
	    COMPREPLY=( $( compgen -W "$SEVERITY" -- $cur ) )
	    return 0
	    ;;
	-l)
	    COMPREPLY=( $( compgen -W "file" -- $cur ) )
	    return 0
	    ;;
	-c)
	    return 0
	    ;;
	adm)
	    WORDS="init remove purge add del update commit \
bind unbind mount umount get"
	    COMPREPLY=( $( compgen -W "$WORDS" -- $cur ) )
	    return 0
	    ;;
	srv|serv|server)
	    WORDS="save extract notify deliver"
	    COMPREPLY=( $( compgen -W "$WORDS" -- $cur ) )
	    return 0
	    ;;
	note|check)
	    COMPREPLY=( $( compgen -W "supp" -- $cur ) )
	    return 0
	    ;;
	add|del)
	    case "${COMP_WORDS[COMP_CWORD-2]}" in
		adm)
		    COMPREPLY=( $( compgen -W "user coll" -- $cur ) )
		    return 0
		    ;;
		mediatex)
		    WORDS="supp file key natClt natSrv"
		    COMPREPLY=( $( compgen -W "$WORDS" -- $cur ) )
		    return 0
		    ;;
		esac
	    return 0
	    ;;
	user)
	    COMPREPLY=( $( compgen -u -- $cur ) )
	    return 0
	    ;;
	update|commit|upgrade|upgrade+|make|clean|audit)
	    COMPREPLY=( $( compgen -W "coll" -- $cur ) )
	    return 0
	    ;;
	upload|upload+|upload++)
	    COMPREPLY=( $( compgen -W "catalog rules file" -- $cur ) )
	    return 0
	    ;;
	mount)
	    _cd_devices
	    return 0
	    ;;
	umount|file|catalog|rules|key|on)
	    _filedir
	    return 0
	    ;;
	list)
	    COMPREPLY=( $( compgen -W "coll supp" -- $cur ) )
	    return 0
	    ;;
	to|from)
	    [ $COMP_CWORD -gt 2 ] || return 0;
	    case "${COMP_WORDS[COMP_CWORD-3]}" in
		supp) # add/del supp SUPP to/from
		    COMPREPLY=( $( compgen -W "coll ALL" -- $cur ) )
		    return 0
		    ;;
		upload|upload+|upload++|key|natClt|natSrv)
		    COMPREPLY=( $( compgen -W "coll" -- $cur ) )
		    return 0
		    ;;
	    esac
	    return 0
	    ;;
    esac

    if [ $COMP_CWORD -eq 1 ]; then
	WORDS="adm serv list new add del note check upload upload+ \
upload++ motd upgrade upgrade+ make clean su audit"
	COMPREPLY=( $( compgen -W "$WORDS" -- $cur ) )
	return 0;
    fi

    case "${COMP_WORDS[COMP_CWORD-2]}" in
	mount)
	    COMPREPLY=( $( compgen -W "on" -- $cur ) )
	    return 0
	    ;;
	get)
	    COMPREPLY=( $( compgen -W "as" -- $cur ) )
	    return 0
	    ;;
	on)
	    COMPREPLY=( $( compgen -W "to" -- $cur ) )
	    return 0
	    ;;
	file)
	    if [ $COMP_CWORD -gt 3 ]; then
		case "${COMP_WORDS[COMP_CWORD-4]}" in
		    catalog) # catalog FILE file FILE
			COMPREPLY=( $( compgen -W "file rules as" -- $cur ) )
			return 0
			;;
		    rules) # rules FILE file FILE
			COMPREPLY=( $( compgen -W "file catalog as" -- $cur ) )
			return 0
			;;
		    file|as) # file|as FILE file FILE
			COMPREPLY=( $( compgen -W "file catalog rule as" -- $cur ) )
			return 0
			;;
		esac
	    fi
	    COMPREPLY=( $( compgen -W "file catalog rules as" -- $cur ) )
	    return 0
	    ;;
	as)
	      if [ $COMP_CWORD -gt 3 ]; then
		  case "${COMP_WORDS[COMP_CWORD-4]}" in
		      get) # get PATH as
			  COMPREPLY=( $( compgen -W "on" -- $cur ) )
			  return 0
			  ;;
		  esac
	      fi
	    if [ $COMP_CWORD -gt 5 ]; then
		case "${COMP_WORDS[COMP_CWORD-6]}" in
		    catalog) # catalog FILE file FILE as
			COMPREPLY=( $( compgen -W "file rules" -- $cur ) )
			return 0
			;;
		    rules) # rules FILE file FILE as
			COMPREPLY=( $( compgen -W "file catalog" -- $cur ) )
			return 0
			;;
		    file|as) # file|as FILE file FILE as
			COMPREPLY=( $( compgen -W "file catalog rule" -- $cur ) )
			return 0
			;;
		esac
	    fi
	    COMPREPLY=( $( compgen -W "file catalog rules" -- $cur ) )
	    return 0
	    ;;
	catalog)
	    if [ $COMP_CWORD -gt 3 ]; then
		case "${COMP_WORDS[COMP_CWORD-4]}" in
		    rules) # rules FILE catalog FILE
			COMPREPLY=( $( compgen -W "file" -- $cur ) )
			return 0
			;;
		    file|as) # XXX FILE catalog
			COMPREPLY=( $( compgen -W "file rules" -- $cur ) )
			return 0
			;;
		esac
	    fi
	    COMPREPLY=( $( compgen -W "rules file" -- $cur ) )
	    return 0
	    ;;
	rules)
	    if [ $COMP_CWORD -gt 3 ]; then
		case "${COMP_WORDS[COMP_CWORD-4]}" in
		    catalog) # catalog FILE file
			COMPREPLY=( $( compgen -W "file" -- $cur ) )
			return 0
			;;
		    file|as) # XXX FILE rules
			COMPREPLY=( $( compgen -W "file catalog" -- $cur ) )
			return 0
			;;
		esac
	    fi
	    COMPREPLY=( $( compgen -W "catalog file" -- $cur ) )
	    return 0
	    ;;
	supp)
	    [ $COMP_CWORD -gt 2 ] || return 0;
	    case "${COMP_WORDS[COMP_CWORD-3]}" in
		check) # check supp SUPP
		    COMPREPLY=( $( compgen -W "on" -- $cur ) )
		    return 0
		    ;;
		add) # add supp SUPP
		    COMPREPLY=( $( compgen -W "on to" -- $cur ) )
		    return 0
		    ;;
		del) # del supp SUPP
		    COMPREPLY=( $( compgen -W "from" -- $cur ) )
		    return 0
		    ;;
		note) # note supp SUPP
		    COMPREPLY=( $( compgen -W "as" -- $cur ) )
		    return 0
		    ;;
	    esac
	    return 0
	    ;;
	natClt|natSrv)
	    [ $COMP_CWORD -gt 2 ] || return 0;
	    case "${COMP_WORDS[COMP_CWORD-3]}" in
		add) # add natXXX XXX
		    COMPREPLY=( $( compgen -W "to" -- $cur ) )
		    return 0
		    ;;
		del) # del natXXX XXX
		    COMPREPLY=( $( compgen -W "from" -- $cur ) )
		    return 0
		    ;;
	    esac
	    return 0
	    ;;
	coll)
	    [ $COMP_CWORD -gt 2 ] || return 0;
	    case "${COMP_WORDS[COMP_CWORD-3]}" in
		audit) # audit coll COLL
		    COMPREPLY=( $( compgen -W "for" -- $cur ) )
		    return 0
		    ;;
	    esac
	    return 0
    esac
        
    [ $COMP_CWORD -gt 3 ] || return 0;
    case "${COMP_WORDS[COMP_CWORD-4]}" in
	get) # get PATH as COLL 
	    COMPREPLY=( $( compgen -W "on" -- $cur ) )
	    return 0
	    ;;
    esac	   	  
}

complete -F _mediatexd mediatexd
complete -F _mediatex mediatex

# Local Variables:
# mode: shell-script
# mode: font-lock
# mode: auto-fill
# End:
