#!/usr/bin/perl -w
#----------------------------------------------------------------------
# Yum database updates
# Copyright (C) 2005 Gordon Rowell <gordonr@gormand.com.au>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License or more details.
#
# You should have received a copy of the GNU 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
#----------------------------------------------------------------------

use strict;
use esmith::ConfigDB;

my $db = esmith::ConfigDB->open_ro or die "Couldn't open ConfigDB\n";

my $pid = fork;
exit 1  unless defined $pid;
# now two processes are executing
if ($pid == 0) {

# XXX - WARNING - XXX
#
# For speed and to reduce log noise, we don't use 
# the esmith::DB routines here

# We do not use -R xxx to sleep for a while, as we do this at cron level
# this allows faster results when needed or delayed when not
 
use constant DNF_CMD => "/usr/bin/dnf -d 0 -e 0 ";

# dnf check to have fresh metadata
system(DNF_CMD." check-update 1>/dev/null");

my $tmp_file;

END { $tmp_file and -f $tmp_file and unlink $tmp_file; }

for my $list_option ( qw(available installed updates) )
{
    my $file = "/home/e-smith/db/dnf_${list_option}";
    $tmp_file = "/home/e-smith/db/dnf_${list_option}.tmp";

    open DB, ">$tmp_file" or die "Couldn't create $tmp_file\n";

    print DB "# Generated by $0: " . scalar localtime() . "\n";

    open YUM, "-|", DNF_CMD . "list $list_option" or 
	die "Couldn't call dnf list $list_option\n";

    #strip header, preventing inclusion as package
    my $header = <YUM>;
    while ( <YUM> )
    {
	my ($package, $version, $repo) = split /\s+/;

	next unless $package and $version and $repo;

        if ($list_option eq 'available')
        {
          my $RestrictRpm = $db->get_prop('dnf', 'RestrictRpm') || '';
          my $RestrictRepo = $db->get_prop('dnf', 'RestrictRepo') || '';
          my @rpms = split(/,/, $RestrictRpm);
          my @repos = split(/,/, $RestrictRepo);

          if ( ($#rpms < 0 && $#repos < 0)
               || (grep { $package =~ /$_/ } @rpms)
               || (grep { $repo =~ /$_/ } @repos)
             )
          {
            print DB "$package=package|Repo|$repo|Version|$version\n";
          }
        }
        else
        {
            print DB "$package=package|Repo|$repo|Version|$version\n";
        }
    }

    close YUM or warn "dnf list $list_option status $?\n";

    open YUM, "-|", DNF_CMD . "grouplist $list_option" or
        die "Couldn't call dnf grouplist $list_option\n";

    while ( <YUM> )
    {
	next if /^\s*$/;

	next unless /^\s+/;
	
	s/\s+//g;

	print DB "$_=group\n";
    }

    close YUM or warn "dnf grouplist $list_option status $?\n";

    close DB;

    rename $tmp_file, $file or die "Couldn't rename $tmp_file $file\n";
}

exit 0;
}
exit 0;
