#!/bin/bash -e

# Copyright (c) 2014 The CoreOS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# We want to grab the GROUP variable
[ -e /usr/share/flatcar/update.conf ] && source /usr/share/flatcar/update.conf
[ -e /etc/flatcar/update.conf ] && source /etc/flatcar/update.conf

source /usr/lib/os-release

mkdir -p /run/flatcar
ln -sfT flatcar /run/coreos
OEM=""
if [ -f /oem/oem-release ]; then
  OEM="$(source /oem/oem-release; echo " for $NAME")"
fi
echo -e "\e[${ANSI_COLOR}m${NAME}\e[39m ${GROUP} ${VERSION}${OEM}" > /run/flatcar/motd

if [[ -d "/etc/motd.d" ]]; then
	cat /etc/motd.d/*.conf 2>/dev/null >> /run/flatcar/motd || true
fi

if ! ( mount | grep -q cgroup2 ); then
	echo -e "\e[33mWarning\e[39m: using legacy cgroups" >> /run/flatcar/motd
	echo -e "Read more: https://kinvolk.io/docs/flatcar-container-linux/latest/container-runtimes/switching-to-unified-cgroups" >> /run/flatcar/motd
fi

if [[ "${GROUP}" = "lts" ]] && [[ "${SERVER}" = "https://public.update.flatcar-linux.net/v1/update/" ]]; then
	echo -n "/etc/flatcar/update.conf: GROUP=lts on the public update server may switch to a newer LTS stream, " >> /run/flatcar/motd
	echo "use GROUP=lts-STREAM or your own update server with a managed 'lts' group to stick to one stream (e.g., 2021) and opt-out of automatic major version updates." >> /run/flatcar/motd
	echo "Read more: https://www.flatcar.org/docs/latest/setup/releases/switching-channels/#freezing-an-lts-stream" >> /run/flatcar/motd
fi
