install-sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #! /bin/sh
  2. #
  3. # install - install a program, script, or datafile
  4. # This comes from X11R5 (mit/util/scripts/install.sh).
  5. #
  6. #
  7. # Copyright 1991 by the Massachusetts Institute of Technology
  8. #
  9. # Permission to use, copy, modify, distribute, and sell this software and its
  10. # documentation for any purpose is hereby granted without fee, provided that
  11. # the above copyright notice appear in all copies and that both that
  12. # copyright notice and this permission notice appear in supporting
  13. # documentation, and that the name of M.I.T. not be used in advertising or
  14. # publicity pertaining to distribution of the software without specific,
  15. # written prior permission. M.I.T. makes no representations about the
  16. # suitability of this software for any purpose. It is provided "as is"
  17. # without express or implied warranty.
  18. #
  19. # Calling this script install-sh is preferred over install.sh, to prevent
  20. # `make' implicit rules from creating a file called install from it
  21. # when there is no Makefile.
  22. #
  23. # This script is compatible with the BSD install script, but was written
  24. # from scratch. It can only install one file at a time, a restriction
  25. # shared with many OS's install programs.
  26. # set DOITPROG to echo to test this script
  27. # Don't use :- since 4.3BSD and earlier shells don't like it.
  28. doit="${DOITPROG-}"
  29. # put in absolute paths if you don't have them in your path; or use env. vars.
  30. mvprog="${MVPROG-mv}"
  31. cpprog="${CPPROG-cp}"
  32. chmodprog="${CHMODPROG-chmod}"
  33. chownprog="${CHOWNPROG-chown}"
  34. chgrpprog="${CHGRPPROG-chgrp}"
  35. stripprog="${STRIPPROG-strip}"
  36. rmprog="${RMPROG-rm}"
  37. mkdirprog="${MKDIRPROG-mkdir}"
  38. transformbasename=""
  39. transform_arg=""
  40. instcmd="$mvprog"
  41. chmodcmd="$chmodprog 0755"
  42. chowncmd=""
  43. chgrpcmd=""
  44. stripcmd=""
  45. rmcmd="$rmprog -f"
  46. mvcmd="$mvprog"
  47. src=""
  48. dst=""
  49. dir_arg=""
  50. while [ x"$1" != x ]; do
  51. case $1 in
  52. -c) instcmd="$cpprog"
  53. shift
  54. continue;;
  55. -d) dir_arg=true
  56. shift
  57. continue;;
  58. -m) chmodcmd="$chmodprog $2"
  59. shift
  60. shift
  61. continue;;
  62. -o) chowncmd="$chownprog $2"
  63. shift
  64. shift
  65. continue;;
  66. -g) chgrpcmd="$chgrpprog $2"
  67. shift
  68. shift
  69. continue;;
  70. -s) stripcmd="$stripprog"
  71. shift
  72. continue;;
  73. -t=*) transformarg=`echo $1 | sed 's/-t=//'`
  74. shift
  75. continue;;
  76. -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
  77. shift
  78. continue;;
  79. *) if [ x"$src" = x ]
  80. then
  81. src=$1
  82. else
  83. # this colon is to work around a 386BSD /bin/sh bug
  84. :
  85. dst=$1
  86. fi
  87. shift
  88. continue;;
  89. esac
  90. done
  91. if [ x"$src" = x ]
  92. then
  93. echo "install: no input file specified"
  94. exit 1
  95. else
  96. true
  97. fi
  98. if [ x"$dir_arg" != x ]; then
  99. dst=$src
  100. src=""
  101. if [ -d $dst ]; then
  102. instcmd=:
  103. chmodcmd=""
  104. else
  105. instcmd=mkdir
  106. fi
  107. else
  108. # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
  109. # might cause directories to be created, which would be especially bad
  110. # if $src (and thus $dsttmp) contains '*'.
  111. if [ -f $src -o -d $src ]
  112. then
  113. true
  114. else
  115. echo "install: $src does not exist"
  116. exit 1
  117. fi
  118. if [ x"$dst" = x ]
  119. then
  120. echo "install: no destination specified"
  121. exit 1
  122. else
  123. true
  124. fi
  125. # If destination is a directory, append the input filename; if your system
  126. # does not like double slashes in filenames, you may need to add some logic
  127. if [ -d $dst ]
  128. then
  129. dst="$dst"/`basename $src`
  130. else
  131. true
  132. fi
  133. fi
  134. ## this sed command emulates the dirname command
  135. dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
  136. # Make sure that the destination directory exists.
  137. # this part is taken from Noah Friedman's mkinstalldirs script
  138. # Skip lots of stat calls in the usual case.
  139. if [ ! -d "$dstdir" ]; then
  140. defaultIFS='
  141. '
  142. IFS="${IFS-${defaultIFS}}"
  143. oIFS="${IFS}"
  144. # Some sh's can't handle IFS=/ for some reason.
  145. IFS='%'
  146. set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
  147. IFS="${oIFS}"
  148. pathcomp=''
  149. while [ $# -ne 0 ] ; do
  150. pathcomp="${pathcomp}${1}"
  151. shift
  152. if [ ! -d "${pathcomp}" ] ;
  153. then
  154. $mkdirprog "${pathcomp}"
  155. else
  156. true
  157. fi
  158. pathcomp="${pathcomp}/"
  159. done
  160. fi
  161. if [ x"$dir_arg" != x ]
  162. then
  163. $doit $instcmd $dst &&
  164. if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
  165. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
  166. if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
  167. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
  168. else
  169. # If we're going to rename the final executable, determine the name now.
  170. if [ x"$transformarg" = x ]
  171. then
  172. dstfile=`basename $dst`
  173. else
  174. dstfile=`basename $dst $transformbasename |
  175. sed $transformarg`$transformbasename
  176. fi
  177. # don't allow the sed command to completely eliminate the filename
  178. if [ x"$dstfile" = x ]
  179. then
  180. dstfile=`basename $dst`
  181. else
  182. true
  183. fi
  184. # Make a temp file name in the proper directory.
  185. dsttmp=$dstdir/#inst.$$#
  186. # Move or copy the file name to the temp name
  187. $doit $instcmd $src $dsttmp &&
  188. trap "rm -f ${dsttmp}" 0 &&
  189. # and set any options; do chmod last to preserve setuid bits
  190. # If any of these fail, we abort the whole thing. If we want to
  191. # ignore errors from any of these, just make sure not to ignore
  192. # errors from the above "$doit $instcmd $src $dsttmp" command.
  193. if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
  194. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
  195. if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
  196. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
  197. # Now rename the file to the real destination.
  198. $doit $rmcmd -f $dstdir/$dstfile &&
  199. $doit $mvcmd $dsttmp $dstdir/$dstfile
  200. fi &&
  201. exit 0