summaryrefslogtreecommitdiff
path: root/build-chroot
blob: cd608e65e1003c938fcc35b33463e1734163d879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
VERSION=0.1
find_libs=~/scripts/find-libs
usage() {
	cat <<HELP
Copies binaries and their dependencies for chroot usage.

Copyright (C) 2012 Peter Wu
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by Peter Wu <lekensteyn@gmail.com>

Usage: $0 [OPTION] file...

Options:
  -n, --dry-run           Do not copy files, just print the action that would be
                            performed.
  -d DIR, --destdir DIR   Destination directory for the files.
  -c, --continue          Continue even if a given file cannot be located.
  -h, --help              Print this help text, then exit.
  --version               Prints the version of this program, then exit.

Environment:
  DESTDIR   If --destdir is not given, this environment variable is sought. If
            this variable does not exist either, a temporary directory is
            created which is printed on termination.
HELP
}

opts=`getopt --name "$0" -o nd:ch \
	--longoptions dry-run,destdir:,continue,help,version \
	-- "$@"`
if [ $? != 0 -o -z "$opts" ]; then
	usage
	exit 1
fi
eval set -- "$opts"

DRY_RUN=false
CONT_ON_ERR=false
while :; do
	case "$1" in
	-n|--dry-run) DRY_RUN=true ;;
	-d|--destdir) DESTDIR="$2" ; shift ;;
	-c|--continue) CONT_ON_ERR=true ;;
	-h|--help) usage; exit 0 ;;
	--version) echo "$VERSION" ; exit 0 ;;
	--) shift; break ;;
	*) echo "Internal error!"; exit 1 ;;
	esac
	shift
done

if [ $# -eq 0 ]; then
	echo "Need at least one binary or library to isolate."
	echo "Try '$0 --help' for more information."
	exit 1
fi

subjects=()
for subject; do
	realsubject="$subject"
	#realsubject="$(readlink -e "$subject")"
	[ -e "$realsubject" ] || realsubject="$(which "$subject" 2>/dev/null)" || true
	if [ -n "$realsubject" ]; then
		subjects[${#subjects[@]}]="$realsubject"
	else
		echo "Missing file: $subject"
	fi
done

if ! [ $# = ${#subjects[@]} ]; then
	echo "Some files could not be found"
	echo "Expected $#, got ${#subjects[@]} valid args."
	if ! $CONT_ON_ERR; then
		echo "Aborting. To ignore this, pass the '--continue' option."
		exit 255
	fi
fi

info="$("$find_libs" "${subjects[@]}")"
echo "$info"
count=${info%% *}
filelist="${info#* }"

try_install() {
	local file="$1"
	# target without leading ../../etc
	local target="$DESTDIR/$(sed 's,\(\.\./\)*,,' <<<"$file")"
	if [ ! -e "$target" ]; then
		if $DRY_RUN; then
			echo "Installing $file"
		else
			install -v -D "$file" "$target"
		fi
	fi
}
if [ $count -gt 0 ]; then
	tmpdir=
	if [ -z "$DESTDIR" ]; then
		tmpdir="$(mktemp -d)"
		DESTDIR="$tmpdir"
	fi
	umask 002
	while read file; do
		try_install "$file"
	done < "$filelist"
	[ -z "$tmpdir" ] || rmdir --ignore-fail-on-non-empty "$tmpdir"
else
	echo "No files to copy"
fi
for subject in "${subjects[@]}"; do
	try_install "$subject"
done
rm "$filelist"