#!/usr/bin/ruby

#//------------------------------------------------
COPYRIGHT = <<"_COPYRIGHT_"
Copyright (C) 2000 Akira Higuchi <a@kondara.org>
Copyright (C) 2000 Motonobu Ichimura <famao@kondara.org>
Copyright (C) 2000 Tenkou N. Hattori <tnh@kondara.org>
Copyright (C) 2000 Kondara Project <http://www.kondara.org/>

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 for 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.
_COPYRIGHT_
#//------------------------------------------------

#//------------------------------------------------
require 'parsearg'

require 'mph'
require 'mph/traditional'

#//------------------------------------------------
def m_ShowUsage()
  print( "#{COPYRIGHT}\n" )
  print( "Usage:\n" )
  print( "\t#{$0} [-cq] local\n")
  print( "\t#{$0} [-cq] remote\n")
  print( "Options:\n" );
  print( "\t-n\tno execute\n" )
  print( "\t-c <file>\tread configurations from <file>\n" )
  print( "\t-q\tdon't update database\n" )
end

#//------------------------------------------------
$USAGE = 'm_ShowUsage()'
parseArgs( 0, nil, 'nvq', 'c:' )

if ARGV.size() == 0 then
  m_ShowUsage()
  exit( 0 )
end

sCommand = ARGV.shift()

asCommands = ['local', 'remote']
if asCommands.index( sCommand ) == nil then
  m_ShowUsage()
  exit( 0 )
end
if Process.uid() != 0 then
  $OPT_q = true
end

if $OPT_c == nil then
  $OPT_c = "/etc/mph.conf"
end

db = nil
msg = MPH::Msg.instance

if sCommand == 'local' then
  db = RPM::DB.new
else
  db = MPH::DB.new($OPT_c)
  ret = nil
  if Zlib then
    ret = db.setup_db([MPH::DBLoader,MPH::TraditionalDBLoader],$OPT_q,true,true,true)
  else
    ret = db.setup_db([MPH::TraditionalDBLoader],$OPT_q,true,true,true)
  end
  if !ret then
    msg.print(MPH::MsgLevel::ERROR,"Can't read mph database.\n")
    exit -1
  end
end

db.each do |p|
  p.requires.each do |r|
    found = false
    version_ok = false
    db.each_match(RPM::TAG_PROVIDENAME,r.name) do |lp|
      found = true
      if r.satisfy?(lp) then
        version_ok = true
      end
    end
    if !version_ok && found then
      msg.print(MPH::MsgLevel::NORMAL,"#{r}...\n")
      db.each_match(RPM::TAG_PROVIDENAME,r.name) do |lp|
        msg.print(MPH::MsgLevel::NORMAL,"  #{r.name} provides by #{lp} but version mismatch!\n")
      end
    end
    if !found then
      db.each_match(RPM::TAG_BASENAMES,r.name) do |lp|
        msg.print(MPH::MsgLevel::NORMAL,"#{r}...\n")
        msg.print(MPH::MsgLevel::NORMAL,"  #{r.name} is found in #{lp}'s file list\n")
        msg.print(MPH::MsgLevel::NORMAL,"  Please add Requires: #{lp.name} and remove Requires: #{r.name} in #{p.name}.spec\n")
        found = true
      end
    end
    if !found
      next if /rpmlib\(VersionedDependencies\)/ =~ r.to_s
      msg.print(MPH::MsgLevel::NORMAL,"#{r} but not found!.\n")
    end
  end
end

### :nodoc:
### Local Variables:
### mode: ruby
### ruby-indent-level: 2
### indent-tabs-mode: nil
### End:
