install-sh 4.7 KB

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