#!/bin/bash

## Copyright (C) 2020 - 2023 ENCRYPTED SUPPORT LP <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## This script gets `source`ed.
## Using both 'return 0' and 'exit 0' to support both, `source`ing as well as
## executing this script.

true "$0: START"

if [ -z "$PS1" ]; then
   true "$0: INFO: Not running interactively, don't do anything, ok."
   return 0 2>/dev/null
   exit 0
fi

shopt -q login_shell
var="$?"
if [ "$var" = "0" ]; then
   true "$0: INFO: Running in a login shell, don't do anything, ok."
   ## Login shells are greeted by /etc/motd.
   return 0 2>/dev/null
   exit 0
fi

## We run in a terminal emulator.

if ! test -d /etc/update-motd.d ; then
   true "$0: INFO: /etc/update-motd.d does not exist, ok."
   return 0 2>/dev/null
   exit 0
fi

if test -f ~/.hushlogin ; then
   true "$0: INFO: ~/.hushlogin exist, ok."
   return 0 2>/dev/null
   exit 0
fi
if test -f "/etc/hushlogin" ; then
   true "$0: INFO: /etc/hushlogin exist, ok."
   return 0 2>/dev/null
   exit 0
fi
if test -f "/usr/local/etc/hushlogin" ; then
   true "$0: INFO: /usr/local/etc/hushlogin exist, ok."
   return 0 2>/dev/null
   exit 0
fi

run-parts /etc/update-motd.d || true

true "$0: END"
