install-sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. else
  103. instcmd=mkdir
  104. fi
  105. else
  106. # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
  107. # might cause directories to be created, which would be especially bad
  108. # if $src (and thus $dsttmp) contains '*'.
  109. if [ -f $src -o -d $src ]
  110. then
  111. true
  112. else
  113. echo "install: $src does not exist"
  114. exit 1
  115. fi
  116. if [ x"$dst" = x ]
  117. then
  118. echo "install: no destination specified"
  119. exit 1
  120. else
  121. true
  122. fi
  123. # If destination is a directory, append the input filename; if your system
  124. # does not like double slashes in filenames, you may need to add some logic
  125. if [ -d $dst ]
  126. then
  127. dst="$dst"/`basename $src`
  128. else
  129. true
  130. fi
  131. fi
  132. ## this sed command emulates the dirname command
  133. dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
  134. # Make sure that the destination directory exists.
  135. # this part is taken from Noah Friedman's mkinstalldirs script
  136. # Skip lots of stat calls in the usual case.
  137. if [ ! -d "$dstdir" ]; then
  138. defaultIFS='
  139. '
  140. IFS="${IFS-${defaultIFS}}"
  141. oIFS="${IFS}"
  142. # Some sh's can't handle IFS=/ for some reason.
  143. IFS='%'
  144. set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
  145. IFS="${oIFS}"
  146. pathcomp=''
  147. while [ $# -ne 0 ] ; do
  148. pathcomp="${pathcomp}${1}"
  149. shift
  150. if [ ! -d "${pathcomp}" ] ;
  151. then
  152. $mkdirprog "${pathcomp}"
  153. else
  154. true
  155. fi
  156. pathcomp="${pathcomp}/"
  157. done
  158. fi
  159. if [ x"$dir_arg" != x ]
  160. then
  161. $doit $instcmd $dst &&
  162. if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
  163. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
  164. if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
  165. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
  166. else
  167. # If we're going to rename the final executable, determine the name now.
  168. if [ x"$transformarg" = x ]
  169. then
  170. dstfile=`basename $dst`
  171. else
  172. dstfile=`basename $dst $transformbasename |
  173. sed $transformarg`$transformbasename
  174. fi
  175. # don't allow the sed command to completely eliminate the filename
  176. if [ x"$dstfile" = x ]
  177. then
  178. dstfile=`basename $dst`
  179. else
  180. true
  181. fi
  182. # Make a temp file name in the proper directory.
  183. dsttmp=$dstdir/#inst.$$#
  184. # Move or copy the file name to the temp name
  185. $doit $instcmd $src $dsttmp &&
  186. trap "rm -f ${dsttmp}" 0 &&
  187. # and set any options; do chmod last to preserve setuid bits
  188. # If any of these fail, we abort the whole thing. If we want to
  189. # ignore errors from any of these, just make sure not to ignore
  190. # errors from the above "$doit $instcmd $src $dsttmp" command.
  191. if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
  192. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
  193. if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
  194. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
  195. # Now rename the file to the real destination.
  196. $doit $rmcmd -f $dstdir/$dstfile &&
  197. $doit $mvcmd $dsttmp $dstdir/$dstfile
  198. fi &&
  199. exit 0