1#!/bin/sh
2#
3# This is an actually-safe install command which installs the new
4# file atomically in the new location, rather than overwriting
5# existing files.
6#
7
8usage() {
9printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2
10exit 1
11}
12
13mkdirp=
14symlink=
15mode=755
16
17while getopts Dlm: name ; do
18case "$name" in
19D) mkdirp=yes ;;
20l) symlink=yes ;;
21m) mode=$OPTARG ;;
22?) usage ;;
23esac
24done
25shift $(($OPTIND - 1))
26
27test "$#" -eq 2 || usage
28src=$1
29dst=$2
30tmp="$dst.tmp.$$"
31
32case "$dst" in
33*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
34esac
35
36set -C
37set -e
38
39if test "$mkdirp" ; then
40umask 022
41case "$2" in
42*/*) mkdir -p "${dst%/*}" ;;
43esac
44fi
45
46trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
47
48umask 077
49
50if test "$symlink" ; then
51ln -s "$1" "$tmp"
52else
53cat < "$1" > "$tmp"
54chmod "$mode" "$tmp"
55fi
56
57mv -f "$tmp" "$2"
58test -d "$2" && {
59rm -f "$2/$tmp"
60printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
61exit 1
62}
63
64exit 0
65