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

MSG = MPH::Msg.instance

#//------------------------------------------------
def m_ShowUsage()
  print( "#{COPYRIGHT}\n" )
  print( "Usage:\n" )
  print( "\t#{$0} [-knvf] [packages...]\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" )
end

#//------------------------------------------------
$USAGE = 'm_ShowUsage()'
parseArgs(0, nil, 'knvf')

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

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

def remove(db,remove_packages,do_it = false)
  ret = nil
  begin
      ret = `rpm -e --test #{remove_packages.keys.join(' ')} 2>&1`.grep(/is needed by/).collect{|i| i.chop.sub(/.* /,'')}
    ret = nil if ret.size == 0
    if do_it
      `rpm -e #{remove_packages.keys.join(' ')}`
    end
  rescue
    MSG.print(MPH::MsgLevel::ERROR,"#{$!} (#{$!.class})")
    MSG.print(MPH::MsgLevel::ERROR, $@.join("\n"))
    exit -1
  end
  ret
end

if ARGV[0] == 'remove' then
  ARGV.shift
end

localdb = RPM::DB.new
remove_packages = {}

ARGV.each do |name|
  mi = localdb.init_iterator(RPM::DBI_LABEL,name)
  if mi then
    mi.each do |p|
      remove_packages[p.to_s] = true
    end
  else
    MSG.print(MPH::MsgLevel::NORMAL, "Package " + name + " is not installed.\n")
  end
end

if !remove_packages.empty? then
  while ret = remove(localdb,remove_packages) do
    ret.each do |dep|
      remove_packages[dep] = true
    end
  end
  MSG.print(MPH::MsgLevel::NORMAL, "Following packages will be removed: \n")
  a = remove_packages.keys.sort
  a.each do |p|
    p =~ /\A(.+)-([^-]+-[^-]+)\Z/
    name, version = $1, $2
    MSG.print(MPH::MsgLevel::NORMAL, "D #{name} : #{version}\n")
  end
  GC.start
  if MSG.confirm("Proceed?",$OPT_f) != true then
    exit(-1)
  end
  if !$OPT_n then
    remove(localdb,remove_packages,true)
  end
end

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