#!/usr/bin/perl

# XAPI-APT 1.3
# By Alfonso E.M. alfonso@el-magnifico.org
# Instalador de paquetes deb por apt

use UI::Dialog;

$dialog = new UI::Dialog (order => ['zenity','xdialog'] );

# (Usamos un antiguo xdialog por problemas con UTF-8 en zenity)
$xdialog = new UI::Dialog (order => ['xdialog','zenity'] );


$|=1;
$file=$ARGV[0];
$file=~s/\.apt//;
@parts=split(/\//,$file);
$file=$parts[$#parts];
$log="/tmp/instalacion_".$file.".log";

$info=&get_apt_data($file);

$info.="\n\n Desea continuar ?";

if ($dialog->yesno( 
	title => 'Instalacin de Programas en Guadalinex',
	backtitle => $file,
	height => 30, width=>85,
	text => "$info") ) { 
		if ($< != 0) {
			system("gksu -m 'Para instalar programas necesita la clave de supervisor (root)' 'apt-get --yes install $file > $log'  ");		
		} else {
			system("apt-get --yes install $file > $log");		
		}
		if (-e $log) { 
	                $xdialog->textbox( 
				title => 'Resultado de la instalacion',
				height => 20, width=>100,
				path => $log 
				);
			unlink $log;
		}
	}

sub get_apt_data() {
	my ($package)=@_;
	$description="";
	open(DEB,"dpkg -p $package |") or return ("");
	while (<DEB>) {
		chop;
		if (/Package: (.*)/) {$name=$1; }
		if (/Version: (.*)/) {$version=$1; }
		if (/Description: (.*)/) {$description="$1\n"; }
		if (/Installed-Size: (.*)/) {$isize=$1; }
		if (/Maintainer: (.*) <.*>/) {$maintainer=$1; }
		if (/^ (.*)/ && $description ne "") { $description.=$1; }
	}
	close DEB;
$info="
Va a instalar \"$name\" (versin $version) mantenido por \"$maintainer\".

Ocupar $isize Kbytes.

Esta es la descripcin incluida en el paquete:

$description
";
	return $info;
}
