#!/usr/bin/perl

if (defined $ARGV[0]) {

$ARGV[0] =~ s/opt-get/http/;
chomp ($pkg_name = `basename $ARGV[0]`);

$/ = "\015";
$| = 1;
# setup progress bar
open UI, "| zenity --progress --title='remote-opt-get' --text='Downloading $pkg_name ...' --auto-close --auto-kill";
# setup download command
open WGET, "wget -c $ARGV[0] -P /opt 2>&1 |";
# download OPT package
while (<WGET>)  {
	 if ($_ =~ 'error') { 	
		 system("zenity --error --title='Download error' --text '$_'");
		 die;
		  };
	my ($progress) =
	$_ =~ /.* (.+?)%/si;
	$progress .= "\n";
	print UI `echo $progress`;
}

close WGET;
close UI;

# install OPT package
system("opt-get /opt/$pkg_name");

#system("zenity --info --text='$pkg_name has been installed'");
system("notify-send '$pkg_name has been installed' -i /usr/share/plate/chrome/content/image/kthememgr.png -u critical");
}
