Finished rewriting enabling and disabling commands

master
Mateusz Chochół 7 years ago
parent 875030f5a4
commit 1d061356cc
  1. 40
      bmotd.sh

@ -6,7 +6,7 @@
# Config # Config
MOTD="/etc/motd2" MOTD="/etc/motd2"
INSTPATH="/etc/bmotd.sh" INSTPATH="/etc/bmotd.sh"
CACHEPATH="/var/cache/bmotd/" CACHEPATH="/var/cache/bmotd"
# Check if root # Check if root
@ -17,10 +17,9 @@ main () {
fi fi
# check if root # check if root
isRoot if ! isRoot; then
if [[ $? -ne 0 ]]; then
echo 'Script needs root privileges to alter motd. Re-run with sudo.' echo 'Script needs root privileges to alter motd. Re-run with sudo.'
exit 0 exit 1
fi fi
# check arguments # check arguments
@ -32,13 +31,19 @@ main () {
cp "$(readlink -f $0)" "$INSTPATH" cp "$(readlink -f $0)" "$INSTPATH"
# check if crontab present # check if crontab present
command -v crontab >/dev/null 2>&1 || ( echo "Error: couldn't find crontab. Exiting..."; exit 1) if ! command -v crontab >/dev/null 2>&1; then
echo "Error: couldn't find crontab. Exiting..."
exit 1
fi
# Check if motd writable # Check if motd writable
test -w $MOTD || ( echo "Unable to write $MOTD . To enable update permissions. Exiting..."; exit 1 ) if ! test -w $MOTD; then
echo "Unable to write $MOTD . To enable update permissions. Exiting..."
exit 1
fi
# Check if script is already in crontab, if not add itself # Check if script is already in crontab, if not add itself
CRON_ENTRY="* * * * * $INSTPATH" CRON_ENTRY="* * * * * $INSTPATH update"
crontab -l 2>/dev/null | grep -q "$CRON_ENTRY" || ( crontab -l 2>/dev/null; echo "$CRON_ENTRY" ) | crontab - crontab -l 2>/dev/null | grep -q "$CRON_ENTRY" || ( crontab -l 2>/dev/null; echo "$CRON_ENTRY" ) | crontab -
echo -e "Success! Check it yourself with crontab -l" echo -e "Success! Check it yourself with crontab -l"
@ -47,12 +52,21 @@ main () {
exit 0 exit 0
;; ;;
disable) disable)
echo -e "\nDisabling PISS" echo -e "\nDisabling BetterMOTD"
( crontab -l 2>/dev/null | egrep -v '.*piss\.sh$'; ) | crontab - ( crontab -l 2>/dev/null | egrep -v "$INSTPATH update$"; ) | crontab -
rm $INSTPATH
exit 0
;;
update)
update
exit 0 exit 0
;; ;;
*)
printHelp
esac esac
}
update() {
# Gather information # Gather information
HOSTNAME=$(hostname -s) HOSTNAME=$(hostname -s)
FQDN=$(hostname -f) FQDN=$(hostname -f)
@ -61,7 +75,7 @@ main () {
EXTERNAL=$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"') EXTERNAL=$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"')
# Get number of updates awaiting # Get number of updates awaiting
test -f ~/.pisscache || PKG_REFRESH=1 test -f "$CACHEPATH/pkg_count" || PKG_REFRESH=1
[[ $(date +%H%M) == "1510" ]] && PKG_REFRESH=1 [[ $(date +%H%M) == "1510" ]] && PKG_REFRESH=1
if [ $PKG_REFRESH ]; then if [ $PKG_REFRESH ]; then
case "$OSID" in case "$OSID" in
@ -74,13 +88,13 @@ main () {
*) *)
PKG_NU="-1" PKG_NU="-1"
esac esac
echo $PKG_NU > ~/.pisscache mkdir -p "$CACHEPATH"
echo $PKG_NU > "$CACHEPATH/pkg_count"
fi fi
PKG_NU=$(cat ~/.pisscache) PKG_NU=$(cat $CACHEPATH/pkg_count)
# Test if /etc/motd writable and run motd generator if so # Test if /etc/motd writable and run motd generator if so
test -w $MOTD && motdGen test -w $MOTD && motdGen
} }
function printHelp { function printHelp {

Loading…
Cancel
Save