install.sh 924 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. usage() {
  8. printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2
  9. exit 1
  10. }
  11. mkdirp=
  12. symlink=
  13. mode=755
  14. while getopts Dlm: name ; do
  15. case "$name" in
  16. D) mkdirp=yes ;;
  17. l) symlink=yes ;;
  18. m) mode=$OPTARG ;;
  19. ?) usage ;;
  20. esac
  21. done
  22. shift $(($OPTIND - 1))
  23. test "$#" -eq 2 || usage
  24. src=$1
  25. dst=$2
  26. tmp="$dst.tmp.$$"
  27. case "$dst" in
  28. */) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
  29. esac
  30. set -C
  31. set -e
  32. if test "$mkdirp" ; then
  33. umask 022
  34. case "$2" in
  35. */*) mkdir -p "${dst%/*}" ;;
  36. esac
  37. fi
  38. trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
  39. umask 077
  40. if test "$symlink" ; then
  41. ln -s "$1" "$tmp"
  42. else
  43. cat < "$1" > "$tmp"
  44. chmod "$mode" "$tmp"
  45. fi
  46. mv -f "$tmp" "$2"
  47. test -d "$2" && {
  48. rm -f "$2/$tmp"
  49. printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
  50. exit 1
  51. }
  52. exit 0