#!/bin/sh
#
# Script for installing all available debug packages for given
# binaries and their direct library dependencies. This file is
# part of maemo-debug-scripts.
#
# Copyright (C) 2006,2007 by Nokia Corporation
#
# Contact: Eero Tamminen <eero.tamminen@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License 
# version 2 as published by the Free Software Foundation. 
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
#
#
# Changelog
# 2007-01-23:
# - Install only debug packages matching binary package version so
#   that user doesn't accidentally upgrade anything
# - Fix bug when apt-cache search returns multiple debug packages
#   -> check only for (new style) debug package which name matches
#      the binary package name
# 2006-11-28:
# - Install also debug package for the given binary, not just its deps
# 2007-01-23:
# - Install only debug packages with versions matching the binaries
#   to avoid accidental package updates
# 2007-08-27:
# - Use target ld directly as host ldd can (using host ld) can report
#   wrong libraries.  Glibc 2.5 has new ld version.

if [ $# -lt 1 ]; then
	echo "usage: $0 <binaries>"
	echo
	echo "Installs all available debug packages for the given binaries"
	echo "and their direct library dependencies."
	exit 1
fi

bins=$*
for bin in $bins; do
	if [ \! -x $bin ]; then
		if [ -z "$(echo $bin|grep \.so)" ]; then
			echo "ERROR: '$bin' doesn't exist or isn't neither executable nor library"
			exit 1
		fi
	fi
	if [ \! -z "$(ls -l $bin|grep maemo-invoker)" ]; then
		echo "ERROR: you gave link to maemo-invoker, that's probably not what you wanted?"
		ls -l "$bin"
		exit 1
	fi
done

if [ -f /targets/links/scratchbox.config ]; then
	apt_get="fakeroot apt-get"
	#ldd="ldd"	# ldd uses host ld which doesn't always find libs
	ldd="/lib/ld-2.5.so --list"
else
	# no fakeroot or ldd on target
	apt_get="apt-get --allow-unauthenticated"
	ldd="/lib/ld-2.5.so --list"
fi

echo "Making sure we see all available packages..."
apt-get update
echo

echo "For each linked library, checking its debian package name..."
libs=$(for bin in $bins; do
	$ldd $bin|cut -d'>' -f2|cut -d'(' -f1
done|sort -u)
files=$(echo $bins $libs|sort -u)
pkgs=$(for file in $files; do
	dpkg -S $file|cut -d: -f1
done|sort -u)

# debug package and if yes, install it
missing=""
install=""
echo "For each package, checking whether there's corresponding debug package..."
for pkg in $pkgs; do
	dbg=$pkg-dbg
	apt-cache show $dbg > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		# use same version from dbg package as binary is
		version=$(dpkg -s $pkg|grep Version:|cut -d' ' -f2)
		install="$install $dbg=$version"
	else
		missing="$missing $pkg"
	fi
done

echo "Installing the debug packages..."
if [ -z "$install" ]; then
	echo "ERROR: no debug packages found!"
else
	failed=""
	for pkg in $install; do
		$apt_get install $pkg
		if [ $? -ne 0 ]; then
			failed="$failed $pkg"
		fi
	done
	echo "Installation was performed for following debug packages:"
	for pkg in $install; do
		echo "- $pkg"
	done

	if [ \! -z "$failed" ]; then
		echo "Installation FAILED for following of the above packages:"
		for pkg in $failed; do
			echo "- $pkg"
		done
	fi
fi

if [ \! -z "$missing" ]; then
	echo "Debug packages are MISSING for following dependencies:"
	for pkg in $missing; do
		echo "- $pkg"
	done
fi
echo "DONE!"
