From af38cb02a9ca95b20056bc92e73fe72a45d1f523 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Tue, 23 Apr 2013 18:59:15 +0200 Subject: Initial checkin. --- build-chroot | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 build-chroot (limited to 'build-chroot') diff --git a/build-chroot b/build-chroot new file mode 100755 index 0000000..cd608e6 --- /dev/null +++ b/build-chroot @@ -0,0 +1,116 @@ +#!/bin/bash +VERSION=0.1 +find_libs=~/scripts/find-libs +usage() { + cat < + +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" -- cgit v1.2.1