install-sh revision 8255a313
192bb16f8Smrg#!/bin/sh
292bb16f8Smrg# install - install a program, script, or datafile
392bb16f8Smrg
48255a313Smrgscriptversion=2009-04-28.21; # UTC
592bb16f8Smrg
692bb16f8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
792bb16f8Smrg# later released in X11R6 (xc/config/util/install.sh) with the
892bb16f8Smrg# following copyright and license.
992bb16f8Smrg#
1092bb16f8Smrg# Copyright (C) 1994 X Consortium
1192bb16f8Smrg#
1292bb16f8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1392bb16f8Smrg# of this software and associated documentation files (the "Software"), to
1492bb16f8Smrg# deal in the Software without restriction, including without limitation the
1592bb16f8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1692bb16f8Smrg# sell copies of the Software, and to permit persons to whom the Software is
1792bb16f8Smrg# furnished to do so, subject to the following conditions:
1892bb16f8Smrg#
1992bb16f8Smrg# The above copyright notice and this permission notice shall be included in
2092bb16f8Smrg# all copies or substantial portions of the Software.
2192bb16f8Smrg#
2292bb16f8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2392bb16f8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2492bb16f8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2592bb16f8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2692bb16f8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2792bb16f8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2892bb16f8Smrg#
2992bb16f8Smrg# Except as contained in this notice, the name of the X Consortium shall not
3092bb16f8Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3192bb16f8Smrg# ings in this Software without prior written authorization from the X Consor-
3292bb16f8Smrg# tium.
3392bb16f8Smrg#
3492bb16f8Smrg#
3592bb16f8Smrg# FSF changes to this file are in the public domain.
3692bb16f8Smrg#
3792bb16f8Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3892bb16f8Smrg# `make' implicit rules from creating a file called install from it
3992bb16f8Smrg# when there is no Makefile.
4092bb16f8Smrg#
4192bb16f8Smrg# This script is compatible with the BSD install script, but was written
428255a313Smrg# from scratch.
438255a313Smrg
448255a313Smrgnl='
458255a313Smrg'
468255a313SmrgIFS=" ""	$nl"
4792bb16f8Smrg
4892bb16f8Smrg# set DOITPROG to echo to test this script
4992bb16f8Smrg
5092bb16f8Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
518255a313Smrgdoit=${DOITPROG-}
528255a313Smrgif test -z "$doit"; then
538255a313Smrg  doit_exec=exec
548255a313Smrgelse
558255a313Smrg  doit_exec=$doit
568255a313Smrgfi
5792bb16f8Smrg
588255a313Smrg# Put in absolute file names if you don't have them in your path;
598255a313Smrg# or use environment vars.
608255a313Smrg
618255a313Smrgchgrpprog=${CHGRPPROG-chgrp}
628255a313Smrgchmodprog=${CHMODPROG-chmod}
638255a313Smrgchownprog=${CHOWNPROG-chown}
648255a313Smrgcmpprog=${CMPPROG-cmp}
658255a313Smrgcpprog=${CPPROG-cp}
668255a313Smrgmkdirprog=${MKDIRPROG-mkdir}
678255a313Smrgmvprog=${MVPROG-mv}
688255a313Smrgrmprog=${RMPROG-rm}
698255a313Smrgstripprog=${STRIPPROG-strip}
708255a313Smrg
718255a313Smrgposix_glob='?'
728255a313Smrginitialize_posix_glob='
738255a313Smrg  test "$posix_glob" != "?" || {
748255a313Smrg    if (set -f) 2>/dev/null; then
758255a313Smrg      posix_glob=
768255a313Smrg    else
778255a313Smrg      posix_glob=:
788255a313Smrg    fi
798255a313Smrg  }
808255a313Smrg'
8192bb16f8Smrg
828255a313Smrgposix_mkdir=
838255a313Smrg
848255a313Smrg# Desired mode of installed file.
858255a313Smrgmode=0755
8692bb16f8Smrg
8792bb16f8Smrgchgrpcmd=
888255a313Smrgchmodcmd=$chmodprog
898255a313Smrgchowncmd=
908255a313Smrgmvcmd=$mvprog
9192bb16f8Smrgrmcmd="$rmprog -f"
928255a313Smrgstripcmd=
938255a313Smrg
9492bb16f8Smrgsrc=
9592bb16f8Smrgdst=
9692bb16f8Smrgdir_arg=
978255a313Smrgdst_arg=
988255a313Smrg
998255a313Smrgcopy_on_change=false
10092bb16f8Smrgno_target_directory=
10192bb16f8Smrg
1028255a313Smrgusage="\
1038255a313SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10492bb16f8Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10592bb16f8Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10692bb16f8Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10792bb16f8Smrg
10892bb16f8SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10992bb16f8SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11092bb16f8SmrgIn the 4th, create DIRECTORIES.
11192bb16f8Smrg
11292bb16f8SmrgOptions:
1138255a313Smrg     --help     display this help and exit.
1148255a313Smrg     --version  display version info and exit.
1158255a313Smrg
1168255a313Smrg  -c            (ignored)
1178255a313Smrg  -C            install only if different (preserve the last data modification time)
1188255a313Smrg  -d            create directories instead of installing files.
1198255a313Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1208255a313Smrg  -m MODE       $chmodprog installed files to MODE.
1218255a313Smrg  -o USER       $chownprog installed files to USER.
1228255a313Smrg  -s            $stripprog installed files.
1238255a313Smrg  -t DIRECTORY  install into DIRECTORY.
1248255a313Smrg  -T            report an error if DSTFILE is a directory.
12592bb16f8Smrg
12692bb16f8SmrgEnvironment variables override the default commands:
1278255a313Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1288255a313Smrg  RMPROG STRIPPROG
12992bb16f8Smrg"
13092bb16f8Smrg
1318255a313Smrgwhile test $# -ne 0; do
13292bb16f8Smrg  case $1 in
1338255a313Smrg    -c) ;;
1348255a313Smrg
1358255a313Smrg    -C) copy_on_change=true;;
13692bb16f8Smrg
1378255a313Smrg    -d) dir_arg=true;;
13892bb16f8Smrg
13992bb16f8Smrg    -g) chgrpcmd="$chgrpprog $2"
1408255a313Smrg	shift;;
14192bb16f8Smrg
14292bb16f8Smrg    --help) echo "$usage"; exit $?;;
14392bb16f8Smrg
1448255a313Smrg    -m) mode=$2
1458255a313Smrg	case $mode in
1468255a313Smrg	  *' '* | *'	'* | *'
1478255a313Smrg'*	  | *'*'* | *'?'* | *'['*)
1488255a313Smrg	    echo "$0: invalid mode: $mode" >&2
1498255a313Smrg	    exit 1;;
1508255a313Smrg	esac
1518255a313Smrg	shift;;
15292bb16f8Smrg
15392bb16f8Smrg    -o) chowncmd="$chownprog $2"
1548255a313Smrg	shift;;
15592bb16f8Smrg
1568255a313Smrg    -s) stripcmd=$stripprog;;
15792bb16f8Smrg
1588255a313Smrg    -t) dst_arg=$2
1598255a313Smrg	shift;;
16092bb16f8Smrg
1618255a313Smrg    -T) no_target_directory=true;;
16292bb16f8Smrg
16392bb16f8Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16492bb16f8Smrg
1658255a313Smrg    --)	shift
16692bb16f8Smrg	break;;
1678255a313Smrg
1688255a313Smrg    -*)	echo "$0: invalid option: $1" >&2
1698255a313Smrg	exit 1;;
1708255a313Smrg
1718255a313Smrg    *)  break;;
17292bb16f8Smrg  esac
1738255a313Smrg  shift
17492bb16f8Smrgdone
17592bb16f8Smrg
1768255a313Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1778255a313Smrg  # When -d is used, all remaining arguments are directories to create.
1788255a313Smrg  # When -t is used, the destination is already specified.
1798255a313Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1808255a313Smrg  for arg
1818255a313Smrg  do
1828255a313Smrg    if test -n "$dst_arg"; then
1838255a313Smrg      # $@ is not empty: it contains at least $arg.
1848255a313Smrg      set fnord "$@" "$dst_arg"
1858255a313Smrg      shift # fnord
1868255a313Smrg    fi
1878255a313Smrg    shift # arg
1888255a313Smrg    dst_arg=$arg
1898255a313Smrg  done
1908255a313Smrgfi
1918255a313Smrg
1928255a313Smrgif test $# -eq 0; then
19392bb16f8Smrg  if test -z "$dir_arg"; then
19492bb16f8Smrg    echo "$0: no input file specified." >&2
19592bb16f8Smrg    exit 1
19692bb16f8Smrg  fi
19792bb16f8Smrg  # It's OK to call `install-sh -d' without argument.
19892bb16f8Smrg  # This can happen when creating conditional directories.
19992bb16f8Smrg  exit 0
20092bb16f8Smrgfi
20192bb16f8Smrg
2028255a313Smrgif test -z "$dir_arg"; then
2038255a313Smrg  trap '(exit $?); exit' 1 2 13 15
2048255a313Smrg
2058255a313Smrg  # Set umask so as not to create temps with too-generous modes.
2068255a313Smrg  # However, 'strip' requires both read and write access to temps.
2078255a313Smrg  case $mode in
2088255a313Smrg    # Optimize common cases.
2098255a313Smrg    *644) cp_umask=133;;
2108255a313Smrg    *755) cp_umask=22;;
2118255a313Smrg
2128255a313Smrg    *[0-7])
2138255a313Smrg      if test -z "$stripcmd"; then
2148255a313Smrg	u_plus_rw=
2158255a313Smrg      else
2168255a313Smrg	u_plus_rw='% 200'
2178255a313Smrg      fi
2188255a313Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2198255a313Smrg    *)
2208255a313Smrg      if test -z "$stripcmd"; then
2218255a313Smrg	u_plus_rw=
2228255a313Smrg      else
2238255a313Smrg	u_plus_rw=,u+rw
2248255a313Smrg      fi
2258255a313Smrg      cp_umask=$mode$u_plus_rw;;
2268255a313Smrg  esac
2278255a313Smrgfi
2288255a313Smrg
22992bb16f8Smrgfor src
23092bb16f8Smrgdo
23192bb16f8Smrg  # Protect names starting with `-'.
23292bb16f8Smrg  case $src in
2338255a313Smrg    -*) src=./$src;;
23492bb16f8Smrg  esac
23592bb16f8Smrg
23692bb16f8Smrg  if test -n "$dir_arg"; then
23792bb16f8Smrg    dst=$src
2388255a313Smrg    dstdir=$dst
2398255a313Smrg    test -d "$dstdir"
2408255a313Smrg    dstdir_status=$?
24192bb16f8Smrg  else
2428255a313Smrg
24392bb16f8Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24492bb16f8Smrg    # might cause directories to be created, which would be especially bad
24592bb16f8Smrg    # if $src (and thus $dsttmp) contains '*'.
24692bb16f8Smrg    if test ! -f "$src" && test ! -d "$src"; then
24792bb16f8Smrg      echo "$0: $src does not exist." >&2
24892bb16f8Smrg      exit 1
24992bb16f8Smrg    fi
25092bb16f8Smrg
2518255a313Smrg    if test -z "$dst_arg"; then
25292bb16f8Smrg      echo "$0: no destination specified." >&2
25392bb16f8Smrg      exit 1
25492bb16f8Smrg    fi
25592bb16f8Smrg
2568255a313Smrg    dst=$dst_arg
25792bb16f8Smrg    # Protect names starting with `-'.
25892bb16f8Smrg    case $dst in
2598255a313Smrg      -*) dst=./$dst;;
26092bb16f8Smrg    esac
26192bb16f8Smrg
26292bb16f8Smrg    # If destination is a directory, append the input filename; won't work
26392bb16f8Smrg    # if double slashes aren't ignored.
26492bb16f8Smrg    if test -d "$dst"; then
26592bb16f8Smrg      if test -n "$no_target_directory"; then
2668255a313Smrg	echo "$0: $dst_arg: Is a directory" >&2
26792bb16f8Smrg	exit 1
26892bb16f8Smrg      fi
2698255a313Smrg      dstdir=$dst
2708255a313Smrg      dst=$dstdir/`basename "$src"`
2718255a313Smrg      dstdir_status=0
2728255a313Smrg    else
2738255a313Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2748255a313Smrg      dstdir=`
2758255a313Smrg	(dirname "$dst") 2>/dev/null ||
2768255a313Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2778255a313Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2788255a313Smrg	     X"$dst" : 'X\(//\)$' \| \
2798255a313Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2808255a313Smrg	echo X"$dst" |
2818255a313Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2828255a313Smrg		   s//\1/
2838255a313Smrg		   q
2848255a313Smrg		 }
2858255a313Smrg		 /^X\(\/\/\)[^/].*/{
2868255a313Smrg		   s//\1/
2878255a313Smrg		   q
2888255a313Smrg		 }
2898255a313Smrg		 /^X\(\/\/\)$/{
2908255a313Smrg		   s//\1/
2918255a313Smrg		   q
2928255a313Smrg		 }
2938255a313Smrg		 /^X\(\/\).*/{
2948255a313Smrg		   s//\1/
2958255a313Smrg		   q
2968255a313Smrg		 }
2978255a313Smrg		 s/.*/./; q'
2988255a313Smrg      `
2998255a313Smrg
3008255a313Smrg      test -d "$dstdir"
3018255a313Smrg      dstdir_status=$?
30292bb16f8Smrg    fi
30392bb16f8Smrg  fi
30492bb16f8Smrg
3058255a313Smrg  obsolete_mkdir_used=false
3068255a313Smrg
3078255a313Smrg  if test $dstdir_status != 0; then
3088255a313Smrg    case $posix_mkdir in
3098255a313Smrg      '')
3108255a313Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3118255a313Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3128255a313Smrg	umask=`umask`
3138255a313Smrg	case $stripcmd.$umask in
3148255a313Smrg	  # Optimize common cases.
3158255a313Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3168255a313Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3178255a313Smrg
3188255a313Smrg	  *[0-7])
3198255a313Smrg	    mkdir_umask=`expr $umask + 22 \
3208255a313Smrg	      - $umask % 100 % 40 + $umask % 20 \
3218255a313Smrg	      - $umask % 10 % 4 + $umask % 2
3228255a313Smrg	    `;;
3238255a313Smrg	  *) mkdir_umask=$umask,go-w;;
3248255a313Smrg	esac
3258255a313Smrg
3268255a313Smrg	# With -d, create the new directory with the user-specified mode.
3278255a313Smrg	# Otherwise, rely on $mkdir_umask.
3288255a313Smrg	if test -n "$dir_arg"; then
3298255a313Smrg	  mkdir_mode=-m$mode
3308255a313Smrg	else
3318255a313Smrg	  mkdir_mode=
3328255a313Smrg	fi
3338255a313Smrg
3348255a313Smrg	posix_mkdir=false
3358255a313Smrg	case $umask in
3368255a313Smrg	  *[123567][0-7][0-7])
3378255a313Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3388255a313Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3398255a313Smrg	    ;;
3408255a313Smrg	  *)
3418255a313Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3428255a313Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3438255a313Smrg
3448255a313Smrg	    if (umask $mkdir_umask &&
3458255a313Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3468255a313Smrg	    then
3478255a313Smrg	      if test -z "$dir_arg" || {
3488255a313Smrg		   # Check for POSIX incompatibilities with -m.
3498255a313Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3508255a313Smrg		   # other-writeable bit of parent directory when it shouldn't.
3518255a313Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3528255a313Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3538255a313Smrg		   case $ls_ld_tmpdir in
3548255a313Smrg		     d????-?r-*) different_mode=700;;
3558255a313Smrg		     d????-?--*) different_mode=755;;
3568255a313Smrg		     *) false;;
3578255a313Smrg		   esac &&
3588255a313Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3598255a313Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3608255a313Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3618255a313Smrg		   }
3628255a313Smrg		 }
3638255a313Smrg	      then posix_mkdir=:
3648255a313Smrg	      fi
3658255a313Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3668255a313Smrg	    else
3678255a313Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3688255a313Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3698255a313Smrg	    fi
3708255a313Smrg	    trap '' 0;;
3718255a313Smrg	esac;;
3728255a313Smrg    esac
37392bb16f8Smrg
3748255a313Smrg    if
3758255a313Smrg      $posix_mkdir && (
3768255a313Smrg	umask $mkdir_umask &&
3778255a313Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3788255a313Smrg      )
3798255a313Smrg    then :
3808255a313Smrg    else
38192bb16f8Smrg
3828255a313Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3838255a313Smrg      # or it failed possibly due to a race condition.  Create the
3848255a313Smrg      # directory the slow way, step by step, checking for races as we go.
38592bb16f8Smrg
3868255a313Smrg      case $dstdir in
3878255a313Smrg	/*) prefix='/';;
3888255a313Smrg	-*) prefix='./';;
3898255a313Smrg	*)  prefix='';;
3908255a313Smrg      esac
39192bb16f8Smrg
3928255a313Smrg      eval "$initialize_posix_glob"
39392bb16f8Smrg
3948255a313Smrg      oIFS=$IFS
3958255a313Smrg      IFS=/
3968255a313Smrg      $posix_glob set -f
3978255a313Smrg      set fnord $dstdir
39892bb16f8Smrg      shift
3998255a313Smrg      $posix_glob set +f
4008255a313Smrg      IFS=$oIFS
4018255a313Smrg
4028255a313Smrg      prefixes=
4038255a313Smrg
4048255a313Smrg      for d
4058255a313Smrg      do
4068255a313Smrg	test -z "$d" && continue
4078255a313Smrg
4088255a313Smrg	prefix=$prefix$d
4098255a313Smrg	if test -d "$prefix"; then
4108255a313Smrg	  prefixes=
4118255a313Smrg	else
4128255a313Smrg	  if $posix_mkdir; then
4138255a313Smrg	    (umask=$mkdir_umask &&
4148255a313Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4158255a313Smrg	    # Don't fail if two instances are running concurrently.
4168255a313Smrg	    test -d "$prefix" || exit 1
4178255a313Smrg	  else
4188255a313Smrg	    case $prefix in
4198255a313Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4208255a313Smrg	      *) qprefix=$prefix;;
4218255a313Smrg	    esac
4228255a313Smrg	    prefixes="$prefixes '$qprefix'"
4238255a313Smrg	  fi
4248255a313Smrg	fi
4258255a313Smrg	prefix=$prefix/
4268255a313Smrg      done
4278255a313Smrg
4288255a313Smrg      if test -n "$prefixes"; then
4298255a313Smrg	# Don't fail if two instances are running concurrently.
4308255a313Smrg	(umask $mkdir_umask &&
4318255a313Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4328255a313Smrg	  test -d "$dstdir" || exit 1
4338255a313Smrg	obsolete_mkdir_used=true
43492bb16f8Smrg      fi
4358255a313Smrg    fi
43692bb16f8Smrg  fi
43792bb16f8Smrg
43892bb16f8Smrg  if test -n "$dir_arg"; then
4398255a313Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4408255a313Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4418255a313Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4428255a313Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44392bb16f8Smrg  else
44492bb16f8Smrg
44592bb16f8Smrg    # Make a couple of temp file names in the proper directory.
44692bb16f8Smrg    dsttmp=$dstdir/_inst.$$_
44792bb16f8Smrg    rmtmp=$dstdir/_rm.$$_
44892bb16f8Smrg
44992bb16f8Smrg    # Trap to clean up those temp files at exit.
45092bb16f8Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45192bb16f8Smrg
45292bb16f8Smrg    # Copy the file name to the temp name.
4538255a313Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45492bb16f8Smrg
45592bb16f8Smrg    # and set any options; do chmod last to preserve setuid bits.
45692bb16f8Smrg    #
45792bb16f8Smrg    # If any of these fail, we abort the whole thing.  If we want to
45892bb16f8Smrg    # ignore errors from any of these, just make sure not to ignore
45992bb16f8Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46092bb16f8Smrg    #
4618255a313Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4628255a313Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4638255a313Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4648255a313Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4658255a313Smrg
4668255a313Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4678255a313Smrg    if $copy_on_change &&
4688255a313Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4698255a313Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4708255a313Smrg
4718255a313Smrg       eval "$initialize_posix_glob" &&
4728255a313Smrg       $posix_glob set -f &&
4738255a313Smrg       set X $old && old=:$2:$4:$5:$6 &&
4748255a313Smrg       set X $new && new=:$2:$4:$5:$6 &&
4758255a313Smrg       $posix_glob set +f &&
4768255a313Smrg
4778255a313Smrg       test "$old" = "$new" &&
4788255a313Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4798255a313Smrg    then
4808255a313Smrg      rm -f "$dsttmp"
4818255a313Smrg    else
4828255a313Smrg      # Rename the file to the real destination.
4838255a313Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4848255a313Smrg
4858255a313Smrg      # The rename failed, perhaps because mv can't rename something else
4868255a313Smrg      # to itself, or perhaps because mv is so ancient that it does not
4878255a313Smrg      # support -f.
4888255a313Smrg      {
4898255a313Smrg	# Now remove or move aside any old file at destination location.
4908255a313Smrg	# We try this two ways since rm can't unlink itself on some
4918255a313Smrg	# systems and the destination file might be busy for other
4928255a313Smrg	# reasons.  In this case, the final cleanup might fail but the new
4938255a313Smrg	# file should still install successfully.
4948255a313Smrg	{
4958255a313Smrg	  test ! -f "$dst" ||
4968255a313Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4978255a313Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4988255a313Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4998255a313Smrg	  } ||
5008255a313Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5018255a313Smrg	    (exit 1); exit 1
5028255a313Smrg	  }
5038255a313Smrg	} &&
5048255a313Smrg
5058255a313Smrg	# Now rename the file to the real destination.
5068255a313Smrg	$doit $mvcmd "$dsttmp" "$dst"
5078255a313Smrg      }
5088255a313Smrg    fi || exit 1
5098255a313Smrg
5108255a313Smrg    trap '' 0
5118255a313Smrg  fi
51292bb16f8Smrgdone
51392bb16f8Smrg
51492bb16f8Smrg# Local variables:
51592bb16f8Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51692bb16f8Smrg# time-stamp-start: "scriptversion="
51792bb16f8Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5188255a313Smrg# time-stamp-time-zone: "UTC"
5198255a313Smrg# time-stamp-end: "; # UTC"
52092bb16f8Smrg# End:
521