#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2006-2007 (C) Pete Savage <petesavage@ubuntu.com>
# Copyright 2007 (C) Siegfried-A. Gevatter <rainct@ubuntu.com>
#
# ##################################################################
#
# 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.
#
# See file /usr/share/common-licenses/GPL for more details.
#
# ##################################################################
#
# This script is used to check if a package and all its build
# dependencies are in main or not.

import subprocess
import sys

def process_deps(deps):
	"""Takes a list of (build) dependencies and processes it."""
	
	for package in [ package.split('|')[0] for package in deps ]:
		
		# Cut package name (from something like "name ( >= version)")
		name = package.split(' ')[0]
		
		if not packages.has_key(name) and name != '':
			# Check the (build) dependencies recursively
			find_main(name)


def find_main(pack):
	"""Searches the dependencies and build dependencies of a package recursively
	to determine if they are all in the 'main' component or not."""
	
	global packages
	
	# Retrieve information about the package
	out = subprocess.Popen('apt-cache madison ' + pack + '  | grep ' + distro + ' | grep -m 1 Packages', shell=True, stdout=subprocess.PIPE).stdout.read()
	
	if out.find("/main") != -1:
		packages[pack] = True
		return 1
	else:
		if not packages.has_key(pack):
			packages[pack] = False
		
		# Retrive package dependencies
		deps = subprocess.Popen('apt-cache show ' + pack + ' | grep -m 1 ^Depends', shell=True, stdout=subprocess.PIPE).stdout.read().split('\n')[0].replace('Depends: ', '').split(', ')
		
		process_deps(deps)
		
		# Retrieve package build dependencies
		deps1 = subprocess.Popen('apt-cache showsrc ' + pack + ' | grep -m 1 ^Build-Depends', shell=True, stdout=subprocess.PIPE).stdout.readlines()
		deps = []
		
		for builddep in deps1:
			if builddep.startswith('Build-Depends'):
				deps += builddep.strip('\n').replace('Build-Depends: ', '').replace('Build-Depends-Indep: ',  '').split(', ')
		
		process_deps(deps)


def main():
	
	global packages, distro
	
	# Check if the amount of arguments is correct
	if len(sys.argv) < 2 or len(sys.argv) > 3 or sys.argv[1] in ('help', '-h', '--help'):
		print 'Usage: %s <package name> [<distribution>]' % sys.argv[0]
		sys.exit(1)
	
	if len(sys.argv) == 3 and sys.argv[2]:
		distro = sys.argv[2]
		if not subprocess.Popen('apt-cache madison bash | grep ' + distro, shell=True, stdout=subprocess.PIPE).stdout.read():
			print '«%s» is not a valid distribution.' % distro
			print 'Remember that for 404main to work with a certain distribution it must be in your /etc/apt/sources.list file.'
			sys.exit(1)
	else:
		distro = subprocess.Popen('lsb_release -cs', shell=True, stdout=subprocess.PIPE).stdout.read().strip('\n')
	
	if not subprocess.Popen('apt-cache madison ' + sys.argv[1] + ' | grep ' + distro, shell=True, stdout=subprocess.PIPE).stdout.read():
		print 'Can\'t find package «%s» in distribution «%s».' % (sys.argv[1], distro)
		sys.exit(1)
	
	print 'Checking package «%s» in distribution «%s»...' % (sys.argv[1], distro)
	
	find_main(sys.argv[1])
	
	# True if everything checked until the point is in main
	all_in_main = True
	
	for package in packages:
		if not packages[package]:
			if all_in_main:
				print 'The following packages aren\'t in main:'
				all_in_main = False
			print '  ', package
	
	if all_in_main:
		print 'Package «%s» and all its dependencies and build dependencies are in main.' % sys.argv[1]

if __name__ == '__main__':
	
	# Global variable to hold the status of all packages
	packages = {}
	
	# Global variable to hold the target distribution
	distro = ''
	
	try:
		main()
	except KeyboardInterrupt:
		print 'Aborted.'
		sys.exit(1)