#!/usr/local/bin/ruby18
# -*- ruby -*-
#
# Copyright (c) 2000-2004 Akinori MUSHA
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

require "pkgtools"

def main(argv)
  chk_db_init()
  chk_db_phase1()
  chk_db_phase2()

  return 0
end


def chk_db_init()
  $pkgnames = $pkgdb.installed_pkgs

  $req_hash = {}	# a hash of pkgname => { dependent1 => true , ... } pairs
  $fix_hash = {}	# a hash of pkgname => ans pairs
end


def chk_db_phase1()
  # fix missing or stale origins
  org_hash = {}		# a hash of origin => [pkg1, pkg2, ...] pairs

  $pkgnames.each do |pkgname|
    # Check origin

    pkg = PkgInfo.new(pkgname)
    origin = pkg.origin

    if ! $portsdb.exist?(origin, true)
      exit 1
    end

    if org_hash.key?(origin)
      org_hash[origin] << pkg
    else
      org_hash[origin] = [pkg]
    end
  end

  chk_duplicates(org_hash)

  $pkgnames.each do |pkgname|
    # check and fix dependencies
    chk_dependencies(pkgname)
  end
end


def chk_dependencies(pkgname)
  deps = $pkgdb.pkgdep(pkgname, true) or return

  deps.each do |dep, origin|
    unless $pkgnames.include?(dep)
      # Stale dependency

      if config_held?(pkgname) && !$force
	# Package is held -> ignore
	next
      end

      exit 1
    end

    ($req_hash[dep] ||= {})[pkgname] = true
  end
end


def chk_db_phase2()
  # +REQUIRED_BY files
  tsort = TSort.new

  $pkgnames.each do |pkgname|
    if $req_hash.key?(pkgname)
      req_by = $req_hash[pkgname].keys

      req_by.each { |req|
	tsort.add(req, pkgname)
      }
    end
  end

  # unlink cyclic dependencies
  chk_cycles(tsort)
end


def chk_cycles(tsort)
  tsort.tsort! do |cycle|
    exit 1
  end
end


def chk_duplicates(org_hash)
  $pkgdb.close_db

  org_hash.each do |origin, pkgs|
    next if pkgs.size < 2
      exit 1
  end
end

exit(main(ARGV) || 1)
