install-sh revision 9e7bcd65
1444c061aSmrg#!/bin/sh
2444c061aSmrg# install - install a program, script, or datafile
3444c061aSmrg
49e7bcd65Smrgscriptversion=2011-11-20.07; # UTC
5444c061aSmrg
6444c061aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7444c061aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8444c061aSmrg# following copyright and license.
9444c061aSmrg#
10444c061aSmrg# Copyright (C) 1994 X Consortium
11444c061aSmrg#
12444c061aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13444c061aSmrg# of this software and associated documentation files (the "Software"), to
14444c061aSmrg# deal in the Software without restriction, including without limitation the
15444c061aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16444c061aSmrg# sell copies of the Software, and to permit persons to whom the Software is
17444c061aSmrg# furnished to do so, subject to the following conditions:
18444c061aSmrg#
19444c061aSmrg# The above copyright notice and this permission notice shall be included in
20444c061aSmrg# all copies or substantial portions of the Software.
21444c061aSmrg#
22444c061aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23444c061aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24444c061aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25444c061aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26444c061aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27444c061aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28444c061aSmrg#
29444c061aSmrg# Except as contained in this notice, the name of the X Consortium shall not
30444c061aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31444c061aSmrg# ings in this Software without prior written authorization from the X Consor-
32444c061aSmrg# tium.
33444c061aSmrg#
34444c061aSmrg#
35444c061aSmrg# FSF changes to this file are in the public domain.
36444c061aSmrg#
37444c061aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389e7bcd65Smrg# 'make' implicit rules from creating a file called install from it
39444c061aSmrg# when there is no Makefile.
40444c061aSmrg#
41444c061aSmrg# This script is compatible with the BSD install script, but was written
422265a131Smrg# from scratch.
432265a131Smrg
442265a131Smrgnl='
452265a131Smrg'
462265a131SmrgIFS=" ""	$nl"
47444c061aSmrg
48444c061aSmrg# set DOITPROG to echo to test this script
49444c061aSmrg
50444c061aSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
512265a131Smrgdoit=${DOITPROG-}
522265a131Smrgif test -z "$doit"; then
532265a131Smrg  doit_exec=exec
542265a131Smrgelse
552265a131Smrg  doit_exec=$doit
562265a131Smrgfi
57444c061aSmrg
582265a131Smrg# Put in absolute file names if you don't have them in your path;
592265a131Smrg# or use environment vars.
602265a131Smrg
612265a131Smrgchgrpprog=${CHGRPPROG-chgrp}
622265a131Smrgchmodprog=${CHMODPROG-chmod}
632265a131Smrgchownprog=${CHOWNPROG-chown}
642265a131Smrgcmpprog=${CMPPROG-cmp}
652265a131Smrgcpprog=${CPPROG-cp}
662265a131Smrgmkdirprog=${MKDIRPROG-mkdir}
672265a131Smrgmvprog=${MVPROG-mv}
682265a131Smrgrmprog=${RMPROG-rm}
692265a131Smrgstripprog=${STRIPPROG-strip}
702265a131Smrg
712265a131Smrgposix_glob='?'
722265a131Smrginitialize_posix_glob='
732265a131Smrg  test "$posix_glob" != "?" || {
742265a131Smrg    if (set -f) 2>/dev/null; then
752265a131Smrg      posix_glob=
762265a131Smrg    else
772265a131Smrg      posix_glob=:
782265a131Smrg    fi
792265a131Smrg  }
802265a131Smrg'
81444c061aSmrg
822265a131Smrgposix_mkdir=
832265a131Smrg
842265a131Smrg# Desired mode of installed file.
852265a131Smrgmode=0755
86444c061aSmrg
87444c061aSmrgchgrpcmd=
882265a131Smrgchmodcmd=$chmodprog
892265a131Smrgchowncmd=
902265a131Smrgmvcmd=$mvprog
91444c061aSmrgrmcmd="$rmprog -f"
922265a131Smrgstripcmd=
932265a131Smrg
94444c061aSmrgsrc=
95444c061aSmrgdst=
96444c061aSmrgdir_arg=
972265a131Smrgdst_arg=
982265a131Smrg
992265a131Smrgcopy_on_change=false
100444c061aSmrgno_target_directory=
101444c061aSmrg
1022265a131Smrgusage="\
1032265a131SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104444c061aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105444c061aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106444c061aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107444c061aSmrg
108444c061aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109444c061aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110444c061aSmrgIn the 4th, create DIRECTORIES.
111444c061aSmrg
112444c061aSmrgOptions:
1132265a131Smrg     --help     display this help and exit.
1142265a131Smrg     --version  display version info and exit.
1152265a131Smrg
1162265a131Smrg  -c            (ignored)
1172265a131Smrg  -C            install only if different (preserve the last data modification time)
1182265a131Smrg  -d            create directories instead of installing files.
1192265a131Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1202265a131Smrg  -m MODE       $chmodprog installed files to MODE.
1212265a131Smrg  -o USER       $chownprog installed files to USER.
1222265a131Smrg  -s            $stripprog installed files.
1232265a131Smrg  -t DIRECTORY  install into DIRECTORY.
1242265a131Smrg  -T            report an error if DSTFILE is a directory.
125444c061aSmrg
126444c061aSmrgEnvironment variables override the default commands:
1272265a131Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282265a131Smrg  RMPROG STRIPPROG
129444c061aSmrg"
130444c061aSmrg
1312265a131Smrgwhile test $# -ne 0; do
132444c061aSmrg  case $1 in
1332265a131Smrg    -c) ;;
1342265a131Smrg
1352265a131Smrg    -C) copy_on_change=true;;
136444c061aSmrg
1372265a131Smrg    -d) dir_arg=true;;
138444c061aSmrg
139444c061aSmrg    -g) chgrpcmd="$chgrpprog $2"
1402265a131Smrg	shift;;
141444c061aSmrg
142444c061aSmrg    --help) echo "$usage"; exit $?;;
143444c061aSmrg
1442265a131Smrg    -m) mode=$2
1452265a131Smrg	case $mode in
1462265a131Smrg	  *' '* | *'	'* | *'
1472265a131Smrg'*	  | *'*'* | *'?'* | *'['*)
1482265a131Smrg	    echo "$0: invalid mode: $mode" >&2
1492265a131Smrg	    exit 1;;
1502265a131Smrg	esac
1512265a131Smrg	shift;;
152444c061aSmrg
153444c061aSmrg    -o) chowncmd="$chownprog $2"
1542265a131Smrg	shift;;
155444c061aSmrg
1562265a131Smrg    -s) stripcmd=$stripprog;;
157444c061aSmrg
1582265a131Smrg    -t) dst_arg=$2
1599e7bcd65Smrg	# Protect names problematic for 'test' and other utilities.
1609e7bcd65Smrg	case $dst_arg in
1619e7bcd65Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1629e7bcd65Smrg	esac
1632265a131Smrg	shift;;
164444c061aSmrg
1652265a131Smrg    -T) no_target_directory=true;;
166444c061aSmrg
167444c061aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
168444c061aSmrg
1692265a131Smrg    --)	shift
170444c061aSmrg	break;;
1712265a131Smrg
1722265a131Smrg    -*)	echo "$0: invalid option: $1" >&2
1732265a131Smrg	exit 1;;
1742265a131Smrg
1752265a131Smrg    *)  break;;
176444c061aSmrg  esac
1772265a131Smrg  shift
178444c061aSmrgdone
179444c061aSmrg
1802265a131Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1812265a131Smrg  # When -d is used, all remaining arguments are directories to create.
1822265a131Smrg  # When -t is used, the destination is already specified.
1832265a131Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1842265a131Smrg  for arg
1852265a131Smrg  do
1862265a131Smrg    if test -n "$dst_arg"; then
1872265a131Smrg      # $@ is not empty: it contains at least $arg.
1882265a131Smrg      set fnord "$@" "$dst_arg"
1892265a131Smrg      shift # fnord
1902265a131Smrg    fi
1912265a131Smrg    shift # arg
1922265a131Smrg    dst_arg=$arg
1939e7bcd65Smrg    # Protect names problematic for 'test' and other utilities.
1949e7bcd65Smrg    case $dst_arg in
1959e7bcd65Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1969e7bcd65Smrg    esac
1972265a131Smrg  done
1982265a131Smrgfi
1992265a131Smrg
2002265a131Smrgif test $# -eq 0; then
201444c061aSmrg  if test -z "$dir_arg"; then
202444c061aSmrg    echo "$0: no input file specified." >&2
203444c061aSmrg    exit 1
204444c061aSmrg  fi
2059e7bcd65Smrg  # It's OK to call 'install-sh -d' without argument.
206444c061aSmrg  # This can happen when creating conditional directories.
207444c061aSmrg  exit 0
208444c061aSmrgfi
209444c061aSmrg
2102265a131Smrgif test -z "$dir_arg"; then
2119e7bcd65Smrg  do_exit='(exit $ret); exit $ret'
2129e7bcd65Smrg  trap "ret=129; $do_exit" 1
2139e7bcd65Smrg  trap "ret=130; $do_exit" 2
2149e7bcd65Smrg  trap "ret=141; $do_exit" 13
2159e7bcd65Smrg  trap "ret=143; $do_exit" 15
2162265a131Smrg
2172265a131Smrg  # Set umask so as not to create temps with too-generous modes.
2182265a131Smrg  # However, 'strip' requires both read and write access to temps.
2192265a131Smrg  case $mode in
2202265a131Smrg    # Optimize common cases.
2212265a131Smrg    *644) cp_umask=133;;
2222265a131Smrg    *755) cp_umask=22;;
2232265a131Smrg
2242265a131Smrg    *[0-7])
2252265a131Smrg      if test -z "$stripcmd"; then
2262265a131Smrg	u_plus_rw=
2272265a131Smrg      else
2282265a131Smrg	u_plus_rw='% 200'
2292265a131Smrg      fi
2302265a131Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2312265a131Smrg    *)
2322265a131Smrg      if test -z "$stripcmd"; then
2332265a131Smrg	u_plus_rw=
2342265a131Smrg      else
2352265a131Smrg	u_plus_rw=,u+rw
2362265a131Smrg      fi
2372265a131Smrg      cp_umask=$mode$u_plus_rw;;
2382265a131Smrg  esac
2392265a131Smrgfi
2402265a131Smrg
241444c061aSmrgfor src
242444c061aSmrgdo
2439e7bcd65Smrg  # Protect names problematic for 'test' and other utilities.
244444c061aSmrg  case $src in
2459e7bcd65Smrg    -* | [=\(\)!]) src=./$src;;
246444c061aSmrg  esac
247444c061aSmrg
248444c061aSmrg  if test -n "$dir_arg"; then
249444c061aSmrg    dst=$src
2502265a131Smrg    dstdir=$dst
2512265a131Smrg    test -d "$dstdir"
2522265a131Smrg    dstdir_status=$?
253444c061aSmrg  else
2542265a131Smrg
255444c061aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256444c061aSmrg    # might cause directories to be created, which would be especially bad
257444c061aSmrg    # if $src (and thus $dsttmp) contains '*'.
258444c061aSmrg    if test ! -f "$src" && test ! -d "$src"; then
259444c061aSmrg      echo "$0: $src does not exist." >&2
260444c061aSmrg      exit 1
261444c061aSmrg    fi
262444c061aSmrg
2632265a131Smrg    if test -z "$dst_arg"; then
264444c061aSmrg      echo "$0: no destination specified." >&2
265444c061aSmrg      exit 1
266444c061aSmrg    fi
2672265a131Smrg    dst=$dst_arg
268444c061aSmrg
269444c061aSmrg    # If destination is a directory, append the input filename; won't work
270444c061aSmrg    # if double slashes aren't ignored.
271444c061aSmrg    if test -d "$dst"; then
272444c061aSmrg      if test -n "$no_target_directory"; then
2732265a131Smrg	echo "$0: $dst_arg: Is a directory" >&2
274444c061aSmrg	exit 1
275444c061aSmrg      fi
2762265a131Smrg      dstdir=$dst
2772265a131Smrg      dst=$dstdir/`basename "$src"`
2782265a131Smrg      dstdir_status=0
2792265a131Smrg    else
2802265a131Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2812265a131Smrg      dstdir=`
2822265a131Smrg	(dirname "$dst") 2>/dev/null ||
2832265a131Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2842265a131Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2852265a131Smrg	     X"$dst" : 'X\(//\)$' \| \
2862265a131Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2872265a131Smrg	echo X"$dst" |
2882265a131Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2892265a131Smrg		   s//\1/
2902265a131Smrg		   q
2912265a131Smrg		 }
2922265a131Smrg		 /^X\(\/\/\)[^/].*/{
2932265a131Smrg		   s//\1/
2942265a131Smrg		   q
2952265a131Smrg		 }
2962265a131Smrg		 /^X\(\/\/\)$/{
2972265a131Smrg		   s//\1/
2982265a131Smrg		   q
2992265a131Smrg		 }
3002265a131Smrg		 /^X\(\/\).*/{
3012265a131Smrg		   s//\1/
3022265a131Smrg		   q
3032265a131Smrg		 }
3042265a131Smrg		 s/.*/./; q'
3052265a131Smrg      `
3062265a131Smrg
3072265a131Smrg      test -d "$dstdir"
3082265a131Smrg      dstdir_status=$?
309444c061aSmrg    fi
310444c061aSmrg  fi
311444c061aSmrg
3122265a131Smrg  obsolete_mkdir_used=false
3132265a131Smrg
3142265a131Smrg  if test $dstdir_status != 0; then
3152265a131Smrg    case $posix_mkdir in
3162265a131Smrg      '')
3172265a131Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3182265a131Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3192265a131Smrg	umask=`umask`
3202265a131Smrg	case $stripcmd.$umask in
3212265a131Smrg	  # Optimize common cases.
3222265a131Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3232265a131Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3242265a131Smrg
3252265a131Smrg	  *[0-7])
3262265a131Smrg	    mkdir_umask=`expr $umask + 22 \
3272265a131Smrg	      - $umask % 100 % 40 + $umask % 20 \
3282265a131Smrg	      - $umask % 10 % 4 + $umask % 2
3292265a131Smrg	    `;;
3302265a131Smrg	  *) mkdir_umask=$umask,go-w;;
3312265a131Smrg	esac
3322265a131Smrg
3332265a131Smrg	# With -d, create the new directory with the user-specified mode.
3342265a131Smrg	# Otherwise, rely on $mkdir_umask.
3352265a131Smrg	if test -n "$dir_arg"; then
3362265a131Smrg	  mkdir_mode=-m$mode
3372265a131Smrg	else
3382265a131Smrg	  mkdir_mode=
3392265a131Smrg	fi
3402265a131Smrg
3412265a131Smrg	posix_mkdir=false
3422265a131Smrg	case $umask in
3432265a131Smrg	  *[123567][0-7][0-7])
3442265a131Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3452265a131Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3462265a131Smrg	    ;;
3472265a131Smrg	  *)
3482265a131Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3492265a131Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3502265a131Smrg
3512265a131Smrg	    if (umask $mkdir_umask &&
3522265a131Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3532265a131Smrg	    then
3542265a131Smrg	      if test -z "$dir_arg" || {
3552265a131Smrg		   # Check for POSIX incompatibilities with -m.
3562265a131Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3579e7bcd65Smrg		   # other-writable bit of parent directory when it shouldn't.
3582265a131Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3592265a131Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3602265a131Smrg		   case $ls_ld_tmpdir in
3612265a131Smrg		     d????-?r-*) different_mode=700;;
3622265a131Smrg		     d????-?--*) different_mode=755;;
3632265a131Smrg		     *) false;;
3642265a131Smrg		   esac &&
3652265a131Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3662265a131Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3672265a131Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3682265a131Smrg		   }
3692265a131Smrg		 }
3702265a131Smrg	      then posix_mkdir=:
3712265a131Smrg	      fi
3722265a131Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3732265a131Smrg	    else
3742265a131Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3752265a131Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3762265a131Smrg	    fi
3772265a131Smrg	    trap '' 0;;
3782265a131Smrg	esac;;
3792265a131Smrg    esac
380444c061aSmrg
3812265a131Smrg    if
3822265a131Smrg      $posix_mkdir && (
3832265a131Smrg	umask $mkdir_umask &&
3842265a131Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3852265a131Smrg      )
3862265a131Smrg    then :
3872265a131Smrg    else
388444c061aSmrg
3892265a131Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3902265a131Smrg      # or it failed possibly due to a race condition.  Create the
3912265a131Smrg      # directory the slow way, step by step, checking for races as we go.
392444c061aSmrg
3932265a131Smrg      case $dstdir in
3942265a131Smrg	/*) prefix='/';;
3959e7bcd65Smrg	[-=\(\)!]*) prefix='./';;
3962265a131Smrg	*)  prefix='';;
3972265a131Smrg      esac
398444c061aSmrg
3992265a131Smrg      eval "$initialize_posix_glob"
400444c061aSmrg
4012265a131Smrg      oIFS=$IFS
4022265a131Smrg      IFS=/
4032265a131Smrg      $posix_glob set -f
4042265a131Smrg      set fnord $dstdir
405444c061aSmrg      shift
4062265a131Smrg      $posix_glob set +f
4072265a131Smrg      IFS=$oIFS
4082265a131Smrg
4092265a131Smrg      prefixes=
4102265a131Smrg
4112265a131Smrg      for d
4122265a131Smrg      do
4139e7bcd65Smrg	test X"$d" = X && continue
4142265a131Smrg
4152265a131Smrg	prefix=$prefix$d
4162265a131Smrg	if test -d "$prefix"; then
4172265a131Smrg	  prefixes=
4182265a131Smrg	else
4192265a131Smrg	  if $posix_mkdir; then
4202265a131Smrg	    (umask=$mkdir_umask &&
4212265a131Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4222265a131Smrg	    # Don't fail if two instances are running concurrently.
4232265a131Smrg	    test -d "$prefix" || exit 1
4242265a131Smrg	  else
4252265a131Smrg	    case $prefix in
4262265a131Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4272265a131Smrg	      *) qprefix=$prefix;;
4282265a131Smrg	    esac
4292265a131Smrg	    prefixes="$prefixes '$qprefix'"
4302265a131Smrg	  fi
4312265a131Smrg	fi
4322265a131Smrg	prefix=$prefix/
4332265a131Smrg      done
4342265a131Smrg
4352265a131Smrg      if test -n "$prefixes"; then
4362265a131Smrg	# Don't fail if two instances are running concurrently.
4372265a131Smrg	(umask $mkdir_umask &&
4382265a131Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4392265a131Smrg	  test -d "$dstdir" || exit 1
4402265a131Smrg	obsolete_mkdir_used=true
441444c061aSmrg      fi
4422265a131Smrg    fi
443444c061aSmrg  fi
444444c061aSmrg
445444c061aSmrg  if test -n "$dir_arg"; then
4462265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4472265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4482265a131Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4492265a131Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450444c061aSmrg  else
451444c061aSmrg
452444c061aSmrg    # Make a couple of temp file names in the proper directory.
453444c061aSmrg    dsttmp=$dstdir/_inst.$$_
454444c061aSmrg    rmtmp=$dstdir/_rm.$$_
455444c061aSmrg
456444c061aSmrg    # Trap to clean up those temp files at exit.
457444c061aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458444c061aSmrg
459444c061aSmrg    # Copy the file name to the temp name.
4602265a131Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461444c061aSmrg
462444c061aSmrg    # and set any options; do chmod last to preserve setuid bits.
463444c061aSmrg    #
464444c061aSmrg    # If any of these fail, we abort the whole thing.  If we want to
465444c061aSmrg    # ignore errors from any of these, just make sure not to ignore
466444c061aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467444c061aSmrg    #
4682265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4692265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4702265a131Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4712265a131Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4722265a131Smrg
4732265a131Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4742265a131Smrg    if $copy_on_change &&
4752265a131Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4762265a131Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4772265a131Smrg
4782265a131Smrg       eval "$initialize_posix_glob" &&
4792265a131Smrg       $posix_glob set -f &&
4802265a131Smrg       set X $old && old=:$2:$4:$5:$6 &&
4812265a131Smrg       set X $new && new=:$2:$4:$5:$6 &&
4822265a131Smrg       $posix_glob set +f &&
4832265a131Smrg
4842265a131Smrg       test "$old" = "$new" &&
4852265a131Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4862265a131Smrg    then
4872265a131Smrg      rm -f "$dsttmp"
4882265a131Smrg    else
4892265a131Smrg      # Rename the file to the real destination.
4902265a131Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4912265a131Smrg
4922265a131Smrg      # The rename failed, perhaps because mv can't rename something else
4932265a131Smrg      # to itself, or perhaps because mv is so ancient that it does not
4942265a131Smrg      # support -f.
4952265a131Smrg      {
4962265a131Smrg	# Now remove or move aside any old file at destination location.
4972265a131Smrg	# We try this two ways since rm can't unlink itself on some
4982265a131Smrg	# systems and the destination file might be busy for other
4992265a131Smrg	# reasons.  In this case, the final cleanup might fail but the new
5002265a131Smrg	# file should still install successfully.
5012265a131Smrg	{
5022265a131Smrg	  test ! -f "$dst" ||
5032265a131Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5042265a131Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5052265a131Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5062265a131Smrg	  } ||
5072265a131Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5082265a131Smrg	    (exit 1); exit 1
5092265a131Smrg	  }
5102265a131Smrg	} &&
5112265a131Smrg
5122265a131Smrg	# Now rename the file to the real destination.
5132265a131Smrg	$doit $mvcmd "$dsttmp" "$dst"
5142265a131Smrg      }
5152265a131Smrg    fi || exit 1
5162265a131Smrg
5172265a131Smrg    trap '' 0
5182265a131Smrg  fi
519444c061aSmrgdone
520444c061aSmrg
521444c061aSmrg# Local variables:
522444c061aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523444c061aSmrg# time-stamp-start: "scriptversion="
524444c061aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5252ae83157Smrg# time-stamp-time-zone: "UTC"
5262ae83157Smrg# time-stamp-end: "; # UTC"
527444c061aSmrg# End:
528