
_makenemo_get_compilers(){
    #${COMP_WORDS[0]} -m help|grep -v "Available compilers"|sed -e 's/\([^[:space:]]*\).*/\1/'
    ls -1R "$(dirname $(which ${COMP_WORDS[0]}))"/../ARCH/|grep ".fcm"|sed -e 's/arch-\(.*\)\.fcm/\1/'
}

_makenemo_get_ref_conf(){
    #${COMP_WORDS[0]} -n help|grep -v "Available configurations"|sed -e 's/\([^[:space:]]*\).*/\1/'
    cat "$(dirname $(which ${COMP_WORDS[0]}))"/cfg.txt |sed -e 's/\([^[:space:]]*\).*/\1/' 
}

_makenemo_get_keys(){
    #$1 - config
    #$2 - 'add' or 'del'
    #$2 - prefix
    #$3 - suffix
    
    local i answ lIFS cpp_file
    cpp_file="$(dirname $(which ${COMP_WORDS[0]}))"/$1
    if [[ "$2" == "del_key" ]];then
	#del keys
	cpp_file=$cpp_file/cpp_$1.fcm
    else
	#add keys
	cpp_file=$cpp_file/cpp_all_$1.fcm
    fi

#Version 1
#    answ=$(cat "$(dirname $(which ${COMP_WORDS[0]}))"/${config}/cpp_${config}.fcm |\
#	sed -e 's/^ *bld::tool::fppkeys *//g' -e 's/ *$//g' -e 's/ \+/\n/g');#"
#    echo  -e "$answ" |sed -e "s/^/$1/" -e "s/\$/$2/"

#Version 2
    lIFS=$IFS;IFS=' '
    answ=''
    for i in $(cat $cpp_file | sed -e 's/^ *bld::tool::fppkeys *//g' );do #"
	#remove those already written to the command line
	if [ "$(echo $1 | grep -c $i )" -eq 0 ];then
	    answ="$answ\n$i"
	fi
    done
    echo  -e "$answ" |tail -n"+2" |sort|sed -e "s/^/$3/" -e "s/\$/$4/"
    IFS=$lIFS
}

_makenemo_get_nemo_subdirs(){
#Version 1    
    # ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/ |sed -e "s/^/$1/" -e "s/\$/$2/"

#Version 2
    # #find the uniq dirs 
    # #get all dirs
    # ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/ >/tmp/f.$$.1
    # #get choosed dirs
    # echo -e "$(echo "$1"| sed -e 's/"//' -e 's/ /\n/g' )" |sort >/tmp/f.$$.2
    # #strio the uniq
    # comm -23 /tmp/f.$$.1 /tmp/f.$$.2 |sed -e "s/^/$1/" -e "s/\$/$2/"

#Version 3
    
    local i answ
    answ=''
    for i in $(ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/);do #"

	if [ "$(echo $1 | grep -c $i )" -eq 0 ];then
	    answ="$answ\n$i"
	fi
    done
    echo -e "$answ" |tail -n"+2" |sort|sed -e "s/^/$1/" -e "s/\$/$2/" 
}

_makenemo_get_institutes(){
    ls -1p "$( dirname ${COMP_WORDS[0]})"/../ARCH/ |grep "/"|sed -e 's/\/$//'
}
_makenemo_add_space(){
    #We use 'complete -o nospace' because of add_key "bbla bla bla"
    #So for -v -j -m -r we do the following:
    #if only one result for completion - add a space to the end                                                                                                        
    #NOTE this function will delete(put space over it) the current text if $cur is not in $res=$1
    #in other words this function will delete the written text if it is not in the recodnised list of coplete words/options
    if [ $(echo "$1"|wc -l) -eq 1 ];then
	COMPREPLY=( $1\  )
    else                                                                                                                                                 
        COMPREPLY=( $1 )                                                                                                                               
    fi                   
}

_makenemo_opts_func () 
{

local prev res saveIFS
local i res  x_n x_r x_m

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


#First check whether do we need completion or some 'help' option is given
#Also fill the expected x_n x_r for proper confing name for the complition of key_cpp-s
for i in n r m;do
    res=$(echo "$COMP_LINE"|sed -e 's/.* -'"$i"' \+\([^-][^[:space:]]*\).*/\1/g'); #'
    if [[ $res != $COMP_LINE ]]; then 
	eval "x_$i=\"$res\""
    else
	eval "x_$i=''"
    fi
done

if [[ $(echo $COMP_LINE|grep ' -h'|wc -l) -eq 1 ]];then
    #-h  is given
    if [[ "$cur" == '-h' ]];then
	COMPREPLY=( '-h ')
    elif [[ "$prev" == '-h' ]];then
	COMPREPLY=($(compgen -W "$(_makenemo_get_institutes) all"  -- ${cur} )) ;#"
    else
	#Do not coplete if not after -h...
	COMPREPLY=()
    fi
    return 0;
elif [[ "$x_n" == 'help' ]];then
    COMPREPLY=()
    return 0;
elif [[ "$x_m" == 'help' ]];then
    COMPREPLY=()
    return 0;
fi

# Ok - we really have to complete so - find what

case "$prev" in
    -n) COMPREPLY=($(compgen -W "$(_makenemo_get_ref_conf) help"  -- ${cur} )) ; #"                                                                         
	#Do not use _makenemo_add_space(to add space after complition)
	#allow user to add suffixes and new confs by default
        return 0;; 
                
    -m) res=$(compgen -W "$( _makenemo_get_compilers ) help"  -- ${cur} ) ;#"
	_makenemo_add_space "$res"
	return 0;;

    -d) saveIFS=$IFS
        IFS=$'\n'
        local last_space_pos=$(expr match "$cur" '.* ')
        local preff=${cur:0:$last_space_pos}
    	
        ##res=$(compgen -W "$( _makenemo_get_nemo_subdirs "$preff")"  -- ${cur} ); #"
        ##previous line has problems with spaces so using:
        res=$(_makenemo_get_nemo_subdirs "${preff:-\"}" ' '|grep "${cur}"|sort -u)
    	COMPREPLY=( $res )
        IFS=$saveIFS
	return 0;; 
	
    -r) res=$(compgen -W "$( _makenemo_get_ref_conf )"  -- ${cur} ) ;#"
	_makenemo_add_space "$res"
	return 0;;

    -s) res=$(cd "$(dirname $(which ${COMP_WORDS[0]}))"/../; compgen -d $cur) ;#"
	COMPREPLY=( $res )
	return 0;;
    
    -j) #Use /proc/cpuinfo only when apropriate, if not - opts= '0 1'
	res=$( [ -f /proc/cpuinfo ] && seq  0 $(cat /proc/cpuinfo |grep processor|wc -l) || echo '0 1' )
	res=$(compgen -W "$res" -- ${cur} ) #" 
	_makenemo_add_space "$res"
        return 0;;                       

    -v)  res=$(compgen -W "$(echo -e '0\n1\n2\n3')"  -- ${cur} ) ;#"
	_makenemo_add_space "$res"
        return 0;;

    -t|-e) COMPREPLY=( $(compgen -d $cur ) );
	return 0;;

        
    del_key|add_key) saveIFS=$IFS
        IFS=$'\n'
        local last_space_pos=$(expr match "$cur" '.* ')
        local prefics=${cur:0:$last_space_pos}
    	
    	#choose config
    	#This script DOES NOT check whether x_n is really existing
    	#if x_r is given we assume it will be used, if not - we use x_n  
    	#otherwise use the defaut from cfg.txt
    	local config=${x_r:-$x_n}
    	config=${config:-$(_makenemo_get_ref_conf |tail -1)}
        ##res=$(compgen -W "$( _makenemo_get_nemo_subdirs "$preff")"  -- ${cur} ); #"
        ##previous line has problems with spaces so using:
        res=$(_makenemo_get_keys $config "$prev" "${prefics:-\"}" ' '|grep "${cur}"|sort -u)
    	COMPREPLY=( $res )
        IFS=$saveIFS
	return 0;; 

    *) #Defaults 
    res="-h -n -m -d -r -s -e -j -v -t add_key del_key clean clean_config"
    res=$(compgen -W "$res" -- ${cur} ) #" 
    _makenemo_add_space "$res" 
    return 0;;
esac

}
complete -o nospace -F _makenemo_opts_func makenemo 

