#!/bin/sh

rootdir="$(rootrdd)"
if test -z "${rootdir}" ; then
    printf "%s\n" "
Root directory has not been found.
You should have 'rootrdd' executable in PATH variable.
" >&2
    exit 3
fi

mkdir -p "${rootdir}/var/tmp/"
tmpmnt="$(od -An -N16 -tx1 /dev/urandom | tr -d ' ')"
if test -z "${tmpmnt}" ; then
    printf "%s\n" "
Troubles with pseudorandom strings.
They lead to possibly overlapping temporary files and errors.
Check reading from /dev/urandom.
" >&2
else
    tmpmnt="-${tmpmnt}"
fi
dropname="${rootdir}/var/tmp/pkg-dump${tmpmnt}"
dumprdd "${@}" rdd_prf_id="resolve,output" any_dump="${dropname}" > "${dropname}"

if test ! -s "${dropname}" ; then
    rm -f "${dropname}"
    printf "%s\n" "
Failed to write down dump file with shell script, needed for execution.
This command should be executed inside working directory.
Such directory contains rdd.def configuration file
or rdd.conf.d directory at its root.
Also check writable permissions of directory
${rootdir}/var/tmp/
" >&2
    exit 2
fi

printf "%s\n" "${dropname}"
