install-sh
author Ozkan Sezer <sezeroz@gmail.com>
Thu, 18 Oct 2018 11:55:14 +0300
branchSDL-1.2
changeset 611 b39ee38ead19
parent 285 b3f70e3d7246
child 664 b6b714389ea9
permissions -rwxr-xr-x
use less ancient versions of autofoo scripts
slouken@285
     1
#!/bin/sh
slouken@285
     2
# install - install a program, script, or datafile
slouken@285
     3
sezeroz@611
     4
scriptversion=2011-11-20.07; # UTC
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
sezeroz@611
    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.
sezeroz@611
    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
sezeroz@611
    61
chgrpprog=${CHGRPPROG-chgrp}
sezeroz@611
    62
chmodprog=${CHMODPROG-chmod}
sezeroz@611
    63
chownprog=${CHOWNPROG-chown}
sezeroz@611
    64
cmpprog=${CMPPROG-cmp}
sezeroz@611
    65
cpprog=${CPPROG-cp}
sezeroz@611
    66
mkdirprog=${MKDIRPROG-mkdir}
sezeroz@611
    67
mvprog=${MVPROG-mv}
sezeroz@611
    68
rmprog=${RMPROG-rm}
sezeroz@611
    69
stripprog=${STRIPPROG-strip}
slouken@285
    70
sezeroz@611
    71
posix_glob='?'
sezeroz@611
    72
initialize_posix_glob='
sezeroz@611
    73
  test "$posix_glob" != "?" || {
sezeroz@611
    74
    if (set -f) 2>/dev/null; then
sezeroz@611
    75
      posix_glob=
sezeroz@611
    76
    else
sezeroz@611
    77
      posix_glob=:
sezeroz@611
    78
    fi
sezeroz@611
    79
  }
sezeroz@611
    80
'
sezeroz@611
    81
slouken@285
    82
posix_mkdir=
slouken@285
    83
slouken@285
    84
# Desired mode of installed file.
slouken@285
    85
mode=0755
slouken@285
    86
sezeroz@611
    87
chgrpcmd=
slouken@285
    88
chmodcmd=$chmodprog
slouken@285
    89
chowncmd=
sezeroz@611
    90
mvcmd=$mvprog
sezeroz@611
    91
rmcmd="$rmprog -f"
slouken@285
    92
stripcmd=
sezeroz@611
    93
slouken@285
    94
src=
slouken@285
    95
dst=
slouken@285
    96
dir_arg=
sezeroz@611
    97
dst_arg=
sezeroz@611
    98
sezeroz@611
    99
copy_on_change=false
slouken@285
   100
no_target_directory=
slouken@285
   101
sezeroz@611
   102
usage="\
sezeroz@611
   103
Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
slouken@285
   104
   or: $0 [OPTION]... SRCFILES... DIRECTORY
slouken@285
   105
   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
slouken@285
   106
   or: $0 [OPTION]... -d DIRECTORIES...
slouken@285
   107
slouken@285
   108
In the 1st form, copy SRCFILE to DSTFILE.
slouken@285
   109
In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
slouken@285
   110
In the 4th, create DIRECTORIES.
slouken@285
   111
slouken@285
   112
Options:
sezeroz@611
   113
     --help     display this help and exit.
sezeroz@611
   114
     --version  display version info and exit.
sezeroz@611
   115
sezeroz@611
   116
  -c            (ignored)
sezeroz@611
   117
  -C            install only if different (preserve the last data modification time)
sezeroz@611
   118
  -d            create directories instead of installing files.
sezeroz@611
   119
  -g GROUP      $chgrpprog installed files to GROUP.
sezeroz@611
   120
  -m MODE       $chmodprog installed files to MODE.
sezeroz@611
   121
  -o USER       $chownprog installed files to USER.
sezeroz@611
   122
  -s            $stripprog installed files.
sezeroz@611
   123
  -t DIRECTORY  install into DIRECTORY.
sezeroz@611
   124
  -T            report an error if DSTFILE is a directory.
slouken@285
   125
slouken@285
   126
Environment variables override the default commands:
sezeroz@611
   127
  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
sezeroz@611
   128
  RMPROG STRIPPROG
slouken@285
   129
"
slouken@285
   130
slouken@285
   131
while test $# -ne 0; do
slouken@285
   132
  case $1 in
sezeroz@611
   133
    -c) ;;
slouken@285
   134
sezeroz@611
   135
    -C) copy_on_change=true;;
sezeroz@611
   136
sezeroz@611
   137
    -d) dir_arg=true;;
slouken@285
   138
slouken@285
   139
    -g) chgrpcmd="$chgrpprog $2"
sezeroz@611
   140
	shift;;
slouken@285
   141
slouken@285
   142
    --help) echo "$usage"; exit $?;;
slouken@285
   143
slouken@285
   144
    -m) mode=$2
slouken@285
   145
	case $mode in
slouken@285
   146
	  *' '* | *'	'* | *'
slouken@285
   147
'*	  | *'*'* | *'?'* | *'['*)
slouken@285
   148
	    echo "$0: invalid mode: $mode" >&2
slouken@285
   149
	    exit 1;;
slouken@285
   150
	esac
sezeroz@611
   151
	shift;;
slouken@285
   152
slouken@285
   153
    -o) chowncmd="$chownprog $2"
sezeroz@611
   154
	shift;;
slouken@285
   155
sezeroz@611
   156
    -s) stripcmd=$stripprog;;
slouken@285
   157
sezeroz@611
   158
    -t) dst_arg=$2
sezeroz@611
   159
	# Protect names problematic for 'test' and other utilities.
sezeroz@611
   160
	case $dst_arg in
sezeroz@611
   161
	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
sezeroz@611
   162
	esac
sezeroz@611
   163
	shift;;
slouken@285
   164
sezeroz@611
   165
    -T) no_target_directory=true;;
slouken@285
   166
slouken@285
   167
    --version) echo "$0 $scriptversion"; exit $?;;
slouken@285
   168
slouken@285
   169
    --)	shift
slouken@285
   170
	break;;
slouken@285
   171
slouken@285
   172
    -*)	echo "$0: invalid option: $1" >&2
slouken@285
   173
	exit 1;;
slouken@285
   174
slouken@285
   175
    *)  break;;
slouken@285
   176
  esac
sezeroz@611
   177
  shift
slouken@285
   178
done
slouken@285
   179
sezeroz@611
   180
if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
slouken@285
   181
  # When -d is used, all remaining arguments are directories to create.
slouken@285
   182
  # When -t is used, the destination is already specified.
slouken@285
   183
  # Otherwise, the last argument is the destination.  Remove it from $@.
slouken@285
   184
  for arg
slouken@285
   185
  do
sezeroz@611
   186
    if test -n "$dst_arg"; then
slouken@285
   187
      # $@ is not empty: it contains at least $arg.
sezeroz@611
   188
      set fnord "$@" "$dst_arg"
slouken@285
   189
      shift # fnord
slouken@285
   190
    fi
slouken@285
   191
    shift # arg
sezeroz@611
   192
    dst_arg=$arg
sezeroz@611
   193
    # Protect names problematic for 'test' and other utilities.
sezeroz@611
   194
    case $dst_arg in
sezeroz@611
   195
      -* | [=\(\)!]) dst_arg=./$dst_arg;;
sezeroz@611
   196
    esac
slouken@285
   197
  done
slouken@285
   198
fi
slouken@285
   199
slouken@285
   200
if test $# -eq 0; then
slouken@285
   201
  if test -z "$dir_arg"; then
slouken@285
   202
    echo "$0: no input file specified." >&2
slouken@285
   203
    exit 1
slouken@285
   204
  fi
sezeroz@611
   205
  # It's OK to call 'install-sh -d' without argument.
slouken@285
   206
  # This can happen when creating conditional directories.
slouken@285
   207
  exit 0
slouken@285
   208
fi
slouken@285
   209
slouken@285
   210
if test -z "$dir_arg"; then
sezeroz@611
   211
  do_exit='(exit $ret); exit $ret'
sezeroz@611
   212
  trap "ret=129; $do_exit" 1
sezeroz@611
   213
  trap "ret=130; $do_exit" 2
sezeroz@611
   214
  trap "ret=141; $do_exit" 13
sezeroz@611
   215
  trap "ret=143; $do_exit" 15
slouken@285
   216
slouken@285
   217
  # Set umask so as not to create temps with too-generous modes.
slouken@285
   218
  # However, 'strip' requires both read and write access to temps.
slouken@285
   219
  case $mode in
slouken@285
   220
    # Optimize common cases.
slouken@285
   221
    *644) cp_umask=133;;
slouken@285
   222
    *755) cp_umask=22;;
slouken@285
   223
slouken@285
   224
    *[0-7])
slouken@285
   225
      if test -z "$stripcmd"; then
slouken@285
   226
	u_plus_rw=
slouken@285
   227
      else
slouken@285
   228
	u_plus_rw='% 200'
slouken@285
   229
      fi
slouken@285
   230
      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
slouken@285
   231
    *)
slouken@285
   232
      if test -z "$stripcmd"; then
slouken@285
   233
	u_plus_rw=
slouken@285
   234
      else
slouken@285
   235
	u_plus_rw=,u+rw
slouken@285
   236
      fi
slouken@285
   237
      cp_umask=$mode$u_plus_rw;;
slouken@285
   238
  esac
slouken@285
   239
fi
slouken@285
   240
slouken@285
   241
for src
slouken@285
   242
do
sezeroz@611
   243
  # Protect names problematic for 'test' and other utilities.
slouken@285
   244
  case $src in
sezeroz@611
   245
    -* | [=\(\)!]) src=./$src;;
slouken@285
   246
  esac
slouken@285
   247
slouken@285
   248
  if test -n "$dir_arg"; then
slouken@285
   249
    dst=$src
slouken@285
   250
    dstdir=$dst
slouken@285
   251
    test -d "$dstdir"
slouken@285
   252
    dstdir_status=$?
slouken@285
   253
  else
slouken@285
   254
slouken@285
   255
    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
slouken@285
   256
    # might cause directories to be created, which would be especially bad
slouken@285
   257
    # if $src (and thus $dsttmp) contains '*'.
slouken@285
   258
    if test ! -f "$src" && test ! -d "$src"; then
slouken@285
   259
      echo "$0: $src does not exist." >&2
slouken@285
   260
      exit 1
slouken@285
   261
    fi
slouken@285
   262
sezeroz@611
   263
    if test -z "$dst_arg"; then
slouken@285
   264
      echo "$0: no destination specified." >&2
slouken@285
   265
      exit 1
slouken@285
   266
    fi
sezeroz@611
   267
    dst=$dst_arg
slouken@285
   268
slouken@285
   269
    # If destination is a directory, append the input filename; won't work
slouken@285
   270
    # if double slashes aren't ignored.
slouken@285
   271
    if test -d "$dst"; then
slouken@285
   272
      if test -n "$no_target_directory"; then
sezeroz@611
   273
	echo "$0: $dst_arg: Is a directory" >&2
slouken@285
   274
	exit 1
slouken@285
   275
      fi
slouken@285
   276
      dstdir=$dst
slouken@285
   277
      dst=$dstdir/`basename "$src"`
slouken@285
   278
      dstdir_status=0
slouken@285
   279
    else
slouken@285
   280
      # Prefer dirname, but fall back on a substitute if dirname fails.
slouken@285
   281
      dstdir=`
slouken@285
   282
	(dirname "$dst") 2>/dev/null ||
slouken@285
   283
	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
slouken@285
   284
	     X"$dst" : 'X\(//\)[^/]' \| \
slouken@285
   285
	     X"$dst" : 'X\(//\)$' \| \
slouken@285
   286
	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
slouken@285
   287
	echo X"$dst" |
slouken@285
   288
	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
slouken@285
   289
		   s//\1/
slouken@285
   290
		   q
slouken@285
   291
		 }
slouken@285
   292
		 /^X\(\/\/\)[^/].*/{
slouken@285
   293
		   s//\1/
slouken@285
   294
		   q
slouken@285
   295
		 }
slouken@285
   296
		 /^X\(\/\/\)$/{
slouken@285
   297
		   s//\1/
slouken@285
   298
		   q
slouken@285
   299
		 }
slouken@285
   300
		 /^X\(\/\).*/{
slouken@285
   301
		   s//\1/
slouken@285
   302
		   q
slouken@285
   303
		 }
slouken@285
   304
		 s/.*/./; q'
slouken@285
   305
      `
slouken@285
   306
slouken@285
   307
      test -d "$dstdir"
slouken@285
   308
      dstdir_status=$?
slouken@285
   309
    fi
slouken@285
   310
  fi
slouken@285
   311
slouken@285
   312
  obsolete_mkdir_used=false
slouken@285
   313
slouken@285
   314
  if test $dstdir_status != 0; then
slouken@285
   315
    case $posix_mkdir in
slouken@285
   316
      '')
slouken@285
   317
	# Create intermediate dirs using mode 755 as modified by the umask.
slouken@285
   318
	# This is like FreeBSD 'install' as of 1997-10-28.
slouken@285
   319
	umask=`umask`
slouken@285
   320
	case $stripcmd.$umask in
slouken@285
   321
	  # Optimize common cases.
slouken@285
   322
	  *[2367][2367]) mkdir_umask=$umask;;
slouken@285
   323
	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
slouken@285
   324
slouken@285
   325
	  *[0-7])
slouken@285
   326
	    mkdir_umask=`expr $umask + 22 \
slouken@285
   327
	      - $umask % 100 % 40 + $umask % 20 \
slouken@285
   328
	      - $umask % 10 % 4 + $umask % 2
slouken@285
   329
	    `;;
slouken@285
   330
	  *) mkdir_umask=$umask,go-w;;
slouken@285
   331
	esac
slouken@285
   332
slouken@285
   333
	# With -d, create the new directory with the user-specified mode.
slouken@285
   334
	# Otherwise, rely on $mkdir_umask.
slouken@285
   335
	if test -n "$dir_arg"; then
slouken@285
   336
	  mkdir_mode=-m$mode
slouken@285
   337
	else
slouken@285
   338
	  mkdir_mode=
slouken@285
   339
	fi
slouken@285
   340
slouken@285
   341
	posix_mkdir=false
slouken@285
   342
	case $umask in
slouken@285
   343
	  *[123567][0-7][0-7])
slouken@285
   344
	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
slouken@285
   345
	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
slouken@285
   346
	    ;;
slouken@285
   347
	  *)
slouken@285
   348
	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
slouken@285
   349
	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
slouken@285
   350
slouken@285
   351
	    if (umask $mkdir_umask &&
slouken@285
   352
		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
slouken@285
   353
	    then
slouken@285
   354
	      if test -z "$dir_arg" || {
slouken@285
   355
		   # Check for POSIX incompatibilities with -m.
slouken@285
   356
		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
sezeroz@611
   357
		   # other-writable bit of parent directory when it shouldn't.
slouken@285
   358
		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
slouken@285
   359
		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
slouken@285
   360
		   case $ls_ld_tmpdir in
slouken@285
   361
		     d????-?r-*) different_mode=700;;
slouken@285
   362
		     d????-?--*) different_mode=755;;
slouken@285
   363
		     *) false;;
slouken@285
   364
		   esac &&
slouken@285
   365
		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
slouken@285
   366
		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
slouken@285
   367
		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
slouken@285
   368
		   }
slouken@285
   369
		 }
slouken@285
   370
	      then posix_mkdir=:
slouken@285
   371
	      fi
slouken@285
   372
	      rmdir "$tmpdir/d" "$tmpdir"
slouken@285
   373
	    else
slouken@285
   374
	      # Remove any dirs left behind by ancient mkdir implementations.
slouken@285
   375
	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
slouken@285
   376
	    fi
slouken@285
   377
	    trap '' 0;;
slouken@285
   378
	esac;;
slouken@285
   379
    esac
slouken@285
   380
slouken@285
   381
    if
slouken@285
   382
      $posix_mkdir && (
slouken@285
   383
	umask $mkdir_umask &&
slouken@285
   384
	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
slouken@285
   385
      )
slouken@285
   386
    then :
slouken@285
   387
    else
slouken@285
   388
slouken@285
   389
      # The umask is ridiculous, or mkdir does not conform to POSIX,
slouken@285
   390
      # or it failed possibly due to a race condition.  Create the
slouken@285
   391
      # directory the slow way, step by step, checking for races as we go.
slouken@285
   392
slouken@285
   393
      case $dstdir in
sezeroz@611
   394
	/*) prefix='/';;
sezeroz@611
   395
	[-=\(\)!]*) prefix='./';;
sezeroz@611
   396
	*)  prefix='';;
slouken@285
   397
      esac
slouken@285
   398
sezeroz@611
   399
      eval "$initialize_posix_glob"
slouken@285
   400
slouken@285
   401
      oIFS=$IFS
slouken@285
   402
      IFS=/
sezeroz@611
   403
      $posix_glob set -f
slouken@285
   404
      set fnord $dstdir
slouken@285
   405
      shift
sezeroz@611
   406
      $posix_glob set +f
slouken@285
   407
      IFS=$oIFS
slouken@285
   408
slouken@285
   409
      prefixes=
slouken@285
   410
slouken@285
   411
      for d
slouken@285
   412
      do
sezeroz@611
   413
	test X"$d" = X && continue
slouken@285
   414
slouken@285
   415
	prefix=$prefix$d
slouken@285
   416
	if test -d "$prefix"; then
slouken@285
   417
	  prefixes=
slouken@285
   418
	else
slouken@285
   419
	  if $posix_mkdir; then
slouken@285
   420
	    (umask=$mkdir_umask &&
slouken@285
   421
	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
slouken@285
   422
	    # Don't fail if two instances are running concurrently.
slouken@285
   423
	    test -d "$prefix" || exit 1
slouken@285
   424
	  else
slouken@285
   425
	    case $prefix in
slouken@285
   426
	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
slouken@285
   427
	      *) qprefix=$prefix;;
slouken@285
   428
	    esac
slouken@285
   429
	    prefixes="$prefixes '$qprefix'"
slouken@285
   430
	  fi
slouken@285
   431
	fi
slouken@285
   432
	prefix=$prefix/
slouken@285
   433
      done
slouken@285
   434
slouken@285
   435
      if test -n "$prefixes"; then
slouken@285
   436
	# Don't fail if two instances are running concurrently.
slouken@285
   437
	(umask $mkdir_umask &&
slouken@285
   438
	 eval "\$doit_exec \$mkdirprog $prefixes") ||
slouken@285
   439
	  test -d "$dstdir" || exit 1
slouken@285
   440
	obsolete_mkdir_used=true
slouken@285
   441
      fi
slouken@285
   442
    fi
slouken@285
   443
  fi
slouken@285
   444
slouken@285
   445
  if test -n "$dir_arg"; then
slouken@285
   446
    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
slouken@285
   447
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
slouken@285
   448
    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
slouken@285
   449
      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
slouken@285
   450
  else
slouken@285
   451
slouken@285
   452
    # Make a couple of temp file names in the proper directory.
slouken@285
   453
    dsttmp=$dstdir/_inst.$$_
slouken@285
   454
    rmtmp=$dstdir/_rm.$$_
slouken@285
   455
slouken@285
   456
    # Trap to clean up those temp files at exit.
slouken@285
   457
    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
slouken@285
   458
slouken@285
   459
    # Copy the file name to the temp name.
slouken@285
   460
    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
slouken@285
   461
slouken@285
   462
    # and set any options; do chmod last to preserve setuid bits.
slouken@285
   463
    #
slouken@285
   464
    # If any of these fail, we abort the whole thing.  If we want to
slouken@285
   465
    # ignore errors from any of these, just make sure not to ignore
slouken@285
   466
    # errors from the above "$doit $cpprog $src $dsttmp" command.
slouken@285
   467
    #
sezeroz@611
   468
    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
sezeroz@611
   469
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
sezeroz@611
   470
    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
sezeroz@611
   471
    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
slouken@285
   472
sezeroz@611
   473
    # If -C, don't bother to copy if it wouldn't change the file.
sezeroz@611
   474
    if $copy_on_change &&
sezeroz@611
   475
       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
sezeroz@611
   476
       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
slouken@285
   477
sezeroz@611
   478
       eval "$initialize_posix_glob" &&
sezeroz@611
   479
       $posix_glob set -f &&
sezeroz@611
   480
       set X $old && old=:$2:$4:$5:$6 &&
sezeroz@611
   481
       set X $new && new=:$2:$4:$5:$6 &&
sezeroz@611
   482
       $posix_glob set +f &&
slouken@285
   483
sezeroz@611
   484
       test "$old" = "$new" &&
sezeroz@611
   485
       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
sezeroz@611
   486
    then
sezeroz@611
   487
      rm -f "$dsttmp"
sezeroz@611
   488
    else
sezeroz@611
   489
      # Rename the file to the real destination.
sezeroz@611
   490
      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
sezeroz@611
   491
sezeroz@611
   492
      # The rename failed, perhaps because mv can't rename something else
sezeroz@611
   493
      # to itself, or perhaps because mv is so ancient that it does not
sezeroz@611
   494
      # support -f.
sezeroz@611
   495
      {
sezeroz@611
   496
	# Now remove or move aside any old file at destination location.
sezeroz@611
   497
	# We try this two ways since rm can't unlink itself on some
sezeroz@611
   498
	# systems and the destination file might be busy for other
sezeroz@611
   499
	# reasons.  In this case, the final cleanup might fail but the new
sezeroz@611
   500
	# file should still install successfully.
sezeroz@611
   501
	{
sezeroz@611
   502
	  test ! -f "$dst" ||
sezeroz@611
   503
	  $doit $rmcmd -f "$dst" 2>/dev/null ||
sezeroz@611
   504
	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
sezeroz@611
   505
	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
sezeroz@611
   506
	  } ||
sezeroz@611
   507
	  { echo "$0: cannot unlink or rename $dst" >&2
sezeroz@611
   508
	    (exit 1); exit 1
sezeroz@611
   509
	  }
sezeroz@611
   510
	} &&
sezeroz@611
   511
sezeroz@611
   512
	# Now rename the file to the real destination.
sezeroz@611
   513
	$doit $mvcmd "$dsttmp" "$dst"
sezeroz@611
   514
      }
sezeroz@611
   515
    fi || exit 1
slouken@285
   516
slouken@285
   517
    trap '' 0
slouken@285
   518
  fi
slouken@285
   519
done
slouken@285
   520
slouken@285
   521
# Local variables:
slouken@285
   522
# eval: (add-hook 'write-file-hooks 'time-stamp)
slouken@285
   523
# time-stamp-start: "scriptversion="
slouken@285
   524
# time-stamp-format: "%:y-%02m-%02d.%02H"
sezeroz@611
   525
# time-stamp-time-zone: "UTC"
sezeroz@611
   526
# time-stamp-end: "; # UTC"
slouken@285
   527
# End: