#!/usr/bin/perl
#
# Tux v1.0 (c) Patrick St. Jean 1997 (stjeanp@pdq.net)
# Use is permitted under the terms of the GPL.  enjoy!

use strict;

my $DATAFILE;
my $REBOOTS = 0;
my $REBOOTFLAG;
my $LINE;
my $SECONDS;
my $TIME;
my $PENGUINS;
my $PREFIX;

$DATAFILE = "/etc/tux.data";

				# is this being called to update the number
				# of reboots?

if(defined($ARGV[0])){
    if($ARGV[0] =~ /-r/io){
	$REBOOTFLAG = 1;
    }else{
	print(STDERR "Usage: tux [-r]\n");
	print(STDERR "       -r: increment reboot count\n");
	exit 0;
    }
}

				# if we don't have a data file, make one.
				# it holds number of reboots...

if(! -f $DATAFILE){
    open(DATA, ">$DATAFILE") || die "Couldn't open $DATAFILE for writing!\n";
    print(DATA "1");
    close(DATA);
    if($REBOOTFLAG){
	exit 0;
    }
}

				# get the number of reboots

open(DATA, "<$DATAFILE") || die "Couldn't open $DATAFILE for reading!\n";
$REBOOTS = <DATA>;
close(DATA);

				# write out the new number of reboots and
				# exit

if($REBOOTFLAG){
    $REBOOTS++;
    open(DATA, ">$DATAFILE") || die "Couldn't open $DATAFILE for writing!\n";
    print(DATA "$REBOOTS");
    close(DATA);
    exit 0;
}
				# check to see that /proc is mounted

if(! -d "/proc"){
    print(STDERR "/proc must be mounted!!!\n");
    exit 1;
}

				# get our uptime.  First of 2 fields

open(UPTIME, "</proc/uptime") || die "Couldn't open /proc/uptime for reading!\n";
$LINE = <UPTIME>;
close(UPTIME);

$SECONDS = (split(" ", $LINE, 2))[0];
$TIME = $SECONDS / 86400;
$PENGUINS = $REBOOTS / $TIME;

if($PENGUINS ge 1){
    $PREFIX = "";
}elsif($PENGUINS ge .1){
    $PREFIX = "deci";
    $PENGUINS *= 10;
}elsif($PENGUINS ge .01){
    $PREFIX = "centi";
    $PENGUINS *= 100;
}elsif($PENGUINS ge .001){
    $PREFIX = "milli";
    $PENGUINS *= 1000;
}elsif($PENGUINS ge .000001){
    $PREFIX = "micro";
    $PENGUINS *= 1000000;
}elsif($PENGUINS ge .000000001){
    $PREFIX = "nano";
    $PENGUINS *= 1000000000;
}elsif($PENGUINS ge .000000000001){
    $PREFIX = "pico";
    $PENGUINS *= 1000000000000;
}

print(STDOUT "$PENGUINS ${PREFIX}penguins\n");
exit 0;
