install-sh 5.5 KB

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