#!/bin/bash
#
# Copyright (c) Authors: https://www.armbian.com/authors
#
# Licensed under the GNU General Public License, version 2.
# Distributed “as is” without any warranty of any kind.

# DO NOT EDIT THIS FILE.
# To customize behavior, override options in /etc/default/armbian-motd.
# Any local edits here will be lost during board support package updates.

set -o pipefail

THIS_SCRIPT="header"
MOTD_DISABLE=""

_motd_mark_after_header() {
  local base="/run/armbian-motd"
  mkdir -p "$base" 2>/dev/null || base="${XDG_RUNTIME_DIR:-/tmp}/armbian-motd"
  mkdir -p "$base" 2>/dev/null || return 0
  local stamp="${base}/.after_header.printed"
  [[ -e "$stamp" ]] || { echo ""; : >"$stamp"; }
}

safe_source() { [[ -f "$1" ]] && . "$1"; }

# --- Load metadata from image and release files ---
safe_source /etc/armbian-image-release
VENDORTEMP="${VENDOR:-}"
safe_source /etc/armbian-release
[[ -n "${VENDORTEMP}" && "${VENDORTEMP}" != "${VENDOR:-}" ]] && VENDOR="${VENDORTEMP}"
[[ -n "${VENDORPRETTYNAME:-}" ]] && VENDOR="${VENDORPRETTYNAME}"

# --- Distribution and upgrade information ---
DISTRIBUTION_CODENAME=""
DISTRIBUTION_ID=""
upgrade=""

if [[ -f /etc/armbian-distribution-status ]]; then
	# Prefer lsb-release (safe to source; no VERSION conflict)
	if [[ -f /etc/lsb-release ]]; then
		# shellcheck disable=SC1091
		. /etc/lsb-release
		DISTRIBUTION_CODENAME="${DISTRIB_CODENAME:-${DISTRIBUTION_CODENAME}}"
		DISTRIBUTION_ID="${DISTRIB_ID:-${DISTRIBUTION_ID}}"
	fi

	# If still missing, read only the keys we need from os-release (do NOT source it)
	if [[ -z "${DISTRIBUTION_CODENAME}" || -z "${DISTRIBUTION_ID}" ]]; then
		if [[ -f /etc/os-release ]]; then
			DISTRIBUTION_CODENAME="$(awk -F= '/^VERSION_CODENAME=/{sub(/^"/,"",$2); sub(/"$/,"",$2); print $2}' /etc/os-release)"
			DISTRIBUTION_ID="$(awk -F= '/^ID=/{sub(/^"/,"",$2); sub(/"$/,"",$2); print $2}' /etc/os-release)"
		fi
	fi

	# If still missing, fallback to lsb_release command
	if [[ -z "${DISTRIBUTION_CODENAME}" || -z "${DISTRIBUTION_ID}" ]]; then
		if command -v lsb_release >/dev/null 2>&1; then
			DISTRIBUTION_CODENAME="$(lsb_release -c | awk -F: '{gsub(/\t/,"",$2); print $2}')"
			DISTRIBUTION_ID="$(lsb_release -i | awk -F: '{gsub(/\t/,"",$2); print $2}')"
		fi
	fi

	DISTRIBUTION_STATUS="$(awk -F'[=;]' -v c="${DISTRIBUTION_CODENAME}" '$1==c{print $2; exit}' /etc/armbian-distribution-status)"
	filter=$(awk -F= '/supported/{print $1}' /etc/armbian-distribution-status)
	current_line="$(awk -F';' -v c="${DISTRIBUTION_CODENAME}" '$1==c{print $2; exit}' /etc/armbian-distribution-status)"
	candidates="$(printf '%s\n' "${current_line#*=}" | tr ',' ' ')"
	for j in $filter; do
		for i in $candidates; do
			if [[ "$i" == "$j" ]]; then upgrade="$i"; fi
		done
	done
fi

safe_source /etc/default/armbian-motd
for f in ${MOTD_DISABLE}; do
	[[ "${f}" == "${THIS_SCRIPT}" ]] && exit 0
done

# --- Kernel and system info ---
KERNELID="$(uname -r)"

_motd_mark_after_header

# --- Vendor banner ---
if [[ -n "${VENDORCOLOR:-}" ]]; then
	if command -v figlet >/dev/null 2>&1; then
		printf '\e[38;2;%sm%s\e[0m\n' "${VENDORCOLOR}" "$(figlet -f small " ${VENDOR}")"
	else
		printf '\e[38;2;%sm### %s ###\e[0m\n' "${VENDORCOLOR}" "${VENDOR}"
	fi
else
	if command -v figlet >/dev/null 2>&1; then
		printf '\e[1;91m%s\e[0m\n' "$(figlet -f small " ${VENDOR}")"
	else
		printf '\e[1;91m### %s ###\e[0m\n' "${VENDOR}"
	fi
fi

# --- Board name and version line ---
# Keep BOARD_NAME from Armbian files; only special-case RPi4 model tweak.
if [[ "${BOARD:-}" == "rpi4b" ]]; then
	BOARD_NAME="$(tr '\0' '\n' < /proc/device-tree/model | sed -E 's/ Rev [0-9.]+$//')"
fi

# Preserve Armbian VERSION; if it ends with "trunk", show "rolling"
if [[ "${VERSION:-}" == *trunk* ]]; then
	VERSION="$(printf '%s' "${VERSION}" | cut -d'.' -f1-2)"
	VERSION="${VERSION} rolling"
fi

printf ' \e[0;92mv%s\x1B[0m for %s running Armbian Linux \e[0;92m%s\x1B[0m\n' \
	"${VERSION}" "${BOARD_NAME}" "${KERNELID}"

# --- Hardware support status ---
HARDWARE_STATUS=""
if [[ "${IMAGE_TYPE:-}" != "stable" ]]; then
	case "${IMAGE_TYPE:-}" in
		"user-built") HARDWARE_STATUS=$'\e[0;91mDIY\x1B[0m (custom image)\x1B[0m' ;;
		"nightly")    HARDWARE_STATUS=$'\e[0;91mfor advanced users\x1B[0m (rolling release)\x1B[0m' ;;
	esac
else
	case "${BOARD_TYPE:-}" in
		"csc"|"tvb") HARDWARE_STATUS=$'\e[0;91mDIY (community maintained)\x1B[0m' ;;
		"wip")       HARDWARE_STATUS=$'\e[0;91mfor advanced users\x1B[0m (work in progress)\x1B[0m' ;;
		"eos")       HARDWARE_STATUS=$'\e[0;91mend of life\x1B[0m' ;;
	esac
fi

# --- Distribution status line ---
case "${DISTRIBUTION_STATUS:-}" in
	supported) DISTRO_STATUS=$'\e[0;92mstable\e[0m' ;;
	eos)       DISTRO_STATUS=$'\e[0;91mend of life\e[0m' ;;
	*)         DISTRO_STATUS=$'\e[0;93mrolling\e[0m' ;;
esac
[[ -n "${DISTRIBUTION_CODENAME}" ]] && DISTRO_STATUS+=" (${DISTRIBUTION_CODENAME})"

# --- Updates status ---
if [[ -f /var/cache/apt/archives/updates.number ]]; then
	# shellcheck disable=SC1091
	. /var/cache/apt/archives/updates.number
fi
UPDATE_STATUS=""
if [[ "${NUM_UPDATES:-0}" -gt 0 ]]; then
	if apt-mark showhold 2>/dev/null | grep -q '^linux-image'; then
		UPDATE_STATUS="Kernel upgrade \e[0;91mdisabled\e[0m"
	else
		UPDATE_STATUS="Kernel upgrade \e[0;92menabled\e[0m"
	fi
	UPDATE_STATUS+=" and \e[1;92m${NUM_UPDATES}\e[0m package"
	[[ "${NUM_UPDATES}" -gt 1 ]] && UPDATE_STATUS+="s"
	UPDATE_STATUS+=" available for upgrade\e[0m "
fi

printf '\n'

# --- Display summary ---
if [[ -n "${DISTRO_STATUS:-}" ]]; then
	[[ -n "${upgrade}" ]] && DISTRO_STATUS+=", possible distro upgrade (${upgrade})"
	printf ' Packages:     %s %b\n' "${DISTRIBUTION_ID^}" "${DISTRO_STATUS}"
fi

[[ -n "${UPDATE_STATUS}" ]] && printf ' Updates:      %b\n' "${UPDATE_STATUS}"
[[ -n "${HARDWARE_STATUS}" ]] && printf ' Support:      %b\n' "${HARDWARE_STATUS}"

# ensure zero exit when executed directly
(return 0 2>/dev/null) || exit 0