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