#!/usr/bin/env zsh
# vim: ft=zplug

# Prepare
tests=()
export ZPLUG_HOME="/tmp/zplug-$RANDOM"
export ZPLUG_REPOS="$ZPLUG_HOME/repos"
mkdir -p "$ZPLUG_HOME" "$ZPLUG_REPOS"

# Prepare
source "$ZPLUG_ROOT/init.zsh"
zplugs=()
zplug clear

# as
{
    # plugin
    zplug "tcnksm/docker-alias", \
        use:zshrc
    # command
    zplug "Jxck/dotfiles", \
        as:command, \
        use:"bin/{histuniq,color}"
    # theme
    zplug "S1cK94/minimal", \
        as:theme

    tests+=(
    '(( $+aliases[dl] ))'
    '[[ -x $ZPLUG_HOME/bin/histuniq ]]'
    '[[ -x $ZPLUG_HOME/bin/color ]]'
    '[[ -n $RPROMPT ]]'
    )
}

# at
{
    # branch
    zplug "b4b4r07/enhancd", \
        at:v1, \
        use:"*.sh"
    # commit
    zplug "mollifier/anyframe", \
        at:4c23cb60

    tests+=(
    '(( $+functions[enhancd_cd] ))'
    '(( $+functions[anyframe-init] ))'
    )
}

# from
{
    # github
    zplug "zsh-users/zsh-history-substring-search", \
        from:github, \
        as:plugin
    zplug "mrowa44/emojify", \
        from:github, \
        as:command
    # bitbucket
    zplug "b4b4r07/hello_bitbucket", \
        from:bitbucket, \
        as:command, \
        hook-build:"chmod 755 *.sh", \
        use:"*.sh"
    # oh-my-zsh
    zplug "plugins/emoji", from:oh-my-zsh
    zplug "plugins/git", from:oh-my-zsh
    zplug "plugins/cp", from:oh-my-zsh
    # prezto
    zplug "modules/git", from:prezto
    zplug "modules/directory", from:prezto
    # gh-r
    zplug "junegunn/fzf-bin", \
        as:command, \
        from:gh-r, \
        rename-to:f
    # gist
    zplug "b4b4r07/79ee61f7c140c63d2786", \
        from:gist, \
        as:command, \
        use:get_last_pane_path.sh
    # local
    ## skip

    tests+=(
    '(( $+functions[_zsh_highlight_bind_widgets] ))'
    '[[ -x $ZPLUG_HOME/bin/emojify ]]'
    '[[ -x $ZPLUG_HOME/bin/hello.sh ]]'
    '(( $+aliases[gba] ))'
    '(( $+functions[cpv] ))'
    '(( $+functions[random_emoji] ))'
    '(( $+aliases[gbS] ))'        # "prezto" modules/git
    '(( $+functions[git-dir] ))'  # "prezto" modules/git
    '[[ -o autocd ]]'             # "prezto" modules/directory
    '[[ -x $ZPLUG_HOME/bin/f ]]'
    '[[ ! -x $ZPLUG_HOME/bin/peco ]]' # for b4b4r07/zsh-gomi (on tag)
    '[[ -x $ZPLUG_HOME/bin/get_last_pane_path.sh ]]'
    )
}

# depth
{
    zplug "willghatch/zsh-cdr", \
        depth:1
}

# dir
{
    : 'SKIP' 'TODO'
}

# frozen
{
    : 'SKIP'
}

# hook-build
{
    # Duplicates
    zplug "b4b4r07/hello_bitbucket", \
        from:bitbucket, \
        as:command, \
        hook-build:"chmod 755 *.sh", \
        use:"*.sh"

    tests+=(
    '[[ -x $ZPLUG_HOME/bin/hello.sh ]]'
    )
}

# hook-load
{
    :
}

# if
{
    # zplug "tj/n", \
    #     as:command, \
    #     use:"bin/n", \
    #     if:'(( $+commands[node] ))'

    # tests+=(
    # '[[ -x $ZPLUG_HOME/bin/n ]]'
    # )
}

# ignore
{
    zplug "zsh-users/zaw", \
        ignore:"zaw-launcher.zsh"

    # SKIP
    # tests+=(
    # '(( ! $+functions[zle-line-init] ))'
    # )
}

# lazy
{
    zplug "mollifier/zload", \
        lazy:true, \
        use:"zload"

    tests+=(
    '(( $+functions[zload] ))'
    )
}

# defer
{
    zplug "zsh-users/zsh-syntax-highlighting", \
        defer:2

    tests+=(
    '(( $+functions[_zsh_highlight] ))'
    )
}

# on
{
    zplug "b4b4r07/zsh-gomi", \
        as:command, \
        use:"bin/gomi", \
        on:"peco/peco"

    # It should not be installed without peco/peco
    tests+=(
    '[[ ! -x $ZPLUG_HOME/bin/gomi ]]'
    )
}

# rename-to
{
    zplug "stedolan/jq", \
        from:gh-r, \
        as:command, \
        rename-to:jq

    tests+=(
    '[[ -x $ZPLUG_HOME/bin/jq ]]'
    )
}

# use
{
    zplug "b4b4r07/http_code", \
        as:command, \
        use:"bin/http_code"
    zplug "monochromegane/the_platinum_searcher", \
        as:command, \
        from:gh-r, \
        rename-to:pt, \
        use:"*${(L)$(uname -s)}*amd64*"
    zplug 'junegunn/fzf', \
        as:command, \
        use:'bin/{fzf,fzf-tmux}'

    tests+=(
    '[[ -x $ZPLUG_HOME/bin/http_code ]]'
    '[[ -x $ZPLUG_HOME/bin/pt ]]'
    '[[ ! -x $ZPLUG_HOME/bin/fzf ]]'
    '[[ -x $ZPLUG_HOME/bin/fzf-tmux ]]'
    )
}

zplug install || ret=1
zplug load --verbose || ret=1

# on (test case)

# SKIP
# this need to be tested after installation
#{
#    local -i line=0
#    local -A tags
#
#    tags[dir]="$(
#    __zplug::core::core::run_interfaces \
#        'dir' \
#        'willghatch/zsh-cdr'
#    )"
#    line="$(
#    git \
#        --git-dir="$tags[dir]/.git" \
#        --work-tree="$tags[dir]" \
#        rev-list --count HEAD
#    )"
#
#    tests+=(
#    "[[ $line -eq 1 ]]"
#    )
#}

ret=0
for test in "$tests[@]"
do
    eval "$test"
    if (( $status != 0 )); then
        printf "$fg[red]FAIL: $test$reset_color\n" >&2
        ret=1
    fi
done

exit $ret
