#!/bin/bash

#
# Copyright (C) 2019 Nethesis S.r.l.
# http://www.nethesis.it - info@nethesis.it
#
# This script is part of Dante.
#
# Dante is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# Dante is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dante.  If not, see COPYING.
#

# Return last yum update


TMP_FILE=$(mktemp)
LOG="/var/log/yum.log"
DAY=$(date +"%b %d")
line=""

# Exit if yum log is empty
if [ -f $LOG ] && [ -s $LOG ]; then
    line=$(tail -n 1 $LOG)
else
    # find newest rotated log
    PREVIOUS_LOG=$(find $(dirname $LOG) -name yum.log-\* -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
    if [ -f "$PREVIOUS_LOG" ]; then
        line=$(tail -n 1 $PREVIOUS_LOG)  
    elif [ -f "$PREVIOUS_LOG.gz" ]; then
        line=$(zcat $PREVIOUS_LOG | tail -n 1)  
    fi
fi


value=$(echo $line | awk '{print $1 " " $2 " " $3}')

id=$(basename $0)
type=$(basename $0 | cut -d'-' -f2)

echo -n "{\"value\":\"$value\",\"type\":\"$type\",\"minerId\":\"$id\"}"
