#!/bin/sh
### nope.sh  -*- Sh -*-
## Create a dummy Provides: ${package} .deb package.

### Ivan Shmakov, 2019

## To the extent possible under law, the author(s) have dedicated
## all copyright and related and neighboring rights to this software
## to the public domain worldwide.  This software is distributed
## without any warranty.

## You should have received a copy of the CC0 Public Domain Dedication
## along with this software.  If not, see
## <http://creativecommons.org/publicdomain/zero/1.0/>.

### History:

## 0.2  2019-06-16 19:17Z
##      Allow more than one provided package per resulting one.  Note
##      that this breaks compatibility with the older PACKAGE [VERSION]
##      calling convention.  Exit with an error if any argument begins
##      with a dash (-).  Show the versions in the Description:, too.
##      Support NOPE_PACKAGE (with the default being the name of the
##      first package given with the no- prefix), NOPE_VERSION (0.1),
##      and NOPE_FMT (fmt -cw72 by default, if fmt is available, and
##      cat otherwise) for Description: and README.  Use date -R for
##      portability.  Use also set -u.
##
## 0.1  2019-04-27 22:20:30Z
##      (sfn.hjlwIOM4_pcY_MMDqiVzRf9YbQQNGnNubgCUYAJRe2Q.sh)
##      Initial revision.

### Code:

set -e
set -C
set -u

case "${1:-}$#" in
    (0 | -*)
        printf %s\\n "Usage: sh ${0##*/}  PACKAGE[=VERSION]..." >&2
        ## .
        exit 1 ;;
esac

: "${NOPE_EMAIL:=${LOGNAME}@example.invalid}"
: "${NOPE_REALNAME:=$(getent -- passwd "$LOGNAME" | cut -d: -f5)}"
: "${NOPE_SUITE:=workarounds}"
if [ -n "${NOPE_FMT:-}" ] ; then
    : do nothing
elif command -v -- fmt > /dev/null ; then
    NOPE_FMT=fmt\ -cw72
else
    NOPE_FMT=cat
fi

p=${1%=*}
no=${NOPE_PACKAGE:-no-${p}}
w=${NOPE_VERSION:-0.1}
prov=
for q in "$@" ; do
    case "$q" in
        (*=*)   pa=${q%=*}  ; v=${q##*=} ;;
        (*)     pa=${q}     ; v= ;;
    esac
    prov=${prov:+${prov}, }${pa}${v:+ (= ${v})}
done
d=$(mktemp -d -- "$no".XXXXXXXX)

## FIXME: do cleanup on exit
# trap cleanup EXIT

doc="${d}/usr/share/doc/${no}"

mkdir -- "$d"/DEBIAN \
    "$d"/usr "$d"/usr/share "$d"/usr/share/doc "$doc"

exec 5>&1

exec > "$d"/DEBIAN/control 

cat <<EOF
Package: ${no}
Version: ${w}
Architecture: all
Provides: ${prov}
Maintainer: ${NOPE_REALNAME} <${NOPE_EMAIL}>
Priority: extra
Description: dummy package to work-around unwarranted dependencies on ${p}
EOF
${NOPE_FMT} <<EOF
 This dummy package exists solely for its
 Provides: ${prov} magic.
EOF

exec > "$doc"/changelog 

printf %s\\n "${no} (${w}) ${NOPE_SUITE}; urgency=medium" ""

${NOPE_FMT} <<EOF
  * Created a dummy Provides: ${prov}
    package to work-around unwarranted dependencies on the respective
    packages.
EOF

printf %s\\n "" " -- ${NOPE_REALNAME} <${NOPE_EMAIL}>  $(date -R)"

exec >&5

case "$d" in
    (*/*) cd "${d%/*}" ;;
esac

## .
${NOPE_DPKG_DEB:-dpkg-deb} --build -- "${d##*/}" .

### nope.sh ends here
