#########################################################
#
# Based on the original work of Ted Naleid/Fernando Takai
# for auto completing Grails commands.
#
# Griffon version provided by Ken Sipe
# Further tweaks by Andres Almiray < blame
#########################################################

#export GRIFFON_VERSION="$(ls -lhr $HOME/.griffon | egrep -i '1\.' | head -1 | gawk '{print $9 }')"
export GRIFFON_VERSION="$(ls -r $HOME/.griffon | egrep -i '0\.' | head -1 | gawk '{print $1}')"

sed_option="-E"

[ "$(uname)" == "Linux" ] && sed_option="-r"

_get_tests(){
    find ./test -iname *.groovy 2> /dev/null | sed 's/\.\/test\/integration\///g' | sed 's/\Tests.groovy//g' | tr '/' \.
}

_get_plugins(){
#    cat $HOME/.griffon/$GRIFFON_VERSION/plugins/plugins-list.xml 2> /dev/null | grep \<plugin | gawk -F"name=" '{print $2}' | sed 's/\"//g' | sed 's/\/\{0,1\}\>//g'
    cat $HOME/.griffon/$GRIFFON_VERSION/plugins-list-default.xml 2> /dev/null | grep \<plugin | gawk -F"name=" '{print $2}' | sed 's/\"//g' | sed 's/>//g'
}

_get_scripts(){
    for D in $SCRIPT_DIRS; do
        if [ -d $D ]
# original regexp chain
#            then ls -1 $D/*.groovy 2> /dev/null | sed $sed_option 's/(.*)\/(.*)\.groovy/\2/' | sed $sed_option 's/([A-Z])/-\1/g' | sed $sed_option 's/^-//' | tr "[:upper:]" "[:lower:]"
            then ls -1 $D/*.groovy 2> /dev/null | sed $sed_option 's/(.*)\/(.*)\.groovy/\2/' | sed $sed_option 's/([ABCDEFGHIJKLMNOPQRSTUVWXYZ])/-\1/g' | sed $sed_option 's/^-//' | tr "[:upper:]" "[:lower:]"
        fi
    done | sort | uniq | grep -vE "^_" | grep -vE "_$"
}

_griffon_comp(){
    local cur prev opts base
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    PWD=`pwd`
    APP_NAME=`basename $PWD`
    APP_HOME="$HOME/.griffon/$GRIFFON_VERSION/projects/$APP_NAME"
    GLOBAL_PLUGINS="$HOME/.griffon/$GRIFFON_VERSION/plugins/"
    if [ -r ./griffon-app ]; then
        SCRIPT_DIRS="$GRIFFON_HOME/scripts ./scripts $HOME/.griffon/scripts"
        if [ -d $GLOBAL_PLUGINS ]
            then for PLUGIN_DIR in $(ls -d $GLOBAL_PLUGINS/*/scripts 2> /dev/null); do
            SCRIPT_DIRS="$SCRIPT_DIRS $PLUGIN_DIR"
            done
        fi
        if [ -d $APP_HOME/plugins ]
            then for PLUGIN_DIR in $(ls -d $APP_HOME/plugins/*/scripts 2> /dev/null); do
            SCRIPT_DIRS="$SCRIPT_DIRS $PLUGIN_DIR"
            done
        fi
 
        opts=$(_get_scripts)
    
        case "${prev}" in
            test-app)
                local test_classes=$(_get_tests)
                COMPREPLY=( $(compgen -W "${test_classes}" -- ${cur}) )
                return 0
                ;;
            install-plugin)
                local plugins=$(_get_plugins)
                COMPREPLY=( $(compgen -W "${plugins}" -- ${cur}) )
                return 0
                ;;
            package-plugin)
                COMPREPLY=( $(compgen -f) )
                return 0
                ;;
            plugin-info)
                local plugins=$(opts)
                COMPREPLY=( $(compgen -W "${plugins}" -- ${cur}) )
                return 0
                ;;
            help)
                local opts=$(_get_scripts)
                COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
                return 0
                ;;
            *)
            ;;
        esac
    
        if [[ "${opts}" =~ "${prev}" ]]; then
            COMPREPLY=( $(compgen -f) )
            return 0
        fi
    
        COMPREPLY=($(compgen -W "${opts}" -- ${cur})) 
        return 0
    else
        opts="create-app create-plugin help list-plugins package-plugin plugin-info set-proxy"
        case "${prev}" in
            create-app)
                COMPREPLY=( $(compgen -f) )
                return 0
                ;;
            create-plugin)
                COMPREPLY=( $(compgen -f) )
                return 0
                ;;
            package-plugin)
                COMPREPLY=( $(compgen -f) )
                return 0
                ;;
            plugin-info)
                local plugins=$(_get_plugins)
                COMPREPLY=( $(compgen -W "${plugins}" -- ${cur}) )
                return 0
                ;;
            *)
                ;;
        esac
        COMPREPLY=($(compgen -W "${opts}" -- ${cur})) 
        return 0
    fi
}
 
complete -F _griffon_comp griffon
