annotate install-sh @ 8:a4e5d053daed

Preparation for first release
author Adam Chlipala <adam@chlipala.net>
date Sat, 19 Mar 2011 14:35:11 -0400
parents ad85b8813e8a
children dd5b333a7960
rev   line source
adam@0 1 #!/bin/sh
adam@0 2 # install - install a program, script, or datafile
adam@0 3
adam@0 4 scriptversion=2009-04-28.21; # UTC
adam@0 5
adam@0 6 # This originates from X11R5 (mit/util/scripts/install.sh), which was
adam@0 7 # later released in X11R6 (xc/config/util/install.sh) with the
adam@0 8 # following copyright and license.
adam@0 9 #
adam@0 10 # Copyright (C) 1994 X Consortium
adam@0 11 #
adam@0 12 # Permission is hereby granted, free of charge, to any person obtaining a copy
adam@0 13 # of this software and associated documentation files (the "Software"), to
adam@0 14 # deal in the Software without restriction, including without limitation the
adam@0 15 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
adam@0 16 # sell copies of the Software, and to permit persons to whom the Software is
adam@0 17 # furnished to do so, subject to the following conditions:
adam@0 18 #
adam@0 19 # The above copyright notice and this permission notice shall be included in
adam@0 20 # all copies or substantial portions of the Software.
adam@0 21 #
adam@0 22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
adam@0 23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
adam@0 24 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
adam@0 25 # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
adam@0 26 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
adam@0 27 # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
adam@0 28 #
adam@0 29 # Except as contained in this notice, the name of the X Consortium shall not
adam@0 30 # be used in advertising or otherwise to promote the sale, use or other deal-
adam@0 31 # ings in this Software without prior written authorization from the X Consor-
adam@0 32 # tium.
adam@0 33 #
adam@0 34 #
adam@0 35 # FSF changes to this file are in the public domain.
adam@0 36 #
adam@0 37 # Calling this script install-sh is preferred over install.sh, to prevent
adam@0 38 # `make' implicit rules from creating a file called install from it
adam@0 39 # when there is no Makefile.
adam@0 40 #
adam@0 41 # This script is compatible with the BSD install script, but was written
adam@0 42 # from scratch.
adam@0 43
adam@0 44 nl='
adam@0 45 '
adam@0 46 IFS=" "" $nl"
adam@0 47
adam@0 48 # set DOITPROG to echo to test this script
adam@0 49
adam@0 50 # Don't use :- since 4.3BSD and earlier shells don't like it.
adam@0 51 doit=${DOITPROG-}
adam@0 52 if test -z "$doit"; then
adam@0 53 doit_exec=exec
adam@0 54 else
adam@0 55 doit_exec=$doit
adam@0 56 fi
adam@0 57
adam@0 58 # Put in absolute file names if you don't have them in your path;
adam@0 59 # or use environment vars.
adam@0 60
adam@0 61 chgrpprog=${CHGRPPROG-chgrp}
adam@0 62 chmodprog=${CHMODPROG-chmod}
adam@0 63 chownprog=${CHOWNPROG-chown}
adam@0 64 cmpprog=${CMPPROG-cmp}
adam@0 65 cpprog=${CPPROG-cp}
adam@0 66 mkdirprog=${MKDIRPROG-mkdir}
adam@0 67 mvprog=${MVPROG-mv}
adam@0 68 rmprog=${RMPROG-rm}
adam@0 69 stripprog=${STRIPPROG-strip}
adam@0 70
adam@0 71 posix_glob='?'
adam@0 72 initialize_posix_glob='
adam@0 73 test "$posix_glob" != "?" || {
adam@0 74 if (set -f) 2>/dev/null; then
adam@0 75 posix_glob=
adam@0 76 else
adam@0 77 posix_glob=:
adam@0 78 fi
adam@0 79 }
adam@0 80 '
adam@0 81
adam@0 82 posix_mkdir=
adam@0 83
adam@0 84 # Desired mode of installed file.
adam@0 85 mode=0755
adam@0 86
adam@0 87 chgrpcmd=
adam@0 88 chmodcmd=$chmodprog
adam@0 89 chowncmd=
adam@0 90 mvcmd=$mvprog
adam@0 91 rmcmd="$rmprog -f"
adam@0 92 stripcmd=
adam@0 93
adam@0 94 src=
adam@0 95 dst=
adam@0 96 dir_arg=
adam@0 97 dst_arg=
adam@0 98
adam@0 99 copy_on_change=false
adam@0 100 no_target_directory=
adam@0 101
adam@0 102 usage="\
adam@0 103 Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
adam@0 104 or: $0 [OPTION]... SRCFILES... DIRECTORY
adam@0 105 or: $0 [OPTION]... -t DIRECTORY SRCFILES...
adam@0 106 or: $0 [OPTION]... -d DIRECTORIES...
adam@0 107
adam@0 108 In the 1st form, copy SRCFILE to DSTFILE.
adam@0 109 In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
adam@0 110 In the 4th, create DIRECTORIES.
adam@0 111
adam@0 112 Options:
adam@0 113 --help display this help and exit.
adam@0 114 --version display version info and exit.
adam@0 115
adam@0 116 -c (ignored)
adam@0 117 -C install only if different (preserve the last data modification time)
adam@0 118 -d create directories instead of installing files.
adam@0 119 -g GROUP $chgrpprog installed files to GROUP.
adam@0 120 -m MODE $chmodprog installed files to MODE.
adam@0 121 -o USER $chownprog installed files to USER.
adam@0 122 -s $stripprog installed files.
adam@0 123 -t DIRECTORY install into DIRECTORY.
adam@0 124 -T report an error if DSTFILE is a directory.
adam@0 125
adam@0 126 Environment variables override the default commands:
adam@0 127 CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
adam@0 128 RMPROG STRIPPROG
adam@0 129 "
adam@0 130
adam@0 131 while test $# -ne 0; do
adam@0 132 case $1 in
adam@0 133 -c) ;;
adam@0 134
adam@0 135 -C) copy_on_change=true;;
adam@0 136
adam@0 137 -d) dir_arg=true;;
adam@0 138
adam@0 139 -g) chgrpcmd="$chgrpprog $2"
adam@0 140 shift;;
adam@0 141
adam@0 142 --help) echo "$usage"; exit $?;;
adam@0 143
adam@0 144 -m) mode=$2
adam@0 145 case $mode in
adam@0 146 *' '* | *' '* | *'
adam@0 147 '* | *'*'* | *'?'* | *'['*)
adam@0 148 echo "$0: invalid mode: $mode" >&2
adam@0 149 exit 1;;
adam@0 150 esac
adam@0 151 shift;;
adam@0 152
adam@0 153 -o) chowncmd="$chownprog $2"
adam@0 154 shift;;
adam@0 155
adam@0 156 -s) stripcmd=$stripprog;;
adam@0 157
adam@0 158 -t) dst_arg=$2
adam@0 159 shift;;
adam@0 160
adam@0 161 -T) no_target_directory=true;;
adam@0 162
adam@0 163 --version) echo "$0 $scriptversion"; exit $?;;
adam@0 164
adam@0 165 --) shift
adam@0 166 break;;
adam@0 167
adam@0 168 -*) echo "$0: invalid option: $1" >&2
adam@0 169 exit 1;;
adam@0 170
adam@0 171 *) break;;
adam@0 172 esac
adam@0 173 shift
adam@0 174 done
adam@0 175
adam@0 176 if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
adam@0 177 # When -d is used, all remaining arguments are directories to create.
adam@0 178 # When -t is used, the destination is already specified.
adam@0 179 # Otherwise, the last argument is the destination. Remove it from $@.
adam@0 180 for arg
adam@0 181 do
adam@0 182 if test -n "$dst_arg"; then
adam@0 183 # $@ is not empty: it contains at least $arg.
adam@0 184 set fnord "$@" "$dst_arg"
adam@0 185 shift # fnord
adam@0 186 fi
adam@0 187 shift # arg
adam@0 188 dst_arg=$arg
adam@0 189 done
adam@0 190 fi
adam@0 191
adam@0 192 if test $# -eq 0; then
adam@0 193 if test -z "$dir_arg"; then
adam@0 194 echo "$0: no input file specified." >&2
adam@0 195 exit 1
adam@0 196 fi
adam@0 197 # It's OK to call `install-sh -d' without argument.
adam@0 198 # This can happen when creating conditional directories.
adam@0 199 exit 0
adam@0 200 fi
adam@0 201
adam@0 202 if test -z "$dir_arg"; then
adam@0 203 trap '(exit $?); exit' 1 2 13 15
adam@0 204
adam@0 205 # Set umask so as not to create temps with too-generous modes.
adam@0 206 # However, 'strip' requires both read and write access to temps.
adam@0 207 case $mode in
adam@0 208 # Optimize common cases.
adam@0 209 *644) cp_umask=133;;
adam@0 210 *755) cp_umask=22;;
adam@0 211
adam@0 212 *[0-7])
adam@0 213 if test -z "$stripcmd"; then
adam@0 214 u_plus_rw=
adam@0 215 else
adam@0 216 u_plus_rw='% 200'
adam@0 217 fi
adam@0 218 cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
adam@0 219 *)
adam@0 220 if test -z "$stripcmd"; then
adam@0 221 u_plus_rw=
adam@0 222 else
adam@0 223 u_plus_rw=,u+rw
adam@0 224 fi
adam@0 225 cp_umask=$mode$u_plus_rw;;
adam@0 226 esac
adam@0 227 fi
adam@0 228
adam@0 229 for src
adam@0 230 do
adam@0 231 # Protect names starting with `-'.
adam@0 232 case $src in
adam@0 233 -*) src=./$src;;
adam@0 234 esac
adam@0 235
adam@0 236 if test -n "$dir_arg"; then
adam@0 237 dst=$src
adam@0 238 dstdir=$dst
adam@0 239 test -d "$dstdir"
adam@0 240 dstdir_status=$?
adam@0 241 else
adam@0 242
adam@0 243 # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
adam@0 244 # might cause directories to be created, which would be especially bad
adam@0 245 # if $src (and thus $dsttmp) contains '*'.
adam@0 246 if test ! -f "$src" && test ! -d "$src"; then
adam@0 247 echo "$0: $src does not exist." >&2
adam@0 248 exit 1
adam@0 249 fi
adam@0 250
adam@0 251 if test -z "$dst_arg"; then
adam@0 252 echo "$0: no destination specified." >&2
adam@0 253 exit 1
adam@0 254 fi
adam@0 255
adam@0 256 dst=$dst_arg
adam@0 257 # Protect names starting with `-'.
adam@0 258 case $dst in
adam@0 259 -*) dst=./$dst;;
adam@0 260 esac
adam@0 261
adam@0 262 # If destination is a directory, append the input filename; won't work
adam@0 263 # if double slashes aren't ignored.
adam@0 264 if test -d "$dst"; then
adam@0 265 if test -n "$no_target_directory"; then
adam@0 266 echo "$0: $dst_arg: Is a directory" >&2
adam@0 267 exit 1
adam@0 268 fi
adam@0 269 dstdir=$dst
adam@0 270 dst=$dstdir/`basename "$src"`
adam@0 271 dstdir_status=0
adam@0 272 else
adam@0 273 # Prefer dirname, but fall back on a substitute if dirname fails.
adam@0 274 dstdir=`
adam@0 275 (dirname "$dst") 2>/dev/null ||
adam@0 276 expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
adam@0 277 X"$dst" : 'X\(//\)[^/]' \| \
adam@0 278 X"$dst" : 'X\(//\)$' \| \
adam@0 279 X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
adam@0 280 echo X"$dst" |
adam@0 281 sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
adam@0 282 s//\1/
adam@0 283 q
adam@0 284 }
adam@0 285 /^X\(\/\/\)[^/].*/{
adam@0 286 s//\1/
adam@0 287 q
adam@0 288 }
adam@0 289 /^X\(\/\/\)$/{
adam@0 290 s//\1/
adam@0 291 q
adam@0 292 }
adam@0 293 /^X\(\/\).*/{
adam@0 294 s//\1/
adam@0 295 q
adam@0 296 }
adam@0 297 s/.*/./; q'
adam@0 298 `
adam@0 299
adam@0 300 test -d "$dstdir"
adam@0 301 dstdir_status=$?
adam@0 302 fi
adam@0 303 fi
adam@0 304
adam@0 305 obsolete_mkdir_used=false
adam@0 306
adam@0 307 if test $dstdir_status != 0; then
adam@0 308 case $posix_mkdir in
adam@0 309 '')
adam@0 310 # Create intermediate dirs using mode 755 as modified by the umask.
adam@0 311 # This is like FreeBSD 'install' as of 1997-10-28.
adam@0 312 umask=`umask`
adam@0 313 case $stripcmd.$umask in
adam@0 314 # Optimize common cases.
adam@0 315 *[2367][2367]) mkdir_umask=$umask;;
adam@0 316 .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
adam@0 317
adam@0 318 *[0-7])
adam@0 319 mkdir_umask=`expr $umask + 22 \
adam@0 320 - $umask % 100 % 40 + $umask % 20 \
adam@0 321 - $umask % 10 % 4 + $umask % 2
adam@0 322 `;;
adam@0 323 *) mkdir_umask=$umask,go-w;;
adam@0 324 esac
adam@0 325
adam@0 326 # With -d, create the new directory with the user-specified mode.
adam@0 327 # Otherwise, rely on $mkdir_umask.
adam@0 328 if test -n "$dir_arg"; then
adam@0 329 mkdir_mode=-m$mode
adam@0 330 else
adam@0 331 mkdir_mode=
adam@0 332 fi
adam@0 333
adam@0 334 posix_mkdir=false
adam@0 335 case $umask in
adam@0 336 *[123567][0-7][0-7])
adam@0 337 # POSIX mkdir -p sets u+wx bits regardless of umask, which
adam@0 338 # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
adam@0 339 ;;
adam@0 340 *)
adam@0 341 tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
adam@0 342 trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
adam@0 343
adam@0 344 if (umask $mkdir_umask &&
adam@0 345 exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
adam@0 346 then
adam@0 347 if test -z "$dir_arg" || {
adam@0 348 # Check for POSIX incompatibilities with -m.
adam@0 349 # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
adam@0 350 # other-writeable bit of parent directory when it shouldn't.
adam@0 351 # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
adam@0 352 ls_ld_tmpdir=`ls -ld "$tmpdir"`
adam@0 353 case $ls_ld_tmpdir in
adam@0 354 d????-?r-*) different_mode=700;;
adam@0 355 d????-?--*) different_mode=755;;
adam@0 356 *) false;;
adam@0 357 esac &&
adam@0 358 $mkdirprog -m$different_mode -p -- "$tmpdir" && {
adam@0 359 ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
adam@0 360 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
adam@0 361 }
adam@0 362 }
adam@0 363 then posix_mkdir=:
adam@0 364 fi
adam@0 365 rmdir "$tmpdir/d" "$tmpdir"
adam@0 366 else
adam@0 367 # Remove any dirs left behind by ancient mkdir implementations.
adam@0 368 rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
adam@0 369 fi
adam@0 370 trap '' 0;;
adam@0 371 esac;;
adam@0 372 esac
adam@0 373
adam@0 374 if
adam@0 375 $posix_mkdir && (
adam@0 376 umask $mkdir_umask &&
adam@0 377 $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
adam@0 378 )
adam@0 379 then :
adam@0 380 else
adam@0 381
adam@0 382 # The umask is ridiculous, or mkdir does not conform to POSIX,
adam@0 383 # or it failed possibly due to a race condition. Create the
adam@0 384 # directory the slow way, step by step, checking for races as we go.
adam@0 385
adam@0 386 case $dstdir in
adam@0 387 /*) prefix='/';;
adam@0 388 -*) prefix='./';;
adam@0 389 *) prefix='';;
adam@0 390 esac
adam@0 391
adam@0 392 eval "$initialize_posix_glob"
adam@0 393
adam@0 394 oIFS=$IFS
adam@0 395 IFS=/
adam@0 396 $posix_glob set -f
adam@0 397 set fnord $dstdir
adam@0 398 shift
adam@0 399 $posix_glob set +f
adam@0 400 IFS=$oIFS
adam@0 401
adam@0 402 prefixes=
adam@0 403
adam@0 404 for d
adam@0 405 do
adam@0 406 test -z "$d" && continue
adam@0 407
adam@0 408 prefix=$prefix$d
adam@0 409 if test -d "$prefix"; then
adam@0 410 prefixes=
adam@0 411 else
adam@0 412 if $posix_mkdir; then
adam@0 413 (umask=$mkdir_umask &&
adam@0 414 $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
adam@0 415 # Don't fail if two instances are running concurrently.
adam@0 416 test -d "$prefix" || exit 1
adam@0 417 else
adam@0 418 case $prefix in
adam@0 419 *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
adam@0 420 *) qprefix=$prefix;;
adam@0 421 esac
adam@0 422 prefixes="$prefixes '$qprefix'"
adam@0 423 fi
adam@0 424 fi
adam@0 425 prefix=$prefix/
adam@0 426 done
adam@0 427
adam@0 428 if test -n "$prefixes"; then
adam@0 429 # Don't fail if two instances are running concurrently.
adam@0 430 (umask $mkdir_umask &&
adam@0 431 eval "\$doit_exec \$mkdirprog $prefixes") ||
adam@0 432 test -d "$dstdir" || exit 1
adam@0 433 obsolete_mkdir_used=true
adam@0 434 fi
adam@0 435 fi
adam@0 436 fi
adam@0 437
adam@0 438 if test -n "$dir_arg"; then
adam@0 439 { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
adam@0 440 { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
adam@0 441 { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
adam@0 442 test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
adam@0 443 else
adam@0 444
adam@0 445 # Make a couple of temp file names in the proper directory.
adam@0 446 dsttmp=$dstdir/_inst.$$_
adam@0 447 rmtmp=$dstdir/_rm.$$_
adam@0 448
adam@0 449 # Trap to clean up those temp files at exit.
adam@0 450 trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
adam@0 451
adam@0 452 # Copy the file name to the temp name.
adam@0 453 (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
adam@0 454
adam@0 455 # and set any options; do chmod last to preserve setuid bits.
adam@0 456 #
adam@0 457 # If any of these fail, we abort the whole thing. If we want to
adam@0 458 # ignore errors from any of these, just make sure not to ignore
adam@0 459 # errors from the above "$doit $cpprog $src $dsttmp" command.
adam@0 460 #
adam@0 461 { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
adam@0 462 { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
adam@0 463 { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
adam@0 464 { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
adam@0 465
adam@0 466 # If -C, don't bother to copy if it wouldn't change the file.
adam@0 467 if $copy_on_change &&
adam@0 468 old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
adam@0 469 new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
adam@0 470
adam@0 471 eval "$initialize_posix_glob" &&
adam@0 472 $posix_glob set -f &&
adam@0 473 set X $old && old=:$2:$4:$5:$6 &&
adam@0 474 set X $new && new=:$2:$4:$5:$6 &&
adam@0 475 $posix_glob set +f &&
adam@0 476
adam@0 477 test "$old" = "$new" &&
adam@0 478 $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
adam@0 479 then
adam@0 480 rm -f "$dsttmp"
adam@0 481 else
adam@0 482 # Rename the file to the real destination.
adam@0 483 $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
adam@0 484
adam@0 485 # The rename failed, perhaps because mv can't rename something else
adam@0 486 # to itself, or perhaps because mv is so ancient that it does not
adam@0 487 # support -f.
adam@0 488 {
adam@0 489 # Now remove or move aside any old file at destination location.
adam@0 490 # We try this two ways since rm can't unlink itself on some
adam@0 491 # systems and the destination file might be busy for other
adam@0 492 # reasons. In this case, the final cleanup might fail but the new
adam@0 493 # file should still install successfully.
adam@0 494 {
adam@0 495 test ! -f "$dst" ||
adam@0 496 $doit $rmcmd -f "$dst" 2>/dev/null ||
adam@0 497 { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
adam@0 498 { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
adam@0 499 } ||
adam@0 500 { echo "$0: cannot unlink or rename $dst" >&2
adam@0 501 (exit 1); exit 1
adam@0 502 }
adam@0 503 } &&
adam@0 504
adam@0 505 # Now rename the file to the real destination.
adam@0 506 $doit $mvcmd "$dsttmp" "$dst"
adam@0 507 }
adam@0 508 fi || exit 1
adam@0 509
adam@0 510 trap '' 0
adam@0 511 fi
adam@0 512 done
adam@0 513
adam@0 514 # Local variables:
adam@0 515 # eval: (add-hook 'write-file-hooks 'time-stamp)
adam@0 516 # time-stamp-start: "scriptversion="
adam@0 517 # time-stamp-format: "%:y-%02m-%02d.%02H"
adam@0 518 # time-stamp-time-zone: "UTC"
adam@0 519 # time-stamp-end: "; # UTC"
adam@0 520 # End: