# shellcheck shell=dash

# cp :/home/el/  ~/abc/cde.txt ./work   => ./work as diretory, not a file

___x_cmd_mv_colon___mvfile(){
    local basedir="${1:?Provide basedir}"
    local dst
    local srcfile;  while read -r srcfile; do
        dst="$dst_folder/${srcfile#"${basedir}"}"
        x mkdirp "${dst%/*}"
        ___x_cmd_cmds_mv "$srcfile" "$dst"
    done
}

# TODO: Using parallel
# TODO: Fail then halt? Or just continue?
___x_cmd_mv_colon(){
    local basedir="${1:-$PWD}"; shift
    [ "$basedir" != . ] || basedir="$PWD"

    local x_;    ___x_cmd_abspath_ "${basedir:-$PWD}";   basedir="${x_}"
    local dst_folder;           eval dst_folder=\"\$$#\";               dst_folder="${dst_folder%/}"
    local dst

    local srcfile
    local srcfile_relative

    if [ "$#" -eq 1 ]; then     ___x_cmd_cmds_cat
    else                        while [ "$#" -ge 2 ]; do    printf "%s\n" "$1"; shift;  done
    fi | (
        while read -r srcfile; do
            case "$srcfile" in
                /*)     ;;
                *)      srcfile="$PWD/${srcfile#/}"
            esac
            ___x_cmd_abspath_ "${srcfile}";   srcfile="${x_}"

            if [ "$srcfile" = "$basedir" ]; then
                command find "$srcfile" -type f | ___x_cmd_mv_colon___mvfile "$basedir"
                continue
            fi

            srcfile_relative="${srcfile#"${basedir}"}"
            if [ "$srcfile_relative" = "$srcfile" ]; then
                x:error "Filepath[$srcfile] does NOT contains $basedir"
                continue
            fi

            dst="$dst_folder/$srcfile_relative";    x mkdirp "${dst%/*}"
            ___x_cmd_cmds_mv "$srcfile" "$dst"
        done
    )
}
