#!/bin/bash
#
#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


list=`cat /var/log/ppp-uptime-log  | sort -rgt "|" -k 2`
current=`ls -l --time-style=+%s /var/run/pppoe.conf-adsl.pid.pppd | awk '{ print $6 }'`
now=`date +%s`
i=1
current_found=0
echo "   #          link-uptime |                   dialin at"
echo "--------------------------+----------------------------"
for element in $list 
do
    connect_date=`echo $element | tr "\|" " " | awk '{ print $1 }'`
    if [ $connect_date ]; then
        if [ -z $first ]; then
            if [ $current = $connect_date ]; then
                first="imno1"
            else
                first=$element
            fi
        fi
        if [ $current = $connect_date ]; then
            secs=`calc $now - $connect_date`
        else
            secs=`echo $element | tr "\|" " " | awk '{ print $2 }'`
        fi
        days=`expr $secs / 86400`
        secs=`calc $secs % 86400`
        hours=`expr $secs / 3600`
        if [ `echo -n $hours | wc -c` -eq "1" ]; then
            hours="0$hours"
        fi
        secs=`calc $secs % 3600`
        minutes=`expr $secs / 60`
        if [ `echo -n $minutes | wc -c` -eq "1" ]; then
            minutes="0$minutes"
        fi
        secs=`calc $secs % 60 | awk '{ print $1 }'`
        #echo -n $secs | wc -c
        #echo -n $secs
        if [ `echo -n $secs | wc -c` -eq "1" ]; then
            secs="0$secs"
        fi
        if [ `echo -n $days | wc -c` -eq "1" ]; then
            days=" $days"
        fi
        num=$i
        while [ `echo -n $num | wc -c` -lt "3" ]; do
            num="0$num"
        done
        if [ $current = $connect_date ]; then
            im_num=$num
        fi
        if [ $i -lt 21 ]; then
            if [ $current = $connect_date ]; then
                echo -ne "\033[1m"
                current_found=1
            fi
            echo -n " $num    $days days, $hours:$minutes:$secs |    `echo $connect_date | awk '{ printf(\"%s\",strftime(\"%a %d %b %Y %H:%M:%S\",$1)); }'`"
            if [ $current = $connect_date ]; then
                echo -ne "\033[0m"
            fi
            echo ""
        else
            break;
        fi
        declare -i i=$i+1
    fi
done
echo "--------------------------+----------------------------"
if [ $current_found -eq 0 ]; then
    secs=`calc $now - $current`
    days=`expr $secs / 86400`
    secs=`calc $secs % 86400`
    hours=`expr $secs / 3600`
    if [ `echo -n $hours | wc -c` -eq "1" ]; then
        hours="0$hours"
    fi
    secs=`calc $secs % 3600`
    minutes=`expr $secs / 60`
    if [ `echo -n $minutes | wc -c` -eq "1" ]; then
        minutes="0$minutes"
    fi
    secs=`calc $secs % 60 | awk '{ print $1 }'`
    if [ `echo -n $secs | wc -c` -eq "1" ]; then
        secs="0$secs"
    fi
    if [ `echo -n $days | wc -c` -eq "1" ]; then
        days=" $days"
    fi
    echo -ne "\033[1m"
    echo " $im_num    $days days, $hours:$minutes:$secs |    `echo $current | awk '{ printf(\"%s\",strftime(\"%a %d %b %Y %H:%M:%S\",$1)); }'`"
    echo -ne "\033[0m"
fi
if [ $first != "imno1" ]; then
    secs=`calc $now - $current`
    no1secs=`echo $first | tr "\|" " " | awk '{ print $2 }'`
    tono1=`calc $no1secs - $secs`
    no1date=`calc $tono1 + $now`
    days=`expr $tono1 / 86400`
    tono1=`calc $tono1 % 86400`
    hours=`expr $tono1 / 3600`
    if [ `echo -n $hours | wc -c` -eq "1" ]; then
        hours="0$hours"
    fi
    tono1=`calc $tono1 % 3600`
    minutes=`expr $tono1 / 60`
    if [ `echo -n $minutes | wc -c` -eq "1" ]; then
        minutes="0$minutes"
    fi
    tono1=`calc $tono1 % 60 | awk '{ print $1 }'`
    if [ `echo -n $tono1 | wc -c` -eq "1" ]; then
        tono1="0$tono1"
    fi
    if [ `echo -n $days | wc -c` -eq "1" ]; then
        days=" $days"
    fi
    
    echo " no1 in $days days, $hours:$minutes:$tono1 | at `echo $no1date | awk '{ printf(\"%s\",strftime(\"%a %d %b %Y %H:%M:%S\",$1)); }'`"
fi
