#!/bin/sh -e
#
# Functions used by most of chrooted scripts
# Copyright (C) 2000-2002  Dmitry V. Levin <ldv@altlinux.org>
#
# 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.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

PROG="${0##*/}"
export LANG=C LANGUAGE=C LC_ALL=C

function Fatal()
{
	echo "${0##*/}: $*" >&2
	exit 1
}

# NAME
#	Copy - copy file
#
# SYNOPSIS
#	Copy [OPTIONS]... SOURCE DEST
#
# DESCRIPTION
#	Copy SOURCE to DEST.
#	Try to hardlink DEST to SOURCE if possible.
#	Avoid copying if SOURCE is same as DEST according to cmp(1),
#	and no "force" have been specified.
#
Copy()
{
	local args=
	local verbose=
	local force=
	local cp=/bin/cp

	local TEMP
	TEMP=`/bin/getopt -n Copy -o qvfo:g:m: -l quiet,verbose,force,owner:,group:,mode: -- "$@"` || return
	eval set -- "$TEMP"

	while :; do
		case "$1" in
			-q|--quiet)
				shift
				verbose=
				;;
			-v|--verbose)
				shift
				verbose=-v
				;;
			-f|--force)
				shift
				force=-f
				;;
			-o|--owner)
				shift
				args="$args -o $1"
				cp=/bin/install
				shift
				;;
			-g|--group)
				shift
				args="$args -g $1"
				cp=/bin/install
				shift
				;;
			-m|--mode)
				shift
				args="$args -m $1"
				cp=/bin/install
				shift
				;;
			--) shift; break ;;
		esac
	done

	local src="$1"
	[ -n "$src" ] || { echo "Copy: SOURCE not specified" >&2; return 1; }
	shift

	local dst="$1"
	[ -n "$dst" ] || { echo "Copy: DEST not specified" >&2; return 1; }
	shift

	[ -r "$src" ] || { [ -z "$verbose" ] || echo "Copy: SOURCE \"$src\" is not available" >&2; return 1; }

	if [ -z "$force" ] && /usr/bin/cmp -s "$src" "$dst"; then
		return 0
	fi
	if ! /bin/ln -nf $verbose "$src" "$dst" 2>/dev/null; then
		$cp -p $verbose $args "$src" "$dst"
	fi
}

# NAME
#	CopyLibs - copy libraries and executables
#
# SYNOPSIS
#	Copy [OPTIONS]... FILES
#
# DESCRIPTION
#	Copy FILES to DESTDIR.
#	Traces library dependencies using ldd(1).
#	Uses Copy() for actual copying.
#
CopyLibs()
{
	local args=
	local mode='-m 755'
	local verbose=
	local force=
	local destdir=
	local libs=

	local TEMP
	TEMP=`/bin/getopt -n CopyLibs -o qvfd:l:o:g:m: -l quiet,verbose,force,destdir:libs:owner:,group:,mode: -- "$@"` || return
	eval set -- "$TEMP"

	while :; do
		case "$1" in
			-q|--quiet)
				shift
				verbose=
				;;
			-v|--verbose)
				shift
				verbose=-v
				;;
			-f|--force)
				shift
				force=-f
				;;
			-d|--destdir)
				# This is mandatory option.
				shift
				destdir="$1"
				shift
				;;
			-l|--libs)
				# Copy these libraries without tracing dependencies.
				shift
				libs="$libs $1"
				shift
				;;
			-o|--owner)
				shift
				args="$args -o $1"
				shift
				;;
			-g|--group)
				shift
				args="$args -g $1"
				shift
				;;
			-m|--mode)
				shift
				mode="-m $1"
				shift
				;;
			--) shift; break ;;
		esac
	done

	[ -n "$destdir" -a -d "$destdir" ] || { echo "CopyLibs: invalid or missing DESTDIR specified" >&2; return 1; }
	[ -n "$*" -o -n "$libs" ] || { echo "CopyLibs: mandatory arguments not specified" >&2; return 1; }

	libs="$(for n in "$libs" `/usr/bin/ldd "$@" 2>/dev/null |/bin/fgrep -v ' not a dynamic executable' |/bin/awk '{print $3}'`; do echo "$n"; done |/bin/sort -u)"
	for n in $libs; do
		local src=$(/bin/readlink -n -f "$n" ||:)
		[ -n "$src" ] || continue # Skip missing files.
		local dst="$destdir/${n##*/}"
		Copy $verbose $force $mode $args "$src" "$dst"
	done
}
