#!/bin/bash VERSION=0.1 find_libs=$(dirname "$(readlink -f "$0")")/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 resolve_interpreter() { local file="$1" local interp interp=$(head -c 256 "$file" | awk -F '[#! \t]+' 'NR==1 && /^#!/{print $2;exit}') [ -n "$interp" ] || return echo "$interp" # /usr/bin/env python2 -> python2 case "$interp" in env|/usr/bin/env) awk -F '[#! \t]+' '{ for (i=3;i/dev/null)" || true if [ -n "$realsubject" ]; then subjects[${#subjects[@]}]="$realsubject" for extra in $(resolve_interpreter "$realsubject"); do if path="$(which "$extra" 2>/dev/null)"; then subjects[${#subjects[@]}]="$path" else echo "Missing program: $extra" ((errors++)) fi done else echo "Missing file: $subject" ((errors++)) fi done if [ $errors -ne 0 ]; then echo "$errors files could not be found" 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"