#!/usr/bin/perl -w

use strict;
use warnings;

use Getopt::Long;

my $opts;
$opts->{no_header}  = 0;
$opts->{delay}      = 2;
$opts->{frames}     = "-/|\\";

GetOptions (
    "no-header" => \$opts->{no_header},
    "usage"     => \$opts->{usage},
    "help"      => \$opts->{usage},
    "delay=i"   => \$opts->{delay},
    "frames=s"  => \$opts->{frames},
) or &usage;

&usage if $opts->{usage};

sub usage {
    print "usage: watchdump.pl --no-header --delay=SEC --frames=CHARS FILE\n";
    exit 0;
}

my @frames      = split "", $opts->{frames};
my $frame_num   = 0;

sub get_frame {
    my $frame = $frames[$frame_num];
    $frame_num++;
    $frame_num = 0 if $frame_num > $#frames;
    return $frame;
}

sub clear {
    system "cls"    if -d "c:\\windows";
    system "clear"  if -f "/bin/ls";
}

my $file = shift @ARGV or &usage;

while (1) {
    &clear;
    system "title $file" if -d "c:\\windows";
    printf "[%s] watching: %s\n%s\n", &get_frame(), $file, "=" x 80
        unless $opts->{no_header};
    if (open my $fh, "<", $file) {
        print join "", <$fh>;
        close $file;
    } else { warn "cannot open $file: $!\n"; next; }
} continue {
    sleep $opts->{delay};
}