# shellcheck shell=sh   disable=SC3043,2120

xrc:mod:lib scotty      mod/other mod/build

___x_cmd_scotty_mod(){
    param:subcmd ___x_cmd_scotty_mod                        \
        wc          "wc"                                    \
        build       "advise_t_json"                         \
        tgz         "package"                               \
        test        "test"                                  \
        "install|i" "cp file to home folder"                \
        lsfile      "ls file to p"                          \
        push        "push all"                              \
        sha512      "generate sha512 for the module"        \
        name        "name"                                  \
        lsfile0     "ls file to p"

    x:trace scotty/mod
    param:subcmd:try

    param:run
    ___x_cmd_scotty help mod
    return 1
}

___x_cmd_scotty_mod_name_(){
    ___X_CMD_SCOTTY_MOD_NAME_="$(___x_cmd git meta path)"
    ___X_CMD_SCOTTY_MOD_NAME_="${___X_CMD_SCOTTY_MOD_NAME_%.git}"
    ___X_CMD_SCOTTY_MOD_NAME_="${___X_CMD_SCOTTY_MOD_NAME_##*/}"
}

___x_cmd_scotty_mod_name(){
    param:void

    local ___X_CMD_SCOTTY_MOD_NAME_
    ___x_cmd_scotty_mod_name_ || return 1
    printf "%s\n" "$___X_CMD_SCOTTY_MOD_NAME_"
}

___x_cmd_scotty_mod_push(){
    param:void
    git push "git@gitee.com:$(___x_cmd git meta path)" "$(___x_cmd git meta branch)"      # TODO: don't push gitee.
    git push "git@github.com:$(___x_cmd git meta path)" "$(___x_cmd git meta branch)"
    # git push "git@gitcode.net:$(___x_cmd git meta path)" "$(___x_cmd git meta branch)"
}

# Section: lsfile install
___x_cmd_scotty_mod_lsfile0()(
    param:void
    ___x_cmd_inner_cd "$(___x_cmd wsroot)" || exit

    local IFS="$___X_CMD_UNSEENCHAR_NEWLINE"
    local i
    command find . -type f  -regex './latest'           \
                    -or     -regex './lib/.*'           \
                    -or     -regex './v.*/.*'           \
                    -or     -regex './_v.*/.*'          \
                    -or     -regex './third-party/.*' | \
    while read -r i; do
        [ "$i" = "${i#*_test*}" ] || continue
        # [ "$i" != "${i%.test}" ] && continue
        [ ! -f "$i" ] || printf "%s\n" "$i"
    done
)

___x_cmd_scotty_mod_lsfile()(
    param:void
    # x:trace scotty/mod/lsfile
    if [ -f "$(___x_cmd wsroot)/.x-cmd/lsfile" ]; then
        # TODO: using engine to directly run the local file
        X_DISABLE_WORKSPACE_EXECUTION_CONFIRMATION=1 ___x_cmd ws lsfile
    else
        ___x_cmd_scotty_mod_lsfile0
    fi
)
# EndSection

# Section: install
___x_cmd_scotty_mod_install_tmp(){
    local src="$2"
    local dst="$1/$src"
    scotty:debug "$(printf "Moving:   %-30s\t%s\n" "$src" "$dst")"
    ___x_cmd_cmds_mkdir -p "$(command dirname "$dst")"
    ___x_cmd_cmds_cp "$src" "$dst"
}

___x_cmd_scotty_mod_code(){
    ___X_CMD_YANFA_MOD_CODE_LINE=0
    ___X_CMD_YANFA_MOD_CODE_CHAR=0
    ___X_CMD_YANFA_MOD_CODE_BYTE=0
    eval "$(command xargs wc 2>/dev/null | command tail -n1 | ___x_cmd_cmds_awk '{
        print "___X_CMD_YANFA_MOD_CODE_LINE=" $1
        print "___X_CMD_YANFA_MOD_CODE_CHAR=" $2
        print "___X_CMD_YANFA_MOD_CODE_BYTE=" $3
    }')"
}

___x_cmd_scotty_mod_install(){(
    param:void
    x:trace scotty/mod/install
    ___x_cmd_inner_cd "$(___x_cmd wsroot)" || exit
    local current_folder
    local x_=; ___x_cmd wsroot_
    local mod; mod="$(basename "$x_")"
    current_folder="$___X_CMD_ROOT_MOD/$mod"

    if [ -f "$x_/.x-cmd/install" ]; then
        scotty:info "Running in workspace: install"
        # TODO: using engine to directly run the local file
        ___x_cmd ws install "$@"
    elif [ "$current_folder" != "$x_" ]; then
        [ ! -d "$current_folder" ] || (
            ___x_cmd_cmds_rm -rf "$current_folder" && ___x_cmd_cmds_mkdir -p "$current_folder"
        )
        scotty:info "Install to folder=$current_folder"

        # TODO: Using codi to replace
        ___x_cmd_scotty_mod_lsfile | {
            awk=0
            sh=0
            txt=0
            json=0
            jso=0
            total=0

            sh_list=""; awk_list="";
            while read -r line; do
                res="$res\t$line"
                total=$(( total + 1 ))
                case "$line" in
                    *.txt)      txt=$((     txt  +1      )) ;;
                    *.awk)      awk=$((     awk  +1      ))
                                awk_list="$awk_list $line"
                                ;;
                    *.json)     json=$((    json +1      )) ;;
                    *.jso)      jso=$((     jso  +1      )) ;;
                    *)          sh=$((      sh   +1      ))
                                sh_list="$sh_list $line"
                                ;;
                esac
                ___x_cmd_scotty_mod_install_tmp "$current_folder" "$line"
            done

            ___x_cmd_scotty_mod_code <<A
$sh_list
A
            sh_line="$___X_CMD_YANFA_MOD_CODE_LINE"
            ___x_cmd_scotty_mod_code <<A
$awk_list
A
            awk_line="$___X_CMD_YANFA_MOD_CODE_LINE"
            scotty:info "Files total=${total} sh=${sh} awk=${awk} json=${json} jso=${jso} txt=${txt}"
            scotty:info "Lines total=${total} sh=${sh_line} awk=${awk_line}"
        }

        # buggy code
        # ___x_cmd_scotty_mod_lsfile | ___x_cmd_args 1 ___x_cmd_scotty_mod_install_tmp "$current_folder"
    fi

    ___x_cmd_scotty_mod_build --advise "$mod"
)}

# EndSection

# Section: pkg
___x_cmd_scotty_mod_tgz(){
    param:void
    local WSROOT;   WSROOT="$(___x_cmd wsroot)"

    local scope;    scope="$(___x_cmd_scotty_mod_name)";  scope="${scope#*/}"

    ___x_cmd mkdirp "$WSROOT/dist"

    ___x_cmd_scotty_mod_lsfile | command sort | command uniq | ___x_cmd z "$scope.tgz" 2>/dev/null &&
        ___x_cmd_cmds_mv "$scope.tgz" "$WSROOT/dist/$scope.tgz" &&
        ___x_cmd_cmds_ls -alh "$WSROOT/dist/$scope.tgz"
}
# EndSection

# Section: common: wc/test
___x_cmd_scotty_mod_wc(){
    param:void
    x:trace scotty/mod/wc
    ___x_cmd_scotty_mod_lsfile "$@" | command xargs wc | command sort -n -r | less
}
# EndSection


___x_cmd_scotty_mod_sha512(){
    param:void
    local sha512; sha512=$( command find lib latest -type f | command sort | command xargs cat | ___x_cmd sha512 )
    printf "%s %s\n" "$sha512" "$mod"
}

___x_cmd_scotty_mod_test(){  param:void ; }
