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

#//------------------------------------------------
def m_ShowUsage()
  print( "#{COPYRIGHT}\n" )
  print( "Usage:\n" )
  print( "\t#{$0} [-knvcfqx] install [packages...]\n" )
  print( "\t#{$0} [-knvcfq] upgrade\n" )
  print( "\t#{$0} [-knvcfqix] get [packages...]\n" )
  print( "\t#{$0} [-knvcfq] get-all\n" )
  print( "\t#{$0} [-knvcfq] repair\n" )
  print( "Options:\n" );
  print( "\t-k\twith kernel\n" )
  print( "\t-n\tno execute\n" )
  print( "\t-v\tverbose mode\n" )
  print( "\t-f\tforce mode\n" )
  print( "\t-c <file>\tread configurations from <file>\n" )
  print( "\t-q\tdon't update database\n" )
  print( "\t-x\tignore $holds[] in mph.conf\n" )
end

#//------------------------------------------------
$USAGE = 'm_ShowUsage()'
parseArgs( 0, nil, 'knvfqix', 'c:/etc/mph.conf' )

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

sCommand = ARGV.shift()

asCommands = ['install', 'installall' ,'upgrade', 'get', 'get-all' ,'repair']
if asCommands.index( sCommand ) == nil then
  m_ShowUsage()
  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:
