#!/usr/bin/perl -w
if (!$ARGV[0]) { die "usage: skipdownload file\n" }
eval 'use Gtk; init Gtk;' or &console();


use strict;

my $command;
my $value;
my $signal;
my $true = 1;
my $false = 0;
my $window;
my $button;
my $vbox;
my $label;
my $adj;
my $pbar;

$window = new Gtk::Window( 'toplevel' );
$signal = $window->signal_connect( 'delete_event', sub { Gtk->exit( 0 ); } );
$window->border_width( 15 );

$vbox = new Gtk::VBox( $false, 0 );
$window->add( $vbox );

$label = new Gtk::Label( "Downloading $ARGV[0]" );
$vbox->pack_start( $label, $false, $false, 10 );
$label->show();

$adj = new Gtk::Adjustment( 0, 1, 100, 0, 0, 0 );
$pbar = new_with_adjustment Gtk::ProgressBar( $adj );
$vbox->pack_start( $pbar, $false, $false, 10 );
$pbar->set_format_string( "%p%%" );
$pbar->set_show_text( 1 );
$pbar->set_value( 0 );
$pbar->show();
$vbox->show();

Gtk->main_iteration while ( Gtk->events_pending );

$window->show();

$command = "wget --dot-style=micro";

open TMP, "$command \"$ARGV[0]\" 2>&1 |";

while ( $value = <TMP> )
  {
    $value =~ s/^.*\[//g;
    $value =~ s/ //g;
    $value =~ s/\%\]//g;
    chomp $value;

    $pbar->set_value( ++$value ) if ( $value =~ /^[0-9]+$/ );

    Gtk->main_iteration while ( Gtk->events_pending );
  }
close TMP;

$vbox->remove( $pbar );

$label->set_text( "Download Complete" );

$button = new Gtk::Button( "Close" );
$button->signal_connect( 'clicked', sub { Gtk->exit( 0 ); } );
$vbox->pack_start( $button, $false, $false, 0 );
$button->show();

main Gtk;
exit( 0 );


sub console
{
	$command = "wget";
	system("xterm -e $command \"$ARGV[0]\"");
	exit(0);
}
