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