#!/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 'optparse'
require 'mph'
require 'mph/traditional'

#//------------------------------------------------
opt = {
  :c => '/etc/mph.conf'
}
ARGV.options {|o|
  Version = '0.92.8'
  o.banner = "#{COPYRIGHT}\n"
  o.banner << "Usage:\n"
  o.banner << "\t#{$0} [-cq] local\n"
  o.banner << "\t#{$0} [-cq] remote\n"
  o.banner << "Options:\n"
  o.on('-q', 'don\'t update database') {|v| opt[:q] = true }
  o.on('-c <file>', 'read configurations from <file>') {|v| opt[:c] = v}
  o.parse!
}

if ARGV.size() == 0 then
  puts ARGV.options.help
  exit( 0 )
end

sCommand = ARGV.shift()

asCommands = ['local', 'remote']
if asCommands.index( sCommand ) == nil then
  puts ARGV.options.help
  exit( 0 )
end
if Process.uid() != 0 then
  opt[:q] = true
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:
