#!/bin/sh
#
# Run tcsh builds using Docker images
#
# 20220513  Kimmo Suominen
#

set -eu

DOCKER_BUILDKIT=1
export DOCKER_BUILDKIT

Dockerfile()
{
    local os suite

    os="${1}"
    suite="${2}"

    expand <<EOF
# syntax=docker/dockerfile:1.4

FROM ${os}:${suite}

ARG DEBIAN_FRONTEND=noninteractive
ARG APT_LISTCHANGES_FRONTEND=none
RUN apt-get -qq update\
 && apt-get -qq install $(list_dependencies)\
 && apt-get -qq clean
EOF

    case "${suite}" in
    bookworm)
	expand <<EOF
RUN grep -q '^VERSION_ID=' /etc/os-release\
 || echo VERSION_ID=12 >> /etc/os-release
EOF
	;;
    esac

    expand <<EOF
RUN useradd -U -m builder

COPY --link docker-entrypoint /

USER builder:builder

ENTRYPOINT ["/docker-entrypoint"]
CMD ["dpkg-buildpackage", "--no-sign", "-nc"]
EOF
}

die()
{
    echo "${PROG}: ${@}" 1>&2
    exit 1
}

list_dependencies()
{
    paste -sd ' ' <<-EOF
	build-essential
	debhelper
	devscripts
	jetring
	libncurses5-dev
	EOF
}

list_suites()
{
    local os

    os="${1}"

    case "${os}" in
    debian)
	echo bookworm
	echo bullseye
	echo buster
	echo stretch
	;;
    ubuntu)
	echo kinetic
	echo jammy
	echo focal
	echo bionic
	;;
    esac
}

maybe()
{
    if ${noop:-false}
    then
	echo - "${@}"
    else
	echo + "${@}"
	"${@}"
    fi
}

usage()
{
    cat <<EOF
Usage:	${PROG} [-hnv] [target]

Builds deb/* packages from the src/*.orig.tar.gz files for select
Debian and Ubuntu releases.

Options:

-h	Show this usage
-n	Do not build anything (no-op)
-v	Show docker build output (verbose)
EOF
}

PROG="${0##*/}"
PDIR="$(dirname $(realpath "${0}"))"
DEPT="${PDIR}/docker-entrypoint"

noop=
quiet_build=-q

while getopts hnv opt
do
    case "${opt}" in
    h)	usage; exit 0;;
    n)	noop=true;;
    v)	quiet_build=;;
    *)	usage 1>&2; exit 1;;
    esac
done
shift $((${OPTIND} - 1))

if [ ${#} -gt 1 ]
then
    usage 1>&2
    exit 2
fi

[ -f "${DEPT}" ] || die "${DEPT} not found"

[ -d deb ] || die 'Are you in the right place? No deb directory found.'
[ -d src ] || die 'Are you in the right place? No src directory found.'

here="$(realpath .)"

case "${1:-all}" in
all)
    ;;
debian|ubuntu)
    os_list="${1}"
    ;;
*)
    for os in debian ubuntu
    do
	if list_suites "${os}" | grep -q "^${1}$"
	then
	    os_list="${os}"
	    suite_list="${1}"
	    break
	fi
    done
    case "${os_list:-}" in
    '')
	die "Unknown target: ${1}"
	;;
    esac
    ;;
esac

first=true
for os in ${os_list:-debian ubuntu}
do
    for suite in ${suite_list:-$(list_suites "${os}")}
    do
	image="tcsh-build/${os}-${suite}"
	${first} && first=false || echo
	echo "* ${image}"
	dir="$(mktemp -d "${TMPDIR:-/tmp}/${PROG}-img-XXXXXXXXXX")"
	maybe install "${DEPT}" "${dir}/docker-entrypoint"
	Dockerfile "${os}" "${suite}" \
	| if ${noop:-false}
	then
	    sed 's/^/> /'
	else
	    cat > "${dir}/Dockerfile"
	fi
	maybe docker build ${quiet_build} -t "${image}" "${dir}"
	rm -rf "${dir}"

	pool="deb/${os}/pool/${suite}/main"
	[ -d "${pool}" ] && continue
	maybe mkdir -p "${pool}"

	maybe docker run --rm \
	    -v "${here}/src":/mnt/src \
	    -v "${here}/${pool}":/mnt/deb \
	    "${image}"
    done
done
