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