#!/bin/bash
#----------------------------------------------------------------------------------------------
# Script defines the time elapsed since last system upgrade
# Returns the result in an exclusive string format designed to be printed in a Conky Widget
# The script is designed to work on deb-kind linux with apt, that keeps apt logs at /var/log/apt
#
# (c) Sergey Murzin, 26 Sep 2016, s.murzin@adviser-media.com

# Messages
MSG_NEVER="System never updated"
MSG_JUST="Your system was recently updated"
MSG_START="" #"It has been "
MSG_END=" since your last update"

CONKY_COLOR="" # it will be placed by script with 3 following options
CONKY_RED="\${color3 #ff4343}"
CONKY_YELLOW="\${color4 #eaE11c}"
CONKY_GREEN="\${color2 #9fee62}"

BASH_COLOR=""
BASH_RED="\033[31m"
BASH_YELLOW="\033[33m"
BASH_GREEN="\033[32m"
BASH_DEFAULT="\033[0m"

# Get the date/time of the last update
LOGDIR="/var/log/apt/" # we need history.log file with the freshest record "Upgrade:"
LOGFILE="history.log"  # if not then we look at "history.log.[1,2,and so on].gz"

UPDATED_AT=`cat $LOGDIR$LOGFILE | grep -A 1 ^Upgrade: | grep ^End-Date: | sort -r` # many dates
UPDATED_AT=`echo ${UPDATED_AT:10:20}` # we need only 1-st one

# empty string means there are not any updated listed in the logfile, so we need to look at rotation files
if [ ${#UPDATED_AT} -eq 0 ]
then
  N="1"
  FILE_GZ=$LOGDIR$LOGFILE"."$N."gz"
  while [ ${#UPDATED_AT} -eq 0 ] && [ -f $FILE_GZ ]
  do
    UPDATED_AT=`gzip -cd $FILE_GZ | grep -A 1 ^Upgrade: | grep ^End-Date: | sort -r` # many dates
    UPDATED_AT=`echo ${UPDATED_AT:10:20}` # we need only 1-st one
    N=$((N + 1))
    FILE_GZ=$LOGDIR$LOGFILE"."$N."gz"
  done
fi
#UPDATED_AT="01-01-2017 16:00" # for debug
# empty string means there were not any updates
if [ ${#UPDATED_AT} -eq 0 ]
then
  MESSAGE=$MSG_NEVER
  CONKY_COLOR=$CONKY_RED ; BASH_COLOR=$BASH_RED
else
  UPDATED_AT=`date +%s -d " $UPDATED_AT "`  # in format of seconds since 01-01-1970
  NOW=`date +%s` # in format of seconds since 01-01-1970
  DIFF_SEC=$((NOW - UPDATED_AT))
  DIFF_HRS=$((DIFF_SEC / 3600))
  if [ $DIFF_HRS -lt 1 ]
  then # if less then 1 hour we do not write minutes, because the minutes would stay a while at Conky that would be wrong
    MESSAGE=$MSG_JUST
    CONKY_COLOR=$CONKY_GREEN ; BASH_COLOR=$BASH_GREEN
  else
    if [ $DIFF_HRS -lt 25 ]
    then
      if [ $DIFF_HRS -eq 1 ] ; then HRS=" hour" ; else HRS=" hrs" ; fi
      MESSAGE=$MSG_START$DIFF_HRS$HRS$MSG_END
      CONKY_COLOR=$CONKY_GREEN ; BASH_COLOR=$BASH_GREEN
    else
      DIFF_DAY=$((DIFF_HRS / 24))
      if [ $DIFF_DAY -eq 1 ] ; then DAY=" day" ; else DAY=" days" ; fi
      MESSAGE=$MSG_START$DIFF_DAY$DAY$MSG_END
      if [ $DIFF_DAY -lt 4 ]
      then
        CONKY_COLOR=$CONKY_GREEN ; BASH_COLOR=$BASH_GREEN
      else
        if [ $DIFF_DAY -lt 8 ]
        then
          CONKY_COLOR=$CONKY_YELLOW ; BASH_COLOR=$BASH_YELLOW
        else
          CONKY_COLOR=$CONKY_RED ; BASH_COLOR=$BASH_RED
        fi
      fi
    fi
  fi
fi

# with parameter -b script returns a string, formatted for bash, for debug purposes
if [ $# -eq 1 ] && [ $1 = "-b" ]
then
  echo -e $BASH_COLOR $MESSAGE $BASH_DEFAULT
else
  echo $CONKY_COLOR $MESSAGE
fi

exit
