#!/bin/bash

#test to see if started by mx-updater.py,
#if it was resize terminal window to 67% of screenwidth & screenheight,
#and reposition to the center of screen
#sleep 1
#ps axjf | grep ^[[:space:]]*$$ -B7 -E | grep mx-updater.py$ -qo
#if [ "$?" = "0" ]
#  then
#    IFS='x' read screenWidth screenHeight < <(xdpyinfo | grep dimensions | grep -o '[0-9x]*' | head -n1)
#    xdotool getactivewindow windowsize --usehints 67% 67%
#    read width height < <(xdotool getactivewindow getwindowgeometry --shell | head -5 | tail -2 | cut -f2 -d= | sed ':a;N;$!ba;s/\n/ /g')
#    newPosX=$(((screenWidth-width)/2))
#    newPosY=$(((screenHeight-height)/2))
#    xdotool getactivewindow windowmove "$newPosX" "$newPosY"
#fi

ShowUnattendedUpgradesDpkgLogs()
{
 ls -1tr /var/log/unattended-upgrades/unattended-upgrades-dpkg.log* |\
 xargs zcat -fq                                                     |\
 sed 's/%\x0D/%\n/g'                                                |\
 grep -v %$
}

#set strings using translations from the apt-notfier.py python script
#define a wrapper function so xgettext will ignore the strings here
mygettext() { gettext "$@"; }
 LessPrompt=$(mygettext -d mx-updater "Press 'h' for help, 'q' to quit")
NoLogsFound=$(mygettext -d mx-updater "No logs found.")


if [ -f /var/log/unattended-upgrades/unattended-upgrades-dpkg.log ]
  then 
    StartOfLatestDpkgLogEntries="$(ShowUnattendedUpgradesDpkgLogs | grep ^\( -B1 | grep -v -e^\( -e\-\- | grep [:][0-5][0-9][:] | cut -f1-2 -d: | rev | cut -c5- | rev | sort -u | tail -1)"
    StartOfLatestDpkgLogEntries="$(ShowUnattendedUpgradesDpkgLogs | grep "$StartOfLatestDpkgLogEntries" -m1)"
    ShowUnattendedUpgradesDpkgLogs | less -~ -R --prompt="--less--[$LessPrompt]" -p "$StartOfLatestDpkgLogEntries"
  else
    echo -e \\n"${NoLogsFound}"\\n | less -~ -R --prompt="--less--[$LessPrompt]"
fi
  
exit
