#!/bin/sh

VERSION="2.1"

print_help()
{
    printf "%s\n" "
Usage:
any rebuild DATA LIST

    Rebuild absent and outdated packages, found among LIST.
    
    Package is outdated, if its inner version is higher, then inner version
    from binary pack.
    Package is absent, if there are no declared binary packs for it.
"
}

eval "$(dumprdd -g "${@}" rdd_prf_id="output,buildfresh" )"
if test -z "${ANYAPI}" ; then
    printf "%s\n" "
Failed to init shell environment, needed for command execution.
The command must be launched within working directory.
Check your cmd arguments and correctness of data.
Check your dumprdd executable as well.
"
    exit 2
fi

mkdir -p "${BASEDIR}tmp"
tmpdump="$(od -An -N16 -tx1 /dev/urandom | tr -d ' ')"
dropname="${BASEDIR}tmp/rebuild-${tmpdump}.txt"
: > "${dropname}"

printf "%s\n" "Saving packages to rebuild here:
${dropname}
"

list_parse "${rdd_list_entry}" | while read packname ; do
    (
    load_name_shell "${packname}"
    T="${TMPDIR}/rebuild/${P}"
    mkdir -p "${T}"

    if test "${any_rebuild_fetch}" != '0' ; then
        # fetch fresh inner version
        fetch_dev_inner_version
    fi
    # whether need we to rebuild this
    buildfresh_map >> "${dropname}"
    ) || continue
done

if test -s "${dropname}" ; then
    any do "${@}" rdd_list_entry="${dropname}"
else
    printf "%s\n" "Everything is up to date, nothing to rebuild."
fi
