#!/bin/bash

. /etc/update-motd.d/colors

if [[ -x /usr/sbin/checkrestart ]]; then
    n=$(/usr/sbin/checkrestart | grep "Found .* processes using old versions of upgraded files")
    re='Found (.*) processes using old versions of upgraded files'
    if [[ $n =~ $re ]]; then
        if [[ ${BASH_REMATCH[1]} -gt 0 ]]; then
            printf $LIGHT_RED
            printf "  %s services need to be restarted.\n" "${BASH_REMATCH[1]}"
            printf $NONE
            printf "  Use needrestart to list and restart them.\n\n"
        fi
    fi
elif [[ -x /usr/sbin/needrestart ]]; then
    n=$(/usr/sbin/needrestart -l -v -n -r l 2>/dev/null | grep -c "Skipping")
    if [[ $n -gt 0 ]]; then
        printf $LIGHT_RED
        printf "  %s services need to be restarted.\n" $n
        printf $NONE
        printf "  Use needrestart to list and restart them.\n\n"
    fi
fi
