#!/bin/sh
# Render /usr/lib/os-release and /etc/lsb-release from the base's own os-release and, where the
# image names one, the Based edition. postinst runs this on configure and on the base-files
# trigger; an image runs it again once it has laid /etc/based/edition, which lands after apt
# configured this package (BASED-1109). Every path is under $DPKG_ROOT.
set -e

name="Based Linux"
distrib_id=Based
home_url="https://basedlinux.com/"

# The base's own os-release: base-files' file lands at the diverted path from its first
# upgrade on, and preinst set the same content aside before that.
base="$DPKG_ROOT/usr/lib/os-release.debian"
# The edition this system is, written by the image from its identity: NAME is what the system
# is called, ID the edition's os-release VARIANT_ID. Absent, the system is plain Based Linux.
edition="$DPKG_ROOT/etc/based/edition"

file_value() {
	if [ -r "$1" ]; then
		sed -n "s/^$2=//p" "$1" | head -n1 | sed 's/^"\(.*\)"$/\1/'
	fi
}

os_value() {
	file_value "$base" "$1"
}

put() {
	mv -f "$1.based-tmp" "$1"
	chmod 0644 "$1"
}

# The product version: the base ID's initial, uppercased, then the release. Debian takes the
# major (D13). Ubuntu takes the year, bare for an LTS (.04 in an even year: U24, U26) and
# lettered for an interim, a for .04 and b for .10 (U25a, U25b). Anything else keeps the whole
# version, dots stripped. The image engine derives identity.version the same way
# (linux_factory.engine.product_version), so the ISO label and the installed system agree.
product_version() {
	initial="$(printf '%s' "$1" | cut -c1 | tr '[:lower:]' '[:upper:]')"
	case "$1:$2" in
	debian:*) printf '%s%s' "$initial" "${2%%.*}" ;;
	ubuntu:[0-9][02468].04) printf '%s%s' "$initial" "${2%.04}" ;;
	ubuntu:[0-9][0-9].04) printf '%s%sa' "$initial" "${2%.04}" ;;
	ubuntu:[0-9][0-9].10) printf '%s%sb' "$initial" "${2%.10}" ;;
	*) printf '%s%s' "$initial" "$(printf '%s' "$2" | tr -d .)" ;;
	esac
}

[ -f "$base" ] || exit 0
id="$(os_value ID)"
id_like="$(os_value ID_LIKE)"
version_id="$(os_value VERSION_ID)"
codename="$(os_value VERSION_CODENAME)"
variant="$(file_value "$edition" NAME)"
variant_id="$(file_value "$edition" ID)"
product="${variant:-$name}"
tag=""
if [ -n "$version_id" ]; then
	tag="$(product_version "$id" "$version_id")"
fi
pretty="$product"
if [ -n "$tag" ]; then
	pretty="$pretty $tag"
fi
if [ -n "$codename" ]; then
	pretty="$pretty ($codename)"
fi

target="$DPKG_ROOT/usr/lib/os-release"
{
	printf 'PRETTY_NAME="%s"\n' "$pretty"
	printf 'NAME="%s"\n' "$product"
	if [ -n "$tag" ]; then
		printf 'VERSION_ID=%s\n' "$(printf '%s' "$tag" | tr '[:upper:]' '[:lower:]')"
		printf 'VERSION="%s"\n' "$tag${codename:+ ($codename)}"
	fi
	if [ -n "$codename" ]; then
		printf 'VERSION_CODENAME=%s\n' "$codename"
	fi
	printf 'ID=based\n'
	printf 'ID_LIKE="%s"\n' "$id${id_like:+ $id_like}"
	if [ -n "$variant" ]; then
		printf 'VARIANT="%s"\n' "$variant"
	fi
	if [ -n "$variant_id" ]; then
		printf 'VARIANT_ID=%s\n' "$variant_id"
	fi
	printf 'HOME_URL="%s"\n' "$home_url"
	# LOGO names an ICON, not a path: GNOME's About panel and gnome-software look
	# `based` up in the icon theme. The based overlay installs it at hicolor
	# scalable + 48/64/128/256 and /usr/share/pixmaps/based.png (BASED-251). Without
	# both halves the panel silently falls back to the base distro's logo, which is
	# what every Based image did until now.
	printf 'LOGO=based\n'
	# the brand accent, for a terminal that colours the distro name
	printf 'ANSI_COLOR="0;38;2;47;109;246"\n'
	# the base's own keys, which scripts that add a PPA or a Debian point release read
	for key in DEBIAN_VERSION_FULL UBUNTU_CODENAME; do
		value="$(os_value "$key")"
		if [ -n "$value" ]; then
			printf '%s=%s\n' "$key" "$value"
		fi
	done
} >"$target.based-tmp"
put "$target"

target="$DPKG_ROOT/etc/lsb-release"
{
	printf 'DISTRIB_ID=%s\n' "$distrib_id"
	printf 'DISTRIB_RELEASE=%s\n' "$tag"
	printf 'DISTRIB_CODENAME=%s\n' "$codename"
	printf 'DISTRIB_DESCRIPTION="%s"\n' "$pretty"
} >"$target.based-tmp"
put "$target"
