#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# PyLDD
# Copyright (C) 2007 Jan Niklas Hasse <jhasse@gmail.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 3 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, see <http://www.gnu.org/licenses/>.

import sys
import commands
import re
import os

result = []

def getPath(line):
	prog = re.compile(".* => (.*) .*")
	result = prog.match(line)
	if result != None:
		if not os.path.exists(result.group(1)) and result.group(1) != '':
			print "ERROR: " + result.group(0)
			sys.exit(-1)
		return result.group(1)
	return None

def parseLib(lib):
	cmdline = "ldd " + lib
	for line in commands.getoutput(cmdline).split('\n'):
		path = getPath(line)
		if path in result:
			result.remove(path)

if __name__ == '__main__':
	cmdline = "ldd " + sys.argv[1]
	for line in commands.getoutput(cmdline).split('\n'):
		path = getPath(line)
		if path != None and path != '':
			result.append(path)
	for lib in result:
		parseLib(lib)
	for line in result:
		print "'" + line + "'"

