install.sh (1069B)
1 #!/bin/sh 2 3 usage() { 4 echo "usage: $0 [ -D ] [ -l ] [ -m mode ] [ -O owner:group ] src dst" 1>&2 5 exit 1 6 } 7 8 mkdirp=false 9 symlink=false 10 mode=0755 11 og= 12 13 while getopts Dlm:O: name ; do 14 case "$name" in 15 D) mkdirp=true ;; 16 l) symlink=true ;; 17 m) mode=$OPTARG ;; 18 O) og=$OPTARG ;; 19 ?) usage ;; 20 esac 21 done 22 shift $(($OPTIND - 1)) 23 24 test "$#" -eq 2 || usage 25 src=$1 26 dst=$2 27 tmp="$dst.tmp.$$" 28 29 case "$dst" in 30 */) echo "$0: $dst ends in /" 1>&2 ; exit 1 ;; 31 esac 32 33 set -C 34 set -e 35 36 if $mkdirp ; then 37 umask 022 38 case "$2" in 39 */*) mkdir -p "${dst%/*}" ;; 40 esac 41 fi 42 43 trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP 44 45 umask 077 46 47 if $symlink ; then 48 ln -s "$src" "$tmp" 49 else 50 cat < "$1" > "$tmp" 51 if test -n "$og" ; then 52 chown -- "$og" "$tmp" 53 fi 54 chmod -- "$mode" "$tmp" 55 fi 56 57 mv -f "$tmp" "$dst" 58 if test -d "$dst" ; then 59 rm -f "$dst/$(basename $tmp)" 60 if $symlink ; then 61 mkdir "$tmp" 62 ln -s "$src" "$tmp/$(basename $dst)" 63 mv -f "$tmp/$(basename $dst)" "${dst%/*}" 64 rmdir "$tmp" 65 else 66 echo "$0: $dst is a directory" 1>&2 67 exit 1 68 fi 69 fi