#!/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} [-vcq] noinstall\n" )
  print( "\t#{$0} [-vcq] noexist\n" )
  print( "\t#{$0} [-vcq] whatprovides [item...]\n" )
  print( "Options:\n" );
  print( "\t-n\tno execute\n" )
  print( "\t-v\tverbose mode\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 = ['noinstall', 'noexist', 'whatprovides']
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

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
  if ARGV.size() < 1 then
    m_ShowUsage()
    exit( -1 )
  end
  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:
