# shellcheck shell=dash

# x webtop image refit --script custom  debian-kde  custom

___x_cmd_webtop_image_refit(){
    local update=""
    local script=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m webtop image refit; return ;;
            --update)   update=1;       shift   ;;
            --script)   script="$2";    shift 2 ;;
            *)          break ;;
        esac
    done

    local tag="$1";         [ -n "$tag" ]       || M='Provide a webtop image'       N=webtop log:ret:1
    local variant="${2}";   [ -n "$variant" ]   || M='Provide a Custom Identifiers' N=webtop log:ret:1

    local image="${___X_CMD_WEBTOP_URL_PREFIX}/webtop:${tag}"
    local newimage="$image"
    case "$newimage" in
        *-*-*)
            newimage="${newimage#*-*}"
            newimage="${image%"$newimage"}"
            ;;
        *-*)
            ;;
    esac

    newimage="${newimage}-${variant}"

    docker:info "Refitting [image=$image]: adding font-noto using system pkg manager"

    # not update, and image exists
    if [ -z "$update" ] && ___x_cmd docker image exist "$newimage" ; then
        webtop:info "Image already exists -> $newimage"
        return 0
    fi

    ___x_cmd_webtop_image_pull ${update:+"--update"} "$image" \
        || N=webtop M="Fail to pull image -> $image" log:return

    ___x_cmd docker refit --from "${image}" --to "${newimage}" --script "${script}" \
        || N=webtop M="Fail to refit image" log:return
}

