# Copyright (c) 2019-2020 Sebastian Gniazdowski
# License MIT

emulate -LR zsh
setopt extendedglob noshortloops nullglob typesetsilent warncreateglobal
if [[ "$1" = plugin ]]; then
    local type="$1" user="$2" plugin="$3" id_as="$4" dir="${5#%}" hook="$6"
else
    local type="$1" url="$2" id_as="$3" dir="${4#%}" hook="$5"
fi

#
# sbin'' ice – creation of shims (the name borrowed from rbenv)
#

if (( ${+ICE[sbin]} )); then
    local -a sbins=() srcdst=()
    # sbins+=(${(s.;.)ICE2[sbin]})
    sbins+=(${(s.;.)ICE[sbin]})
    (( !$#sbins )) && sbins+=("")
    local sbin
    (
        builtin cd -q "$dir" || return
        for sbin in $sbins; do
            integer set_gem_home=0 set_node_path=0 set_virtualenv=0 set_cwd=0 use_all_null=0 use_err_null=0 use_out_null=0 use_1=0
            if [[ -z $sbin ]]; then
                if [[ -f $dir/${id_as:t} ]]; then
                    sbin="$dir/${id_as:t}"
                elif [[ -n $plugin && -f $dir/$plugin ]]; then
                    sbin="$dir/$plugin"
                elif [[ -n $url && -f $dir/${url:t} ]]; then
                    sbin="$dir/${url:t}"
                elif [[ -f $dir/**/($id_as:t|$plugin)(#qN[1].) ]]; then
                    sbin=($dir/**/($id_as:t|$plugin)(Nnon.))
                    sbin=$sbin[1]
                else
                    local -a files
                    files=($dir/**/*(*Nnon:t))
                    if (( ${#files} )); then
                        sbin="${files[1]}"
                    else
                        +zi-log "{w} {b}bin-gem-node{rst}: The automatic-empty sbin ice didn't find any executable files"
                        break
                    fi
                fi
            fi
            srcdst=(${(@s.=>.)${(@s.→.)${(@s.->.)sbin}}})
            srcdst=("${srcdst[@]//((#s)[[:space:]]##|[[:space:]]##(#e))/}")
            [[ ${srcdst[1]} = [1gnpcNEO]#g[1gnpcNEO]#:* ]] && set_gem_home=1
            [[ ${srcdst[1]} = [1gnpcNEO]#n[1gnpcNEO]#:* ]] && set_node_path=1
            [[ ${srcdst[1]} = [1gnpcNEO]#p[1gnpcNEO]#:* ]] && set_virtualenv=1
            [[ ${srcdst[1]} = [1gnpcNEO]#c[1gnpcNEO]#:* ]] && set_cwd=1
            [[ ${srcdst[1]} = [1gnpcNEO]#N[1gnpcNEO]#:* ]] && use_all_null=1
            [[ ${srcdst[1]} = [1gnpcNEO]#E[1gnpcNEO]#:* ]] && use_err_null=1
            [[ ${srcdst[1]} = [1gnpcNEO]#O[1gnpcNEO]#:* ]] && use_out_null=1
            [[ ${srcdst[1]} = [1gnpcNEO]#1[1gnpcNEO]#:* ]] && use_1=1
            srcdst[1]=${srcdst[1]#[a-zA-Z0-9]#:}
            @zinit-substitute 'srcdst[1]' 'srcdst[2]'
            local -a fnames=()
            local fname
            if [[ $srcdst[1] != /* ]]; then
                eval "fnames=( **/${srcdst[1]}(Nnon-.) )"
            else
                eval "fnames=( ${srcdst[1]}(Nnon-.) )"
            fi
            if (( !${#fnames} )); then
                +zi-log "{e} {b}bin-gem-node{rst}: The {ice}sbin{rst} ice ({glob}$sbin{rst}) did not match any files"
                continue
            fi
            for fname in $fnames; do
                srcdst[1]="$fname"
                local target_binary="${${(M)srcdst[1]:#/*}:-$dir/${srcdst[1]}}" fnam="${srcdst[2]:-${srcdst[1]:t}}"
                local file="$ZPFX/bin/$fnam"
                command rm -f "$file"
                if [[ ! -e $file ]]; then
                    (( !OPTS[opt_-q,--quiet] )) && +zi-log "{m} {b}sbin{rst}: Deleted {file}$fnam{rst} shim"
                else
                    +zi-log "{w} {b}sbin{rst}: {file}$fnam{rst} shim not found in \$ZPFX/bin"
                fi
            done
        done
    )
fi

# vim:ft=zsh:sw=4:sts=4:et
