#!/usr/bin/perl -w
#
# Rubi's Ultimate Backup Implementation
#
# Version 4.0, (C) 3.1999 by Beat Rubischon <beat@rubis.ch>
#                  8.1999 by Beat Rubischon <beat.rubischon@trivadis.com>
#                  2.2000 by Beat Rubischon <beat@rubis.ch>
#                  2.2004 by Beat Rubischon <rubischon@phys.ethz.ch>
#                  7.2007 by Beat Rubischon <beat@0x1b.ch>
#                 10.2010 by Beat Rubischon <beat.rubischon@dalco.ch>
#                 12.2015 by Beat Rubischon <beat.rubischon@dalco.ch>
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: report.pl,v 1.1 2015/12/01 16:21:22 beat Exp $
#
# Nice statistics

use strict;

my $ftcount=0; my $ftbytes=0;
my $fccount=0; my $fcbytes=0;

while(<>) {
  if (/Number of files: (\d+)/) {
    $ftcount+=$1;
  } elsif (/Number of files transferred: (\d+)/) {
    $fccount+=$1;
  } elsif (/Total file size: (\d+) bytes/) {
    $ftbytes+=$1;
  } elsif (/Total transferred file size: (\d+) bytes/) {
    $fcbytes+=$1;
  }
}

my $flbytes=$ftbytes-$fcbytes;
my $flcount=$ftcount-$fccount;

my $fcval="B";
if ($fcbytes >= 1024) {
    $fcbytes=$fcbytes / 1024;
    $fcval="K";
    if ($fcbytes >= 1024) {
        $fcbytes=$fcbytes / 1024;
        $fcval="M";
        if ($fcbytes >= 1024) {
            $fcbytes=$fcbytes / 1024;
            $fcval="G";
            if ($fcbytes >= 1024) {
                $fcbytes=$fcbytes / 1024;
                $fcval="T";
                if ($fcbytes >= 1024) {
                    $fcbytes=$fcbytes / 1024;
                    $fcval="P";
                }
            }
        }
    }
}

my $flval="B";
if ($flbytes >= 1024) {
    $flbytes=$flbytes / 1024;
    $flval="K";
    if ($flbytes >= 1024) {
        $flbytes=$flbytes / 1024;
        $flval="M";
        if ($flbytes >= 1024) {
            $flbytes=$flbytes / 1024;
            $flval="G";
            if ($flbytes >= 1024) {
                $flbytes=$flbytes / 1024;
                $flval="T";
                if ($flbytes >= 1024) {
                    $flbytes=$flbytes / 1024;
                    $flval="P";
                }
            }
        }
    }
}

my $ftval="B";
if ($ftbytes >= 1024) {
    $ftbytes=$ftbytes / 1024;
    $ftval="K";
    if ($ftbytes >= 1024) {
        $ftbytes=$ftbytes / 1024;
        $ftval="M";
        if ($ftbytes >= 1024) {
            $ftbytes=$ftbytes / 1024;
            $ftval="G";
            if ($ftbytes >= 1024) {
                $ftbytes=$ftbytes / 1024;
                $ftval="T";
                if ($ftbytes >= 1024) {
                    $ftbytes=$ftbytes / 1024;
                    $ftval="P";
                }
            }
        }
    }
}

$fccount=~s/(\d)(?=(\d{3})+(\D|$))/$1\'/g;
$flcount=~s/(\d)(?=(\d{3})+(\D|$))/$1\'/g;
$ftcount=~s/(\d)(?=(\d{3})+(\D|$))/$1\'/g;

print "Type               Count     Size\n";
print "----------------------------------------------------------------------------\n";
printf "%-8s %15s %6.1f %s\n", "copied:", $fccount, $fcbytes, $fcval;
printf "%-8s %15s %6.1f %s\n", "linked:", $flcount, $flbytes, $flval;
printf "%-8s %15s %6.1f %s\n", "total:", $ftcount, $ftbytes, $ftval;
print "----------------------------------------------------------------------------\n";
