#!/usr/bin/env python
#
# Copyright (c) 2008 Ben Motmans <ben.motmans@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import os
import sys
import string
import shutil
import commands
import datetime
import tempfile

def check_dependencies ():
	output = commands.getoutput ("python -c 'from configobj import ConfigObj'")
	if (output != ""):
		print "ERROR: you need to install ConfigObj before running this script"
		print "for example: apt-get install python-configobj"
		return False
	else:
		return True

def print_section (name):
	line = "".ljust (50, "+")
	print line
	print "+++ " + name
	print line

def print_item (item, value):
	header = item.ljust (20)

	print header + ": " + value

def print_os_info ():
	print_section ("Operating System")

	opsys = commands.getoutput ("uname -rvs")
	arch = commands.getoutput ("uname -m")

	if os.path.exists ("/etc/lsb-release"):
		release = ConfigObj ("/etc/lsb-release")
		distro = release["DISTRIB_ID"] + " " + release["DISTRIB_RELEASE"] + " (" + release["DISTRIB_CODENAME"] + ") [" + release["DISTRIB_DESCRIPTION"] + "]"
	else:
		distro = "<no lsb info detected>"

	print_item ("OS", opsys)
	print_item ("Distro", distro)
	print_item ("Arch", arch)

	print ""

def print_is_installed (executable, check_version):
	which = commands.getoutput ("which " + executable)

	if which == "":
		print_item (executable, "no")
	else:
		if check_version == True:
			version_info = commands.getoutput (executable + " --version")
			if version_info.find ("\n"):
				version = version_info.split ("\n")[0]
			else:
				version = version_info

			print_item (executable, "yes (" + version + ")")
		else:
			print_item (executable, "yes")

def print_pkg_config_module_info (module):
	exists = commands.getstatusoutput ("pkg-config --exists " + module)[0] == 0
	pkgconfig = commands.getoutput ("pkg-config --modversion " + module)

	if exists == False:
		print_item (module, "no")
	else:
		print_item (module, "yes (" + pkgconfig + ")")

def print_gac_assembly_info (assembly):
	gac_info = commands.getoutput ("gacutil -l | grep " + assembly)
	if gac_info == "":
		found = "no"
	else:
		found = "yes"

	print assembly + ": " + found
	for line in gac_info.split ("\n"):
		print "\t" + line

def print_app_info ():
	print_section ("Applications")

	print_is_installed ("mono", True)
	print_is_installed ("gmcs", True)
	print_is_installed ("gcc", True)

	print ""

def print_gac_info ():
	print_section ("GAC")

	print_is_installed ("gacutil", False)

	print ""

	print_gac_assembly_info ("gtk-sharp")
	print_gac_assembly_info ("glib-sharp")
	print_gac_assembly_info ("glade-sharp")
	print_gac_assembly_info ("gecko-sharp")
	print_gac_assembly_info ("Mono.Nat")
	print_gac_assembly_info ("Mono.Addins")
	print_gac_assembly_info ("NDesk")
	print_gac_assembly_info ("Anculus")
	print_gac_assembly_info ("ICSharpCode.SharpZipLib")

	print ""

def print_pkg_config_info ():
	print_section ("pkg-config")

	print_is_installed ("pkg-config", True)

	print ""

	print_pkg_config_module_info ("mono")
	print_pkg_config_module_info ("mono-addins")
	print_pkg_config_module_info ("mono-nat")

	print ""

	print_pkg_config_module_info ("glib-sharp-2.0")
	print_pkg_config_module_info ("gtk-sharp-2.0")
	print_pkg_config_module_info ("glade-sharp-2.0")
	print_pkg_config_module_info ("gecko-sharp-2.0")
	print_pkg_config_module_info ("gstreamer-0.10")

	print ""

	print_pkg_config_module_info ("ndesk-dbus-1.0")
	print_pkg_config_module_info ("ndesk-dbus-glib-1.0")

	print ""

	print_pkg_config_module_info ("anculus-core")
	print_pkg_config_module_info ("anculus-core-extended")
	print_pkg_config_module_info ("anculus-gui")

	print ""

def diagnose ():
	print_os_info ()
	print_app_info ()
	print_gac_info ()
	print_pkg_config_info ()

if check_dependencies () == True:
	from configobj import ConfigObj
	diagnose ()
