summaryrefslogtreecommitdiff
path: root/tools/install.sh
blob: e96dd7b7796925d9a9ae319c1fe76a2fa62bef44 (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
#!/bin/sh

usage() {
  echo "usage: $0 [ -D ] [ -l ] [ -m mode ] [ -O owner:group ] src dst" 1>&2
  exit 1
}

mkdirp=false
symlink=false
mode=0755
og=

while getopts Dlm:O: name ; do
  case "$name" in
    D) mkdirp=true ;;
    l) symlink=true ;;
    m) mode=$OPTARG ;;
    O) og=$OPTARG ;;
    ?) usage ;;
  esac
done
shift $(($OPTIND - 1))

test "$#" -eq 2 || usage
src=$1
dst=$2
tmp="$dst.tmp.$$"

case "$dst" in
  */) echo "$0: $dst ends in /" 1>&2 ; exit 1 ;;
esac

set -C
set -e

if $mkdirp ; then
  umask 022
  case "$2" in
    */*) mkdir -p "${dst%/*}" ;;
  esac
fi

trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP

umask 077

if $symlink ; then
  ln -s "$src" "$tmp"
else
  cat < "$1" > "$tmp"
  if test -n "$og" ; then
    chown -- "$og" "$tmp"
  fi
  chmod -- "$mode" "$tmp"
fi

mv -f "$tmp" "$dst"
if test -d "$dst" ; then
  rm -f "$dst/$(basename $tmp)"
  if $symlink ; then
    mkdir "$tmp"
    ln -s "$src" "$tmp/$(basename $dst)"
    mv -f "$tmp/$(basename $dst)" "${dst%/*}"
    rmdir "$tmp"
  else
    echo "$0: $dst is a directory" 1>&2
    exit 1
  fi
fi