diff options
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/install.sh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tools/install.sh b/tools/install.sh new file mode 100755 index 0000000..4641973 --- /dev/null +++ b/tools/install.sh @@ -0,0 +1,48 @@ +#!/bin/sh -e +# Portable install version that supports -D -m and -t +# We have our own extension flag -s for running sed on the given files while +# installing. +die() { printf '%s\n' "$@" >&2; exit 1;} +usage() { + die "usage: ${0##*/} [-D] [-m mode] [-s sedscript] source dest" \ + " or: ${0##*/} [-D] [-m mode] [-s sedscript] [-t dir] [source...]" +} + +sed='' +mkdirp='' +target='' +mode=0755 + +while getopts 'Dm:s:t:h' opt; do + case $opt in + D) mkdirp=1 ;; + s) sed=$OPTARG ;; + t) target=$OPTARG ;; + m) mode=$OPTARG ;; + *) usage + esac +done + +shift "$((OPTIND - 1))" + +if [ "$target" ]; then + [ "$mkdirp" ] || [ -d "$target" ] || die "$target doesn't exist" + mkdir -p "$target" + for arg; do + [ -d "$target/${arg##*/}" ] && die "$target/${arg##*/} is a directory" + if [ "$sed" ]; then + sed "$sed" < "$arg" > "$target/${arg##*/}" + else + cp "$arg" "$target" + fi + + chmod "$mode" "$target/${arg##*/}" + done +else + case "$2" in */*) [ "$mkdirp" ] || [ -d "${2%/*}" ] || die "${2%/*} doesn't exist" + mkdir -p "${2%/*}" + esac + [ -d "$2" ] && die "$2 is a directory" + if [ "$sed" ]; then sed "$sed" < "$1" > "$2"; else cp "$1" "$2"; fi + chmod "$mode" "$2" +fi |