#!/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/application'

#//------------------------------------------------
opt = {
  :c => '/etc/mph.conf'
}
ARGV.options {|o|
  Version = '0.92.8'
  o.banner = "#{COPYRIGHT}\n"
  o.banner << "Usage:\n"
  o.banner << "\t#{$0} [-knvcfqx] install [packages...]\n"
  o.banner << "\t#{$0} [-knvcfq] upgrade\n"
  o.banner << "\t#{$0} [-knvcfqx] get [packages...]\n"
  o.banner << "\t#{$0} [-knvcfq] get-all\n"
  o.banner << "\t#{$0} [-knvcfq] installall\n"
  o.banner << "\t#{$0} [-knvcfq] repair\n"
  o.banner << "\t#{$0} [-knvf] remove [packages...]\n"
  o.banner << "\t#{$0} clean\n"
  o.banner << "Options:\n"
  o.on('-k', 'with kernel') {|v| opt[:k] = true }
  o.on('-n', 'no execute') {|v| opt[:n] = true }
  o.on('-f', 'force mode') {|v| opt[:f] = true }
  o.on('-q', 'don\'t update database') {|v| opt[:q] = true }
  o.on('-x', 'ignore $holds[] in mph.conf') {|v| opt[:x] = 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 = ['install', 'installall' ,'upgrade', 'get', 'get-all' ,'repair']
if asCommands.index( sCommand ) == nil then
  puts ARGV.options.help
  exit( 0 )
end

MSG = MPH::Msg.instance

if Process.uid() != 0 then
  MSG.print(MPH::MsgLevel::ERROR, "Only root can do it.\n" )
  exit( -1 )
end

if opt[:n] then
  opt[:q] = true
end

remotedb = MPH::Mph.new(opt[:c], opt[:f])
if !remotedb.setup_db(opt[:q], opt[:x], opt[:k]) then
  MSG.print(MPH::MsgLevel::ERROR,"failed to setup mph database.\n")
  exit( -1 )
end

#//------------------------------------------------
#// body
#//------------------------------------------------
provides = nil
if sCommand =~ /^(install|get)$/ then
  provides = ARGV
elsif sCommand =~ /^(installall)$/ then
  provides = []
  remotedb.each do |rp|
    provides << rp.name
  end
elsif sCommand =~ /^(repair)$/ then
  provides = []
  localdb = RPM::DB.new
  localdb.each do |p|
    p.requires.each do |r|
      version_ok = false
      mi = localdb.init_iterator(RPM::TAG_PROVIDENAME,r.name)
      if mi then
        if !mi.find {|lp| r.satisfy?(lp)}
          provides << r.name
        end
      else
        provides << r.name
      end
    end
  end
  localdb = nil
end

if !remotedb.resolve(provides) then
  exit( -1 )
end

# hold ξ򤫤..

if remotedb.need_to_install.length() == 0 then
  exit(0)
end

if sCommand =~ /^(install|upgrade)$/ then
  MSG.print(MPH::MsgLevel::NORMAL, "#{remotedb.need_to_install.length()} packages will be installed\n")
end

remotedb.print_download_info

if !MSG.confirm("Proceed?", opt[:f]) then
  exit(-1)
end

if !remotedb.get_all_packages(opt[:n]) then
  exit(-1)
end

if sCommand =~ /^(get-all|get)$/ then
  exit(0)
end

remotedb.install(opt[:n])

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