#!/bin/bash

# Copyright (C) 2006-2007 Silvian Cretu <silvian86@yahoo.com>
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 

output='/var/log/uptimeRecord.log'
 
function compareHrs
{
        uptimeCurent=`uptime`
        if [ `echo $uptimeCurent | grep -c day` == '0' ]; then
#               echo Uptime mai mic de o zi
                uptimeCurentOre=`uptime | awk '{print $3}'`
                uptimeRecordOre=`cat $output | head -1 | awk '{print $3}'`
        else
                uptimeCurentOre=`uptime | awk '{print $5}'`
                uptimeRecordOre=`cat $output | head -1 | awk '{print $5}'`
        fi
        if [ `expr length $uptimeCurentOre` \> `expr length $uptimeRecordOre` ]; then
#               echo Nou record - Uptime curent mai mare cu cateva ore decat uptime-ul record 1
                uptime > $output
                date >> $output
        else
                if [ `expr length $uptimeCurentOre` == `expr length $uptimeRecordOre` ]; then
                        uptimeCurentOra=`echo $uptimeCurentOre | cut -d":" -f1`
                        uptimeRecordOra=`echo $uptimeRecordOre | cut -d":" -f1`
                        if [ $uptimeCurentOra -gt $uptimeRecordOra ]; then
#                               echo Nou record - Uptime curent mai mare cu cateva ore decat uptime-ul record 2
                                uptime > $output
                                date >> $output
                        fi
                fi
        fi
}
 
if [ -a $output ]; then
        uptimeCurent=`uptime`
        uptimeRecord=`cat $output | head -1`
        if [ `echo $uptimeCurent | grep -c day` == '0' ]; then
#               echo Uptime mai mic de o zi
                if [ `echo $uptimeRecord | grep -c day` == '0' ]; then
#                       echo Uptime record mai mic de o zi, comparam orele
                        compareHrs
#               else
#                       echo Uptime record mai mare ca uptime curent
                fi
        else
#               echo Uptime mai mare de o zi
                if [ `echo $uptimeRecord | grep -c day` == '0' ]; then
#                       echo Uptime record mai mic de o zi
                        uptime > $output
                        date >> $output
                else
                        uptimeCurentZile=`uptime | awk '{print $3}'`
                        uptimeRecordZile=`cat $output | head -1 | awk '{print $3}'`
                        if [ $uptimeCurentZile -eq $uptimeRecordZile ]; then
#                               echo Uptime in zile egal
                                compareHrs
                        else
                                if [ $uptimeCurentZile -gt $uptimeRecordZile ]; then
#                                       echo Record nou
                                        uptime > $output
                                        date >> $output
                                fi
                        fi
                fi
        fi
else
#       echo Fisierul nu exista, deci trebuie creat. Recordul e uptime-ul curent
        uptime > $output
        date >> $output
fi
 
exit 0

