#!/bin/bash

# Source Mage GNU/Linux config vars are exported to current environment
[ -f /etc/sorcery/config ] &&
eval `. /etc/sorcery/config; 
      echo "export GRIMOIRE_LIST=$GRIMOIRE_LIST";
      echo "export SPELL_STATUS=$SPELL_STATUS"`

# Source Mage GNU/Linux utility functions used by actual completion functions
# by Lunar_fr (lunar@l-a-p.org)
#
_spell_list()
{
    for GRIMOIRE in `. $GRIMOIRE_LIST; echo ${GRIMOIRE_DIR[*]}`; do
	COMPREPLY=( ${COMPREPLY[@]} $( cut -d' ' -f1 "$GRIMOIRE/codex.index" | sed -ne 's/^\('$cur'.*\)/\1/p' ) )
    done
}

_dashify()
{
    local i
    for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
	if [ ${#COMPREPLY[i]} -le 2 ]; then
	    COMPREPLY[i]=-${COMPREPLY[i]}
	else
	    COMPREPLY[i]=--${COMPREPLY[i]}
	fi
    done
}

_add_installed_spell_list()
{
    COMPREPLY=( ${COMPREPLY[@]}
	$( sed -ne 's/^\('$cur'.*\):[0-9]*:installed:[0-9a-z.\-]*$/\1/p' $SPELL_STATUS ) )
}

_add_held_spell_list()
{
   COMPREPLY=( ${COMPREPLY[@]}
   $( sed -ne 's/^\('$cur'.*\):[0-9]*:held:[0-9a-z.\-]*$/\1/p' $SPELL_STATUS ) )
}

_add_exiled_spell_list()
{
   COMPREPLY=( ${COMPREPLY[@]}
   $( sed -ne 's/^\('$cur'.*\):[0-9]*:exiled:[0-9a-z.\-]*$/\1/p' $SPELL_STATUS ) )
}

_add_section_list()
{
    for GRIMOIRE in `. $GRIMOIRE_LIST; echo ${GRIMOIRE_DIR[*]}`; do
	COMPREPLY=( ${COMPREPLY[@]} $( ls $GRIMOIRE | \
	    sed -ne 's/^\('$cur'.*\)/\1/p' ) )
    done
}

# grimoire functions by Arjan Bouter (abouter@sourcemage.org)
_add_grimoire_list()
{
    for GRIMOIRE in `. $GRIMOIRE_LIST; echo ${GRIMOIRE_DIR[*]}`; do
	COMPREPLY=( ${COMPREPLY[@]} $( basename $GRIMOIRE | sed -ne 's/^\('$cur'.*\)/\1/p' ) )
    done
}

# Source Mage GNU/Linux gaze(1) completion
# by Lunar_fr (lunar@l-a-p.org)
# updated by Bor Kraljic (pyrobor@ver.si)
#
have gaze &&
_gaze()
{
    local cur prev
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    
    if [ $COMP_CWORD = 1 ]; then
	COMPREPLY=( $( compgen -W 'alien from search service provides what \
           where website install installed compile sources history sum \
           md5sum export import section maintainer grimoire html newer \
           older prune voyeur pam orphans depends spider time version versions \
			activity install-queue remove-queue show-held show-exiled short \
			source_urls install-full install-spell license size grimoires dependencies \
			time time-system system-info tablet\
			BUILD CONFIGURE CONFLICTS DETAILS DEPENDS DOWNLOAD FINAL HISTORY \
			INSTALL INSTALL_EXTRAS PATCH POST_BUILD POST_INSTALL POST_REMOVE \
			POST_RESURRECT PRE_BUILD PRE_INSTALL PRE_REMOVE PRE_RESURRECT \
			PRE_SUB_DEPENDS PREPARE PROVIDES SECURITY SUB_DEPENDS TRANSFER \
			TRIGGER_CHECK TRIGGERS UP_TRIGGERS' $cur ))
    elif [ $COMP_CWORD = 2 ]; then
	case "$prev" in
		@(what|where|website|sources|maintainer|depends|dependencies|history|version|versions|short|url|source_urls))
                _spell_list
		return 0
		;;
		@(compile|install|sum|md5sum|install-full|install-spell|size|voyeur|checkmd5s))
	        _spell_list
		return 0
		;;
	    installed|tablet)
	        _add_installed_spell_list
		return 0
		;;
		time|time-system)
        COMPREPLY=( $( compgen -W 'last median mean weigh-last full' ${cur#--} ))
        _dashify
        return 0
		;;
		section)
			_add_section_list
		return 0
		;;
		grimoire|html)
			_add_grimoire_list
		return 0
		;;
		@(BUILD|CONFIGURE|CONFLICTS|DETAILS|DEPENDS|DOWNLOAD|FINAL|HISTORY|INSTALL|INSTALL_EXTRAS|PATCH|POST_BUILD|POST_INSTALL|POST_REMOVE|POST_RESURRECT|PRE_BUILD|PRE_INSTALL|PRE_REMOVE|PRE_RESURRECT|PRE_SUB_DEPENDS|PREPARE|PROVIDES|SECURITY|SUB_DEPENDS|TRANSFER|TRIGGER_CHECK|TRIGGERS|UP_TRIGGERS))
		_spell_list
		return 0
		;;
	    import)
	        COMPREPLY=( $( compgen -d $cur ) )
		return 0
	        ;;
		-q)
			_add_grimoire_list
			return 0
		;;
	    *)
		return 0
		;;
	esac
	elif [ $COMP_CWORD = 3 ]; then
		preprev=${COMP_WORDS[COMP_CWORD-2]}
		case "$prev" in
		--last|--median|--mean|--weigh-last|--full)
 			if [ $preprev = time ]; then
				_add_installed_spell_list
 			fi
			return 0
		;;
		esac
	fi
	return 0
}
[ "$have" ] && complete -F _gaze -o default gaze


# Source Mage GNU/Linux sorcery(8) completion
# by Bor Kraljic (pyrobor@ver.si)
#
have sorcery &&
_sorcery()
{
	local cur prev

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}
	if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
		--*)
			COMPREPLY=( $( compgen -W 'version help' ${cur#--} ) )
			_dashify
			return 0
		;;
		-*)
			COMPREPLY=( $( compgen -W 'v h u s g q r rr' ${cur#-} ) )
			_dashify
		return 0
		;;
	esac
    fi
	case "$prev" in
		hold|exile|add-queue|remove-queue)
			_spell_list
			return 0
		;;
      unhold)
        _add_held_spell_list
        return 0
        ;;
      unexile)
        _add_exiled_spell_list
        return 0
        ;;
		default)
			COMPREPLY=( $( compgen -W 'add remove search' $cur ) )
			return 0
		;;
       *)
      COMPREPLY=( $( compgen -W 'update system-update upgrade queue queue-security \
                  rebuild rebuild-reconfigure hold unhold exile unexile add-queue \
                  remove-queue default' $cur ))
      return 0
      ;;
	esac
	
	return 0
}
[ "$have" ] && complete -F _sorcery -o default sorcery

# Source Mage GNU/Linux cast(8) completion
# by Lunar_fr (lunar@l-a-p.org)
#
have cast &&
_cast()
{        
    local cur prev
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
	    --*)
		COMPREPLY=( $( compgen -W 'quiet dot fix nofix download deps compile reconfigure \
								from url pam queue cflags cxxflags ldflags cppflags no-opts \
								grimoire from recast-down recast-up recast-optional lazy-updates \
								force-base-dep help\ ' ${cur#--} ) )
		_dashify
		return 0
		;;
	    -*)
		COMPREPLY=( $( compgen -W 'q t f n d s c r' ${cur#-} ) )
		_dashify
		return 0
		;;
	esac
    fi
    case "$prev" in
	--from)
	    COMPREPLY=( $( compgen -d $cur ) )
	    return 0
	    ;;
	-O|--recast-optional|--lazy-updates|-Z)
		COMPREPLY=( $( compgen -W 'ignore ask-yes ask-no always' $cur ) )
		return 0
		;;
	*)
	    _spell_list
	    return 0
	    ;;
    esac
    return 0
}
[ "$have" ] && complete -F _cast -o default cast

# Source Mage GNU/Linux summon(8) completion
# by Mads Laursen (dossen+sgl@daimi.au.dk)
#
have summon &&
_summon()
{
    local cur prev
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    
    if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
	    --*)
		COMPREPLY=( $( compgen -W 'from url download queue all print raw' ${cur#--} ) )
		_dashify
		return 0
		;;
	    -*)
		COMPREPLY=( $( compgen -W 'd p g r' ${cur#-} ) )
		_dashify
		return 0
		;;
	esac
    fi
    case "$prev" in
	--from)
	    COMPREPLY=( $( compgen -d $cur ) )
	    return 0
	    ;;
	--print|-p)
      COMPREPLY=( $( compgen -d 'raw source one_url all_urls' $cur ) )
      return 0
      ;;
	*)
	    _spell_list
	    return 0
	    ;;
    esac

    return 0
}
[ "$have" ] && complete -F _summon -o default summon

# Source Mage GNU/Linux dispel(8) completion
# by Mads Laursen (dossen+sgl@daimi.au.dk)
#
have dispel &&
_dispel()
{
    local cur prev
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
	    --*)
		COMPREPLY=( $( compgen -W 'exile downgrade nosustain notriggers user-deps \
					user-child-deps user-parent-deps recast-parent non-orphan orphan \
					total-dispel no-reap-depends noreap noqueue child dispel-parent help' ${cur#--} ) )
		_dashify
		return 0
		;;
	    -*)
		COMPREPLY=( $( compgen -W 'e d h' ${cur#-} ) )
		_dashify
		return 0
		;;
	esac
    fi
	case "$prev" in
	--recast-parent|--non-orphan|--orphan|--dispel-parent)
		COMPREPLY=( $( compgen -W 'ignore ask-yes ask-no always' $cur ) )
		return 0 ;;
	*)
    _add_installed_spell_list
		return 0 ;;
	esac
    return 0
}
[ "$have" ] && complete -F _dispel -o default dispel

# Source Mage GNU/Linux confmeld(8) completion
# by Bor Kraljic (pyrobor@ver.si)
#

have confmeld &&
_confmeld()
{
	local cur prev

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

	if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
		--*)
		COMPREPLY=( $( compgen -W 'help list list-configs list-spells no-column force' ${cur#--} ) )
		_dashify
		return 0
		;;
		-*)
		COMPREPLY=( $( compgen -W 'h l c s q f' ${cur#-} ) )
		_dashify
		return 0
		;;

	esac
	fi
	case "$prev" in
	-f|--force)
		COMPREPLY=( $( compgen -W 'discard-new discard-old use-old use-new' $cur ) )
		_dashify
		return 0
		;;
	*)
		_spell_list
		return 0
		;;
	esac

	return 0
}
[ "$have" ] && complete -F _confmeld -o default confmeld

# Source Mage GNU/Linux cleanse(8) completion
# by Bor Kraljic (pyrobor@ver.si)
#
have cleanse &&
_cleanse()
{
	local cur prev

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

	if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
		--*)
			COMPREPLY=( $( compgen -W 'delint prune packages fix nofix nofix_quick \
						tablet tablet_spell logs sweep sweep_all help' ${cur#--} ) )
			_dashify
			return 0
		;;
		-*)
			COMPREPLY=( $( compgen -W 'h' ${cur#-} ) )
			_dashify
			return 0
		;;
	esac
	fi
	case "$prev" in
	--delint)
		COMPREPLY=( $( compgen -W 'full' ${cur} ) )
		return 0 ;;
	--prune)
		COMPREPLY=( $( compgen -W 'doit' ${cur} ) )
		return 0 ;;
	--tablet)
		COMPREPLY=( $( compgen -W 'coalesce' ${cur} ) )
		return 0 ;;
    --fix|--nofix|--nofix_quick|--tablet_spell|*)
      _add_installed_spell_list
      return 0 ;;
	esac
	return 0
}
[ "$have" ] && complete -F _cleanse -o default cleanse

# Source Mage GNU/Linux resurrect(8) completion
# by Bor Kraljic (pyrobor@ver.si)
#
have resurrect &&
_resurrect()
{
	local cur prev

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

	if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
		--*)
		COMPREPLY=( $( compgen -W 'version latest from check voyeur nosustain help' ${cur#--} ) )
		_dashify
		return 0
		;;
		-*)
		COMPREPLY=( $( compgen -W 'v l f c V h' ${cur#-} ) )
		_dashify
		return 0
		;;
	esac
	fi
	case "$prev" in
	--from)
		COMPREPLY=( $( compgen -d $cur ) )
		return 0
		;;
	*)
		_spell_list
		return 0
		;;
	esac

	return 0
}
[ "$have" ] && complete -F _resurrect -o default resurrect

# Source Mage GNU/Linux scribe(8) completion
# by Arjan Bouter (abouter@sourcemage.org)
#
have scribe &&
_scribe()
{
    local cur prev
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    first=${COMP_WORDS[1]}
    second=${COMP_WORDS[2]}

    if [ $COMP_CWORD = 1 ]; then
	COMPREPLY=( $( compgen -W 'update add remove fix index reindex set \
		swap localize unlocalize reindex-keyword reindex-version' $cur ))
    elif [ $COMP_CWORD = 2 ]; then
	case "$prev" in
		@(update|remove|fix|reindex|reindex-keyword|reindex-version|set|swap|localize|unlocalize))
                _add_grimoire_list
		return 0
		;;
	    add)
		COMPREPLY=( $( compgen -W 'stable games z-rejected test binary' $cur ) )
		return 0
		;;
	    *)
		return 0
		;;
	esac
    elif [ $COMP_CWORD = 3 ]; then
	case "$first" in
	    @(swap|set))
                _add_grimoire_list
		return 0
		;;
	    add|update)
		COMPREPLY=( $( compgen -W 'from' $cur ) )
		return 0
		;;
	    *)
		return 0
		;;
	esac
    elif [ $COMP_CWORD = 4 ]; then
	case "$prev" in
	    from)
		case "$second" in
		stable|test|z-rejected|games|binary)
			COMPREPLY=( $( compgen -W 'http://codex.sourcemage.org/$second.tar.bz2 rsync://sourcemage.org::$second' $cur))
			return 0
			;;
		  *)	COMPREPLY=( $( compgen -W 'http:// rsync://' $cur))
			return 0
			;;
		esac
		;;
	    *)
		return 0
		;;
	esac
    fi
    return 0
}
[ "$have" ] && complete -F _scribe -o default scribe

# Source Mage GNU/Linux scribbler(8) completion
# by Bor Kraljic (pyrobor@ver.si)
#
have scribbler &&
_scribbler()
{
	local cur prev

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

	if [ $COMP_CWORD = 1 ]; then
		COMPREPLY=( $( compgen -W 'add remove' $cur ))
		return 0
	elif [ $COMP_CWORD = 2 ]; then
		if [ "$prev" = add ] || [ "$prev" = remove ]; then
			_spell_list
			return 0
		else
			_add_grimoire_list
			return 0
		fi
	elif [ $COMP_CWORD = 3 ]; then
		_add_grimoire_list
		return 0
	elif  [ $COMP_CWORD = 4 ]; then
		_add_grimoire_list
		return 0
	fi

	return 0
}
[ "$have" ] && complete -F _scribbler -o default scribbler

# Source Mage GNU/Linux quill(8) completion
# by p3pilot
#
have quill &&
_quill()
{        
    local cur prev
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    if [ $COMP_CWORD -ge 1 -o ${prev} != ${prev#-} -o ${prev} != ${prev#--} ]; then
	case "$cur" in
	    --*)
		COMPREPLY=( $( compgen -W 'update apprentice mage wizard help version reconfigure purge fmxml perlcpan rubyratt savane savannah gna fsf hackagedb' ${cur#--} ) )
		_dashify
		return 0
		;;
	    -*)
		COMPREPLY=( $( compgen -W 'u a m w h v r p f c y s n g F H' ${cur#-} ) )
		_dashify
		return 0
		;;
	esac
    fi
    case "$prev" in
	--from)
	    COMPREPLY=( $( compgen -d $cur ) )
	    return 0
	    ;;
	*)
	    _spell_list
	    return 0
	    ;;
    esac
    
    return 0
}
[ "$have" ] && complete -F _quill -o default quill

