MOTDfetch/00-motdfetch
2023-02-07 16:10:46 +07:00

122 lines
3.7 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# MOTDfetch is a modular and dynamic MOTD replacement
# and a very nice command line information tool for Debian/Ubuntu systems.
#
# repository: https://git.tkapias.net/tkapias/MOTDfetch
# author: Tomasz Kapias
# email: tomasz@tkapias.net
#
# modules path: ./motdfetch.d/
# config paths: $HOME/.config/motdfetch/motdfetch.conf
# /etc/motdfetch/motdfetch.conf
#
# requirements (modules apart): sudo apt install coreutils bc
# locale env
unset LC_ALL
export LC_MESSAGES=C
# GLOBAL COLORS ######################################################
c_txt="39"
c_txt_emphase="35"
c_txt_deco="97"
c_txt_invert="30"
c_bg="47"
c_danger="31"
c_warning="33"
c_success="32"
c_title="${c_bg};1;${c_txt_invert}m"
# RUN OPTIONS #######################################################
# MODTfetch directory path
SCRIPT_DIR=$(dirname "$(realpath "$0")")
Help()
{
echo -e "\e[1;${c_txt_emphase}mMOTDfetch\e[1;${c_txt}m is a modular and dynamic MOTD replacement and a very"
echo -e "nice command line information tool for Debian/Ubuntu systems.\e[0m"
echo
echo "Syntax: bash $SCRIPT_DIR/00-motdfetch [-h] [-f] [-t ARG] [-l]"
echo
echo "options: -h Print this Help"
echo " -f force execution under heavy load"
echo " -t execute a standalone module with internal sample conf"
echo " -l list available modules files names"
}
unset option_force_load
# command options and arguments
while getopts "hft:l" option; do
case $option in
h) Help
exit ;;
f) option_force_load=1;;
t) # standalone module option
if [[ -f "$SCRIPT_DIR/motdfetch.d/$OPTARG" ]]; then
(. "$SCRIPT_DIR/motdfetch.d/$OPTARG")
exit 0
else
echo -e "\e[1;${c_danger}mError\e[0m: Invalid module filename, check the -l option.\n"
Help
exit 1
fi;;
l) # List modules option
for file in $SCRIPT_DIR/motdfetch.d/*
do
echo -e "$(basename $file) $(head -3 $file|tail -1)"
done | column -t -s '#'
exit 0;;
\?) # Invalid option
echo -e "\e[1;${c_danger}mError\e[0m: Invalid option"
Help
exit 1;;
esac
done
# CONFIG LOADING ###################################################
userconf="$HOME/.config/motdfetch/motdfetch.conf"
sysconf="/etc/motdfetch/motdfetch.conf"
if [[ -f "$userconf" ]];then
. "$userconf"
elif [[ -f "$sysconf" ]];then
. "$sysconf"
else
echo -e "\e[1;${c_danger}mError\e[0m: no configuration available.\n"
Help
exit 1
fi
# EXECUTION #########################################################
## load check
cores=$(/usr/bin/grep -ioPc 'processor\t:' /proc/cpuinfo 2>/dev/null)
if [[ "$cores" -eq "0" ]]; then
cores=1
fi
threshold="${cores:-1}.5"
echo -e "\n\e[${c_title} MOTDFETCH \e[0m\n"
if [[ $(echo "`cut -f1 -d ' ' /proc/loadavg` < $threshold" | /usr/bin/bc) -eq 1 || $option_force_load -eq 1 ]]; then
### output modules
for file in $SCRIPT_DIR/motdfetch.d/*
do
(. "$file")
done
echo
else
### too much load for modules, run only 00 & 99 + msg
module_header_disable=0
(. $SCRIPT_DIR/motdfetch.d/00-*)
module_userslog_disable=0
(. $SCRIPT_DIR/motdfetch.d/99-*)
echo -e "\n\e[${c_title} WE SKIPPED SOME MODULES \e[0m\n"
echo -e " MOTDfetch skipped some modules because of\n this \e[1;${c_danger}msystem load\e[0m being higher than \e[1;${c_danger}m$threshold\e[0;${c_warning}m.\e[0m\n"
echo -e " \e[1;${c_txt}mBAD ADVICE\e[0m: you can force execution with the option [-f]:\n # bash $SCRIPT_DIR/00-motdfetch -f\n"
fi