#! /bin/bash
OUTFILE="$1"
PKG="$2"
PKGVER="$3"
SRCURL="$4"
PLATFORM="$5"
REQS="$6"
DESC="$7"
LICENSE="$8"
TMPFILE="${TMPDIR:-/tmp}/reqfile-$$${RANDOM}${RANDOM}${RANDOM}"
export OUTFILE PKG PKGVER SRCURL PLATFORM REQS DESC TMPFILE
VALIDSYNTAX=1
if [ -z "${OUTFILE}" ]; then
echo 'ERROR: Output not specified' >&2
VALIDSYNTAX=0
fi
if [ -z "${PKG}" ]; then
echo 'ERROR: Package not specified' >&2
VALIDSYNTAX=0
fi
if [ -z "${PKGVER}" ]; then
echo 'ERROR: Package version not specified' >&2
VALIDSYNTAX=0
fi
if [ -z "${PLATFORM}" ]; then
echo 'ERROR: Platform not specified' >&2
VALIDSYNTAX=0
fi
if [ -z "${DESC}" ]; then
echo 'ERROR: Description not specified' >&2
VALIDSYNTAX=0
fi
if [ "${VALIDSYNTAX}" = "0" ]; then
echo 'Usage: create_teapot <output> <pkg> <pkgver> <srcurl> <platform> <reqs> <desc>' >&2
exit 1
fi
OUTSHORT=$(echo "${OUTFILE}" | sed 's@^.*/@@')
TEXTFILE=0
if [ "${OUTSHORT}" = "teapot.txt" ]; then
TEXTFILE=1
fi
if [ ! -e "${OUTFILE}" ]; then
TEXTFILE=1
fi
rm -f "${TMPFILE}"
cat << __EOF__ > "${TMPFILE}"
Package ${PKG} ${PKGVER}
Meta platform ${PLATFORM}
Meta rsk::build::date $(date +%Y-%m-%d)
Meta description ${DESC}
__EOF__
if [ -n "${LICENSE}" ]; then
echo "Meta license ${LICENSE}" >> "${TMPFILE}"
fi
echo "${REQS}" | tr ',' "\n" | while read req; do
req="$(echo "${req}" | sed 's@^ *@@;s@ *$@@')"
if [ -z "${req}" ]; then
continue
fi
if echo "${req}" | grep ' ' >/dev/null; then
req="{${req}}"
fi
echo "Meta require ${req}"
done | sort -u >> "${TMPFILE}"
if [ "${TEXTFILE}" = "1" ]; then
cat "${TMPFILE}" > "${OUTFILE}"
else
(
head -1 "${OUTFILE}"
echo ''
echo '# @@ Meta Begin'
sed 's@^@# @' "${TMPFILE}"
echo '# @@ Meta End'
echo ''
tail +2 "${OUTFILE}"
) > "${OUTFILE}.new"
cat "${OUTFILE}.new" > "${OUTFILE}"
rm -f "${OUTFILE}.new"
fi
rm -f "${TMPFILE}"