#!/bin/bash
#
# Copyright (c) Authors: https://www.armbian.com/authors
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.

# DO NOT EDIT THIS FILE but add config options to /etc/default/armbian-motd
# any changes will be lost on board support package update

THIS_SCRIPT="header"
MOTD_DISABLE=""

# Read image configuration
[[ -f /etc/armbian-image-release ]] && . /etc/armbian-image-release
VENDORTEMP="${VENDOR}"

# Read update configuration
[[ -f /etc/armbian-release ]] && . /etc/armbian-release

# Keep the VENDOR from image if its defined there
[[ -n "${VENDORTEMP}" && "${VENDORTEMP}" != "${VENDOR}" ]] && VENDOR="${VENDORTEMP}"

# If VENDORPRETTYNAME is defined, used that
[[ -n $VENDORPRETTYNAME ]] && VENDOR="$VENDORPRETTYNAME"

if [[ -f /etc/armbian-distribution-status ]]; then
	. /etc/armbian-distribution-status
	# Fina a way that works
	[[ -f /etc/lsb-release ]] && DISTRIBUTION_CODENAME=$(grep CODENAME /etc/lsb-release | cut -d"=" -f2)
	[[ -f /etc/lsb-release ]] && DISTRIBUTION_ID=$(grep DISTRIB_ID /etc/lsb-release | cut -d"=" -f2)
	[[ -z "$DISTRIBUTION_CODENAME" && -f /etc/os-release ]] && DISTRIBUTION_CODENAME=$(grep VERSION_CODENAME /etc/os-release | cut -d"=" -f2)
	[[ -z "$DISTRIBUTION_ID" && -f /etc/os-release ]] && DISTRIBUTION_ID=$(grep "^ID" /etc/os-release | cut -d"=" -f2)
	[[ -z "$DISTRIBUTION_CODENAME" && -x /usr/bin/lsb_release ]] && DISTRIBUTION_CODENAME=$(/usr/bin/lsb_release -c | cut -d":" -f2 | tr -d "\t")
	[[ -z "$DISTRIBUTION_ID" && -x /usr/bin/lsb_release ]] && DISTRIBUTION_ID=$(/usr/bin/lsb_release -i | cut -d":" -f2 | tr -d "\t")
	# Read Armbian distribution status
	DISTRIBUTION_STATUS=$(grep "$DISTRIBUTION_CODENAME" /etc/armbian-distribution-status | cut -d"=" -f2)
fi
[[ -f /etc/default/armbian-motd ]] && . /etc/default/armbian-motd

for f in $MOTD_DISABLE; do
	[[ $f == $THIS_SCRIPT ]] && exit 0
done

# Read Armbian kernel version
KERNELID=$(uname -r)

# Display software vendor logo
echo -e "\e[1;91m$(figlet -f small " $VENDOR")\e[0m";

# Display version, board, and kernel version
[[ $VERSION == *trunk* ]] && VERSION=$(echo -e $VERSION | cut -d"." -f1-2 | sed "s/\$/ rolling/")
echo -e " \e[0;92mv${VERSION}\x1B[0m for $BOARD_NAME running Armbian Linux \e[0;92m${KERNELID^}\x1B[0m"

# render image and board type
if [[ "$IMAGE_TYPE" != "stable" ]]; then
	[[ "$IMAGE_TYPE" == "user-built" ]] && HARDWARE_STATUS="\e[0;91mDIY\x1B[0m (custom image)\x1B[0m"
	[[ "$IMAGE_TYPE" == "nightly" ]] && HARDWARE_STATUS="\e[0;91mfor advanced users\x1B[0m (rolling release)\x1B[0m"
else
	[[ "$BOARD_TYPE" == "csc" || "$BOARD_TYPE" == "tvb" ]] && HARDWARE_STATUS="\e[0;91mDIY (community maintained)\x1B[0m"
	[[ "$BOARD_TYPE" == "wip" ]] && HARDWARE_STATUS="\e[0;91mfor advanced users\x1B[0m (work in progress)\x1B[0m"
	[[ "$BOARD_TYPE" == "eos" ]] && HARDWARE_STATUS="\e[0;91mend of life\x1B[0m"
fi

# render distribution status
if [[ $DISTRIBUTION_STATUS == supported ]]; then
	DISTRO_STATUS="\e[0;92mstable\e[0m ($DISTRIBUTION_CODENAME)"
elif [[ $DISTRIBUTION_STATUS == eos ]]; then
	DISTRO_STATUS="\e[0;91mend of life\e[0m ($DISTRIBUTION_CODENAME)"
else
	DISTRO_STATUS="\e[0;93mrolling\e[0m ($DISTRIBUTION_CODENAME)"
fi

# read packages update status
NUM_UPDATES=0
NUM_UPDATES_ONHOLD=0
NUM_SECURITY_UPDATES=0
[[ -f /var/cache/apt/archives/updates.number ]] && . /var/cache/apt/archives/updates.number
if [[ $NUM_UPDATES -gt 0 ]]; then
	if apt-mark showhold | grep -q linux-image 2>/dev/null; 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"
	# Cosmetic is important
	[[ $NUM_UPDATES -gt 1 ]] && UPDATE_STATUS+="s"
	UPDATE_STATUS+=" available for upgrade\e[0m "
fi

echo ""

# Display packages status
if [[ -n $DISTRO_STATUS ]]; then
	echo -e " Packages:     ${DISTRIBUTION_ID^} ${DISTRO_STATUS}"
fi

# Display available updates
if [[ -n $UPDATE_STATUS ]]; then
        echo -e " Updates:      $UPDATE_STATUS"
fi

# Display hardware support status
if [[ -n $HARDWARE_STATUS ]]; then
        echo -e " Support:      $HARDWARE_STATUS"
fi
