#!/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 -sf flatcar /run/coreos
OEM=""
if [ -f /usr/share/oem/oem-release ]; then
  OEM="$(source /usr/share/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" ]]; then
	echo "FCL LTS documentation: https://kinvolk.io/fcl-lts-docs" >> /run/flatcar/motd
fi
if [[ "${GROUP}" =~ "lts" ]] && { [[ "${DOWNLOAD_USER}" = "" ]] || [[ "${DOWNLOAD_PASSWORD}" = "" ]] ; }; then
	echo "/etc/flatcar/update.conf: DOWNLOAD_USER and DOWNLOAD_PASSWORD are missing to download LTS updates." >> /run/flatcar/motd
	echo "Documentation for update.conf: https://kinvolk.io/docs/flatcar-container-linux/latest/clusters/management/update-conf" >> /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." >> /run/flatcar/motd
fi
