#!/usr/bin/perl

# Copyright (C) 2001 Mandrakesoft
# Author David Odin <odin@mandrakesoft.com>
#
# $Id: show_description.pl,v 1.6 2001/06/05 16:21:32 philippe Exp $
#

# show_description.pl
# version 0.1
# The purpose of this script is to retrieve as much informations as
# possible about the package given on the command line.

# Usage: show_description.pl [trans_name] package_name

# First get the transaction name (FIRST STEP IN ALL SCRIPTS)
my $transaction_name = shift @ARGV if (scalar(@ARGV)==2);

my $mandrake_release = "snf7.2";

$LANG = $ENV{LC_ALL} || $ENV{LANGUAGE} || $ENV{LC_MESSAGES} || $ENV{LANG} || 'us';

# if $test = 1, the current mirror is taken on the command-line
# if $test = 0, the current mirror is taken from the proxy variable config file
my $test = 0;

# First part, get the config variables...
my ($CurrentMirror, $package_name);

if ($test)
{
  $CurrentMirror = $ARGV[0];
  $package_name = $ARGV[1];
} else
{
  if (! $transaction_name ) {
	$transaction_name = "$$";
	# initialize a backend transaction
	system ("config-wrapper.pl $transaction_name -i");
  }

  # Get the CurrentMirror value
  $CurrentMirror = `config-wrapper.pl $transaction_name -g CurrentMirror`;
  chomp $CurrentMirror;
  $package_name = $ARGV[0];

  if ( $transaction_name eq "$$" ) {
      # cleanup the transaction
      system ("config-wrapper.pl $transaction_name -a");
  }

}

my ($installed_version, $installed_release, $group, $summary);
my ($installed_size, $version, $release, $fullname, $size);
my ($importance, $pre, $description, $post);

$installed_version = `rpm -q --queryformat "%{VERSION}" $package_name`;
$installed_release = `rpm -q --queryformat "%{RELEASE}" $package_name`;
$group = `rpm -q --queryformat "%{GROUP}" $package_name`;
$summary = `rpm -q --queryformat "%{SUMMARY}" $package_name`;
$installed_size = `rpm -q --queryformat "%{SIZE}" $package_name`;

## now get the list of updatable packages
open F, "curl -s $CurrentMirror/$mandrake_release/ls-lR |";
foreach (<F>)
{
  if (/^([^ ]+):$/)
  {
    $rep = "updates/$mandrake_release/$1";
  } elsif (/^(-|l)/)
  {
    $rep =~ /SRPMS/ and next;
    my @l = split;
    $_ = $l[8];
    m!(^|/)($package_name)-([^-]+)-([^-]+)\.[^.]+\.rpm$! or next;
    ($version, $release) = ($3, $4);
#    push @to_update, "$l[8]" if $l[8];
    $fullname=$l[8];
    $size = $l[4];
  }
}
close F or die;

## Then get the description from the description file
my (%e, $text, $garb, @l);
$importance  = "n/a";
$pre         = "n/a";
$description = "n/a";
$post        = "n/a";
open F, "curl -s $CurrentMirror/$mandrake_release/descriptions |";
foreach (<F>)
{
  if (/^%package\s+(.*)/i)
  {
    if ($1 eq $package_name) #?!? could someone explain why this works???
                             #imho, it should be if ($e{package} eq $package_name)...
    {
#      push @l, @e{qw(package noupgrade importance pre post description)};
      $importance  = $e{importance};
      $pre         = $e{pre};
      $description = $e{description};
      $post        = $e{post};
      %e = ();
    }
    
    $text = undef;
    $e{package} = $1;
  } elsif (/^%(\w+)(\s+-l\s+(.*))?/) # something with (-l lang) optional
  {
    $text = $3 eq $LANG || !$3 && !$e{$1} ? \$e{$1} : \$garb;
  } elsif ($text)
  {
    $$text .= $_;
  } elsif (/^flag:\s+(.*)/)
  {
    $e{noupgrade} = $1 =~ /install/i;
  } elsif (/^importance:\s+(.*)/)
  {
    $e{importance} = $1;
  }
}
#if ($e{package} eq $package_name)
#{
#  $importance  = $e{importance};
#  $pre         = $e{pre};
#  $description = $e{description};
#  $post        = $e{post};
#}


# encrypt the fields which have to be encrypted
$summary     = encrypt($summary);
$group       = encrypt($group);
$pre         = encrypt($pre);
$description = encrypt($description);
$post        = encrypt($post);

print "Package: $package_name%13";
print "Installed Version: $installed_version%13";
print "Installed Release: $installed_release%13";
print "Group: $group%13";
print "Summary: $summary%13";
print "Fullname: $fullname%13";
print "Version: $version%13";
print "Release: $release%13";
print "Size: $size%13";
print "Installed size: $installed_size%13";
print "Pre: $pre%13";
print "Description: $description%13";
print "Post: $post%13";
print "Importance: $importance%13";
print "\n";

#this sub replace many characters by their %XX counterparts
sub encrypt
{
  my $result = shift;
  $result =~ s/%/%25/g; # this one should be the first !!!
  $result =~ s/\t/%09/g;
  $result =~ s/\n/%13/g;
  $result =~ s/\r/%10/g;
  $result =~ s/\n/%13/g;
  $result =~ s/ /%20/g;
  $result =~ s/!/%21/g;
  $result =~ s/\"/%22/g;
  $result =~ s/\#/%23/g;
  $result =~ s/$/%24/g;
  $result =~ s/&/%26/g;
  $result =~ s/\'/%27/g;
  $result =~ s/\(/%28/g;
  $result =~ s/\)/%29/g;
  $result =~ s/\*/%2A/g;
  $result =~ s/\+/%2B/g;
  $result =~ s/,/%2C/g;
  $result =~ s/-/%2D/g;
  $result =~ s/\./%2E/g;
  $result =~ s!/!%2F!g;

  return $result;
}

 
