#!/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} [-vcq] noinstall\n"
  o.banner << "\t#{$0} [-vcq] noexist\n"
  o.banner << "\t#{$0} [-vcq] whatprovides [item...]\n"
  o.banner << "Options:\n"
  o.on('-v', 'verbose mode') {|v| opt[:v] = true }
  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()

case sCommand
when 'noinstall', 'noexist'
  unless ARGV.empty?
    puts ARGV.options.help
    exit( -1 )
  end
when 'whatprovides'
  if ARGV.empty?
    puts ARGV.options.help
    exit( -1 )
  end
else
  puts ARGV.options.help
  exit( 0 )
end
if Process.uid() != 0 then
  opt[:q] = true
end

localdb = RPM::DB.new
remotedb = MPH::DB.new(opt[:c])
MSG = MPH::Msg.instance

ret = nil
if Zlib then
  ret = remotedb.setup_db([MPH::DBLoader,MPH::TraditionalDBLoader], opt[:q],true,true,true)
else
  ret = remotedb.setup_db([MPH::TraditionalDBLoader], opt[:q],true,true,true)
end

if !ret
  MSG.print(MPH::MsgLevel::ERROR,"Can't read mph database.\n")
  exit -1
end

#//------------------------------------------------
#// mph-get-search noexist
#//------------------------------------------------
if sCommand == 'noexist' then
  localdb.sort.each do |lp|
    found = false
    remotedb.each_match(RPM::DBI_LABEL,lp.name) do |rp|
      found = true
    end
    if !found then
      MSG.print(MPH::MsgLevel::NORMAL, "? #{lp.name} : #{lp.version}\n")
    end
  end
  exit( 0 )

#//------------------------------------------------
#// mph-get-search noinstall
#//------------------------------------------------
elsif sCommand == 'noinstall' then
  remotedb.sort.each do |rp|
    if !localdb.init_iterator(RPM::DBI_LABEL,rp.name) then
      MSG.print(MPH::MsgLevel::NORMAL, "- #{rp.name} : #{rp.version}\n")
    end
  end
  exit( 0 )

#//------------------------------------------------
#// mph-get-search whatprovides
#//------------------------------------------------
elsif sCommand == 'whatprovides' then
  ARGV.each do |sName|
    found = false
    localdb.each_match(RPM::TAG_PROVIDENAME, sName) do |p|
      found = true
      MSG.print(MPH::MsgLevel::NORMAL, "Local : Item " + sName + ' is provided by ' + p.name + ".\n" )
    end
    remotedb.each_match(RPM::TAG_PROVIDENAME, sName) do |p|
      found = true
      MSG.print(MPH::MsgLevel::NORMAL, "Remote: Item " + sName + ' is provided by ' + p.name + ".\n" )
    end
    if !found then
      MSG.print(MPH::MsgLevel::NORMAL, "Item " + sName + " is not found.\n" )
    end
  end
  exit( 0 )
end

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