#!/bin/sh
# This should be called from cron once per day to log uptime
# Log format: 'MM/DD/YY up XX days'

LOG=$HOME/text/uptimelog.txt
DAYS=0
uptime|grep -q days  &&  DAYS="$(uptime|sed 's/.*up \([0-9]*\) days.*/\1/')"
    
# Delete the last line from the log unless we've rebooted

if [ -s $LOG ]; then
    LAST="$(tail -n1 $LOG | sed 's/^.*up *\([0-9]*\) *days.*$/\1/')"
    if [ -n "$LAST" ]  &&  [[ $DAYS -ge $LAST ]]; then
        sed -i '$ d' $LOG
    fi
fi

# Log the current uptime

printf "$(date +%D)  up %2d days\n" $DAYS >> $LOG
