#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Package lister. 
# Designed to be architecture- and distribution independent.
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Richard Bos <allabos@freeler.nl>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

# --- Common stuff --- #


BEGIN {
  $SCRIPTSDIR = "/usr/share/setup-tool-backends/scripts";
  if ($SCRIPTSDIR =~ /^@scriptsdir[@]/)
  {
      $SCRIPTSDIR = ".";
      $DOTIN = ".in";
  }
  
# require "$SCRIPTSDIR/general.pl$DOTIN";
# require "$SCRIPTSDIR/platform.pl$DOTIN";
# require "$SCRIPTSDIR/util.pl$DOTIN";
# require "$SCRIPTSDIR/file.pl$DOTIN";
  require "$SCRIPTSDIR/xml.pl$DOTIN";
}


# --- Tool information --- #

$name = "package";
$version = "1.4.2";
@platforms = ("redhat-5.2", "redhat-6.0", "redhat-6.1", "redhat-6.2", "redhat-7.0",
              "mandrake-7.2", "suse-7.0", "turbolinux-7.0");

$description =<<"end_of_description;";
List available packages at the system only.  Use the available package update
tooks like; rpm, apt-get, red-carpet, etc to update your system.
end_of_description;

# --- System config file locations --- #

# Where are the tools?

$cmd_pack = &gst_file_locate_tool ("rpm");

# --- Internal configuration variables --- #

# Configuration is parsed/read to, and printed/written from, these temporary variables.

@cf_package = ();


# --- XML scanning --- #

# --- XML printing --- #

sub xml_print
{
  &gst_xml_print_line ("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
  &gst_xml_print_line ("<!DOCTYPE packages []>\n");
  &gst_xml_print_line ("<packages>");
  &gst_xml_enter ();

  &gst_xml_print_vspace ();
  &gst_xml_print_line ("<!-- Configuration starts here -->\n");

  &gst_xml_print_vspace ();
  &gst_xml_print_line ("<meta>");
  &gst_xml_enter;

  my $number_of_packages = $#cf_package + 1;
  &gst_xml_print_line ("<number>$number_of_packages</number>");
  &gst_xml_leave;
  &gst_xml_print_line ("</meta>");

  &gst_xml_print_vspace ();
  &gst_xml_print_line ("</packagedb>");
  &gst_xml_print_vspace ();
  &gst_xml_enter;

  foreach $entry (@cf_package) {
    &gst_xml_print_line ("<package>");
    &gst_xml_enter;
    &gst_xml_print_line ("<name>$entry->{name}</name>");
    &gst_xml_print_line ("<version>$entry->{vers}</version>");
    &gst_xml_print_line ("<distribution>$entry->{dist}</distribution>");
    &gst_xml_leave;
    &gst_xml_print_line ("</package>");
  &gst_xml_print_vspace ();
  }

  &gst_xml_print_vspace ();
  &gst_xml_leave ();
  &gst_xml_print_line ("</packagedb>");

  &gst_xml_print_vspace ();
  &gst_xml_print_line ("<!-- End of configuration -->\n");

  &gst_xml_print_vspace ();
  &gst_xml_leave ();
  &gst_xml_print_line ("</packages>");
}


# --- Get (read) config --- #

sub get_package
{
  my $dist;
  my $distributor;
  my $name;
  my $pid;
  my $vers;
  my $fd;

  my %cmd_map =
  (
    "rpm" => "rpm -qa --queryformat '%{name},%{version},%{release}\n'",
    "deb" => "unknown"
  );

  my %dist_map =
  (
    "redhat"   => "$cmd_map{rpm}",
    "openna"   => "$cmd_map{rpm}",
    "mandrake" => "$cmd_map{rpm}",
    "suse"     => "$cmd_map{rpm}",
    "debian"   => "$cmd_map{deb}",
    "turbolinux" => "$cmd_map{rpm}",
  );

  ($distributor) = split /-/, $gst_dist;

  $fd = &gst_file_run_pipe_read ("$dist_map{$distributor}");
  die "Could not execute the package query command" if $fd eq undef;

  while (<$fd>)
  {
    chomp $_;
    ($name, $vers, $dist) = split /,/, $_;

    push @cf_package, {
      "name"     => $name,
      "vers"     => $vers,
      "dist"     => $dist,
    };
  }
  &gst_file_close ($fd);
}

sub get
{
  &gst_report ("get_packages");

  &get_package; &gst_print_progress();
  
  &gst_report_end ();
  &xml_print ();
}


# --- Set (write) config --- #

# --- Main --- #

# get, set and filter are special cases that don't need more parameters than a ref to their function.
# Read general.pl.in:gst_run_directive to know about the format of this hash.

$directives = {
  "get"    => [ \&get,    [], "" ],
  "set"    => [ \&set,    [], "" ],
  "filter" => [ \&filter, [], "" ]
    };

$tool = &gst_init ($name, $version, $description, $directives, @ARGV);
&gst_platform_ensure_supported ($tool, @platforms);
&gst_run ($tool);
