install-sh revision 9c125d91
116910e25Smrg#!/bin/sh
216910e25Smrg# install - install a program, script, or datafile
39c125d91Smrg
49c125d91Smrgscriptversion=2009-04-28.21; # UTC
59c125d91Smrg
616910e25Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
716910e25Smrg# later released in X11R6 (xc/config/util/install.sh) with the
816910e25Smrg# following copyright and license.
916910e25Smrg#
1016910e25Smrg# Copyright (C) 1994 X Consortium
1116910e25Smrg#
1216910e25Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1316910e25Smrg# of this software and associated documentation files (the "Software"), to
1416910e25Smrg# deal in the Software without restriction, including without limitation the
1516910e25Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1616910e25Smrg# sell copies of the Software, and to permit persons to whom the Software is
1716910e25Smrg# furnished to do so, subject to the following conditions:
1816910e25Smrg#
1916910e25Smrg# The above copyright notice and this permission notice shall be included in
2016910e25Smrg# all copies or substantial portions of the Software.
2116910e25Smrg#
2216910e25Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2316910e25Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2416910e25Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2516910e25Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2616910e25Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2716910e25Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2816910e25Smrg#
2916910e25Smrg# Except as contained in this notice, the name of the X Consortium shall not
3016910e25Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3116910e25Smrg# ings in this Software without prior written authorization from the X Consor-
3216910e25Smrg# tium.
3316910e25Smrg#
3416910e25Smrg#
3516910e25Smrg# FSF changes to this file are in the public domain.
3616910e25Smrg#
3716910e25Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3816910e25Smrg# `make' implicit rules from creating a file called install from it
3916910e25Smrg# when there is no Makefile.
4016910e25Smrg#
4116910e25Smrg# This script is compatible with the BSD install script, but was written
429c125d91Smrg# from scratch.
4316910e25Smrg
449c125d91Smrgnl='
459c125d91Smrg'
469c125d91SmrgIFS=" ""	$nl"
4716910e25Smrg
4816910e25Smrg# set DOITPROG to echo to test this script
4916910e25Smrg
5016910e25Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
519c125d91Smrgdoit=${DOITPROG-}
529c125d91Smrgif test -z "$doit"; then
539c125d91Smrg  doit_exec=exec
5416910e25Smrgelse
559c125d91Smrg  doit_exec=$doit
5616910e25Smrgfi
5716910e25Smrg
589c125d91Smrg# Put in absolute file names if you don't have them in your path;
599c125d91Smrg# or use environment vars.
609c125d91Smrg
619c125d91Smrgchgrpprog=${CHGRPPROG-chgrp}
629c125d91Smrgchmodprog=${CHMODPROG-chmod}
639c125d91Smrgchownprog=${CHOWNPROG-chown}
649c125d91Smrgcmpprog=${CMPPROG-cmp}
659c125d91Smrgcpprog=${CPPROG-cp}
669c125d91Smrgmkdirprog=${MKDIRPROG-mkdir}
679c125d91Smrgmvprog=${MVPROG-mv}
689c125d91Smrgrmprog=${RMPROG-rm}
699c125d91Smrgstripprog=${STRIPPROG-strip}
709c125d91Smrg
719c125d91Smrgposix_glob='?'
729c125d91Smrginitialize_posix_glob='
739c125d91Smrg  test "$posix_glob" != "?" || {
749c125d91Smrg    if (set -f) 2>/dev/null; then
759c125d91Smrg      posix_glob=
769c125d91Smrg    else
779c125d91Smrg      posix_glob=:
789c125d91Smrg    fi
799c125d91Smrg  }
809c125d91Smrg'
819c125d91Smrg
829c125d91Smrgposix_mkdir=
839c125d91Smrg
849c125d91Smrg# Desired mode of installed file.
859c125d91Smrgmode=0755
869c125d91Smrg
879c125d91Smrgchgrpcmd=
889c125d91Smrgchmodcmd=$chmodprog
899c125d91Smrgchowncmd=
909c125d91Smrgmvcmd=$mvprog
919c125d91Smrgrmcmd="$rmprog -f"
929c125d91Smrgstripcmd=
9316910e25Smrg
949c125d91Smrgsrc=
959c125d91Smrgdst=
969c125d91Smrgdir_arg=
979c125d91Smrgdst_arg=
9816910e25Smrg
999c125d91Smrgcopy_on_change=false
1009c125d91Smrgno_target_directory=
10116910e25Smrg
1029c125d91Smrgusage="\
1039c125d91SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1049c125d91Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1059c125d91Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1069c125d91Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10716910e25Smrg
1089c125d91SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1099c125d91SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1109c125d91SmrgIn the 4th, create DIRECTORIES.
11116910e25Smrg
1129c125d91SmrgOptions:
1139c125d91Smrg     --help     display this help and exit.
1149c125d91Smrg     --version  display version info and exit.
11516910e25Smrg
1169c125d91Smrg  -c            (ignored)
1179c125d91Smrg  -C            install only if different (preserve the last data modification time)
1189c125d91Smrg  -d            create directories instead of installing files.
1199c125d91Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1209c125d91Smrg  -m MODE       $chmodprog installed files to MODE.
1219c125d91Smrg  -o USER       $chownprog installed files to USER.
1229c125d91Smrg  -s            $stripprog installed files.
1239c125d91Smrg  -t DIRECTORY  install into DIRECTORY.
1249c125d91Smrg  -T            report an error if DSTFILE is a directory.
12516910e25Smrg
1269c125d91SmrgEnvironment variables override the default commands:
1279c125d91Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1289c125d91Smrg  RMPROG STRIPPROG
1299c125d91Smrg"
13016910e25Smrg
1319c125d91Smrgwhile test $# -ne 0; do
1329c125d91Smrg  case $1 in
1339c125d91Smrg    -c) ;;
13416910e25Smrg
1359c125d91Smrg    -C) copy_on_change=true;;
13616910e25Smrg
1379c125d91Smrg    -d) dir_arg=true;;
13816910e25Smrg
1399c125d91Smrg    -g) chgrpcmd="$chgrpprog $2"
1409c125d91Smrg	shift;;
14116910e25Smrg
1429c125d91Smrg    --help) echo "$usage"; exit $?;;
14316910e25Smrg
1449c125d91Smrg    -m) mode=$2
1459c125d91Smrg	case $mode in
1469c125d91Smrg	  *' '* | *'	'* | *'
1479c125d91Smrg'*	  | *'*'* | *'?'* | *'['*)
1489c125d91Smrg	    echo "$0: invalid mode: $mode" >&2
1499c125d91Smrg	    exit 1;;
1509c125d91Smrg	esac
1519c125d91Smrg	shift;;
15216910e25Smrg
1539c125d91Smrg    -o) chowncmd="$chownprog $2"
1549c125d91Smrg	shift;;
15516910e25Smrg
1569c125d91Smrg    -s) stripcmd=$stripprog;;
15716910e25Smrg
1589c125d91Smrg    -t) dst_arg=$2
1599c125d91Smrg	shift;;
16016910e25Smrg
1619c125d91Smrg    -T) no_target_directory=true;;
16216910e25Smrg
1639c125d91Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16416910e25Smrg
1659c125d91Smrg    --)	shift
1669c125d91Smrg	break;;
16716910e25Smrg
1689c125d91Smrg    -*)	echo "$0: invalid option: $1" >&2
1699c125d91Smrg	exit 1;;
17016910e25Smrg
1719c125d91Smrg    *)  break;;
1729c125d91Smrg  esac
1739c125d91Smrg  shift
1749c125d91Smrgdone
17516910e25Smrg
1769c125d91Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1779c125d91Smrg  # When -d is used, all remaining arguments are directories to create.
1789c125d91Smrg  # When -t is used, the destination is already specified.
1799c125d91Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1809c125d91Smrg  for arg
1819c125d91Smrg  do
1829c125d91Smrg    if test -n "$dst_arg"; then
1839c125d91Smrg      # $@ is not empty: it contains at least $arg.
1849c125d91Smrg      set fnord "$@" "$dst_arg"
1859c125d91Smrg      shift # fnord
1869c125d91Smrg    fi
1879c125d91Smrg    shift # arg
1889c125d91Smrg    dst_arg=$arg
1899c125d91Smrg  done
1909c125d91Smrgfi
19116910e25Smrg
1929c125d91Smrgif test $# -eq 0; then
1939c125d91Smrg  if test -z "$dir_arg"; then
1949c125d91Smrg    echo "$0: no input file specified." >&2
1959c125d91Smrg    exit 1
1969c125d91Smrg  fi
1979c125d91Smrg  # It's OK to call `install-sh -d' without argument.
1989c125d91Smrg  # This can happen when creating conditional directories.
1999c125d91Smrg  exit 0
2009c125d91Smrgfi
20116910e25Smrg
2029c125d91Smrgif test -z "$dir_arg"; then
2039c125d91Smrg  trap '(exit $?); exit' 1 2 13 15
2049c125d91Smrg
2059c125d91Smrg  # Set umask so as not to create temps with too-generous modes.
2069c125d91Smrg  # However, 'strip' requires both read and write access to temps.
2079c125d91Smrg  case $mode in
2089c125d91Smrg    # Optimize common cases.
2099c125d91Smrg    *644) cp_umask=133;;
2109c125d91Smrg    *755) cp_umask=22;;
2119c125d91Smrg
2129c125d91Smrg    *[0-7])
2139c125d91Smrg      if test -z "$stripcmd"; then
2149c125d91Smrg	u_plus_rw=
2159c125d91Smrg      else
2169c125d91Smrg	u_plus_rw='% 200'
2179c125d91Smrg      fi
2189c125d91Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2199c125d91Smrg    *)
2209c125d91Smrg      if test -z "$stripcmd"; then
2219c125d91Smrg	u_plus_rw=
2229c125d91Smrg      else
2239c125d91Smrg	u_plus_rw=,u+rw
2249c125d91Smrg      fi
2259c125d91Smrg      cp_umask=$mode$u_plus_rw;;
2269c125d91Smrg  esac
2279c125d91Smrgfi
22816910e25Smrg
2299c125d91Smrgfor src
2309c125d91Smrgdo
2319c125d91Smrg  # Protect names starting with `-'.
2329c125d91Smrg  case $src in
2339c125d91Smrg    -*) src=./$src;;
2349c125d91Smrg  esac
2359c125d91Smrg
2369c125d91Smrg  if test -n "$dir_arg"; then
2379c125d91Smrg    dst=$src
2389c125d91Smrg    dstdir=$dst
2399c125d91Smrg    test -d "$dstdir"
2409c125d91Smrg    dstdir_status=$?
2419c125d91Smrg  else
2429c125d91Smrg
2439c125d91Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2449c125d91Smrg    # might cause directories to be created, which would be especially bad
2459c125d91Smrg    # if $src (and thus $dsttmp) contains '*'.
2469c125d91Smrg    if test ! -f "$src" && test ! -d "$src"; then
2479c125d91Smrg      echo "$0: $src does not exist." >&2
2489c125d91Smrg      exit 1
2499c125d91Smrg    fi
2509c125d91Smrg
2519c125d91Smrg    if test -z "$dst_arg"; then
2529c125d91Smrg      echo "$0: no destination specified." >&2
2539c125d91Smrg      exit 1
2549c125d91Smrg    fi
2559c125d91Smrg
2569c125d91Smrg    dst=$dst_arg
2579c125d91Smrg    # Protect names starting with `-'.
2589c125d91Smrg    case $dst in
2599c125d91Smrg      -*) dst=./$dst;;
2609c125d91Smrg    esac
26116910e25Smrg
2629c125d91Smrg    # If destination is a directory, append the input filename; won't work
2639c125d91Smrg    # if double slashes aren't ignored.
2649c125d91Smrg    if test -d "$dst"; then
2659c125d91Smrg      if test -n "$no_target_directory"; then
2669c125d91Smrg	echo "$0: $dst_arg: Is a directory" >&2
2679c125d91Smrg	exit 1
2689c125d91Smrg      fi
2699c125d91Smrg      dstdir=$dst
2709c125d91Smrg      dst=$dstdir/`basename "$src"`
2719c125d91Smrg      dstdir_status=0
2729c125d91Smrg    else
2739c125d91Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2749c125d91Smrg      dstdir=`
2759c125d91Smrg	(dirname "$dst") 2>/dev/null ||
2769c125d91Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2779c125d91Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2789c125d91Smrg	     X"$dst" : 'X\(//\)$' \| \
2799c125d91Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2809c125d91Smrg	echo X"$dst" |
2819c125d91Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2829c125d91Smrg		   s//\1/
2839c125d91Smrg		   q
2849c125d91Smrg		 }
2859c125d91Smrg		 /^X\(\/\/\)[^/].*/{
2869c125d91Smrg		   s//\1/
2879c125d91Smrg		   q
2889c125d91Smrg		 }
2899c125d91Smrg		 /^X\(\/\/\)$/{
2909c125d91Smrg		   s//\1/
2919c125d91Smrg		   q
2929c125d91Smrg		 }
2939c125d91Smrg		 /^X\(\/\).*/{
2949c125d91Smrg		   s//\1/
2959c125d91Smrg		   q
2969c125d91Smrg		 }
2979c125d91Smrg		 s/.*/./; q'
2989c125d91Smrg      `
2999c125d91Smrg
3009c125d91Smrg      test -d "$dstdir"
3019c125d91Smrg      dstdir_status=$?
3029c125d91Smrg    fi
3039c125d91Smrg  fi
3049c125d91Smrg
3059c125d91Smrg  obsolete_mkdir_used=false
3069c125d91Smrg
3079c125d91Smrg  if test $dstdir_status != 0; then
3089c125d91Smrg    case $posix_mkdir in
3099c125d91Smrg      '')
3109c125d91Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3119c125d91Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3129c125d91Smrg	umask=`umask`
3139c125d91Smrg	case $stripcmd.$umask in
3149c125d91Smrg	  # Optimize common cases.
3159c125d91Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3169c125d91Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3179c125d91Smrg
3189c125d91Smrg	  *[0-7])
3199c125d91Smrg	    mkdir_umask=`expr $umask + 22 \
3209c125d91Smrg	      - $umask % 100 % 40 + $umask % 20 \
3219c125d91Smrg	      - $umask % 10 % 4 + $umask % 2
3229c125d91Smrg	    `;;
3239c125d91Smrg	  *) mkdir_umask=$umask,go-w;;
3249c125d91Smrg	esac
3259c125d91Smrg
3269c125d91Smrg	# With -d, create the new directory with the user-specified mode.
3279c125d91Smrg	# Otherwise, rely on $mkdir_umask.
3289c125d91Smrg	if test -n "$dir_arg"; then
3299c125d91Smrg	  mkdir_mode=-m$mode
33016910e25Smrg	else
3319c125d91Smrg	  mkdir_mode=
33216910e25Smrg	fi
33316910e25Smrg
3349c125d91Smrg	posix_mkdir=false
3359c125d91Smrg	case $umask in
3369c125d91Smrg	  *[123567][0-7][0-7])
3379c125d91Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3389c125d91Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3399c125d91Smrg	    ;;
3409c125d91Smrg	  *)
3419c125d91Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3429c125d91Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3439c125d91Smrg
3449c125d91Smrg	    if (umask $mkdir_umask &&
3459c125d91Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3469c125d91Smrg	    then
3479c125d91Smrg	      if test -z "$dir_arg" || {
3489c125d91Smrg		   # Check for POSIX incompatibilities with -m.
3499c125d91Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3509c125d91Smrg		   # other-writeable bit of parent directory when it shouldn't.
3519c125d91Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3529c125d91Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3539c125d91Smrg		   case $ls_ld_tmpdir in
3549c125d91Smrg		     d????-?r-*) different_mode=700;;
3559c125d91Smrg		     d????-?--*) different_mode=755;;
3569c125d91Smrg		     *) false;;
3579c125d91Smrg		   esac &&
3589c125d91Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3599c125d91Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3609c125d91Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3619c125d91Smrg		   }
3629c125d91Smrg		 }
3639c125d91Smrg	      then posix_mkdir=:
3649c125d91Smrg	      fi
3659c125d91Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3669c125d91Smrg	    else
3679c125d91Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3689c125d91Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3699c125d91Smrg	    fi
3709c125d91Smrg	    trap '' 0;;
3719c125d91Smrg	esac;;
3729c125d91Smrg    esac
37316910e25Smrg
3749c125d91Smrg    if
3759c125d91Smrg      $posix_mkdir && (
3769c125d91Smrg	umask $mkdir_umask &&
3779c125d91Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3789c125d91Smrg      )
3799c125d91Smrg    then :
3809c125d91Smrg    else
3819c125d91Smrg
3829c125d91Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3839c125d91Smrg      # or it failed possibly due to a race condition.  Create the
3849c125d91Smrg      # directory the slow way, step by step, checking for races as we go.
3859c125d91Smrg
3869c125d91Smrg      case $dstdir in
3879c125d91Smrg	/*) prefix='/';;
3889c125d91Smrg	-*) prefix='./';;
3899c125d91Smrg	*)  prefix='';;
3909c125d91Smrg      esac
3919c125d91Smrg
3929c125d91Smrg      eval "$initialize_posix_glob"
3939c125d91Smrg
3949c125d91Smrg      oIFS=$IFS
3959c125d91Smrg      IFS=/
3969c125d91Smrg      $posix_glob set -f
3979c125d91Smrg      set fnord $dstdir
3989c125d91Smrg      shift
3999c125d91Smrg      $posix_glob set +f
4009c125d91Smrg      IFS=$oIFS
4019c125d91Smrg
4029c125d91Smrg      prefixes=
4039c125d91Smrg
4049c125d91Smrg      for d
4059c125d91Smrg      do
4069c125d91Smrg	test -z "$d" && continue
4079c125d91Smrg
4089c125d91Smrg	prefix=$prefix$d
4099c125d91Smrg	if test -d "$prefix"; then
4109c125d91Smrg	  prefixes=
4119c125d91Smrg	else
4129c125d91Smrg	  if $posix_mkdir; then
4139c125d91Smrg	    (umask=$mkdir_umask &&
4149c125d91Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4159c125d91Smrg	    # Don't fail if two instances are running concurrently.
4169c125d91Smrg	    test -d "$prefix" || exit 1
4179c125d91Smrg	  else
4189c125d91Smrg	    case $prefix in
4199c125d91Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4209c125d91Smrg	      *) qprefix=$prefix;;
4219c125d91Smrg	    esac
4229c125d91Smrg	    prefixes="$prefixes '$qprefix'"
4239c125d91Smrg	  fi
4249c125d91Smrg	fi
4259c125d91Smrg	prefix=$prefix/
4269c125d91Smrg      done
4279c125d91Smrg
4289c125d91Smrg      if test -n "$prefixes"; then
4299c125d91Smrg	# Don't fail if two instances are running concurrently.
4309c125d91Smrg	(umask $mkdir_umask &&
4319c125d91Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4329c125d91Smrg	  test -d "$dstdir" || exit 1
4339c125d91Smrg	obsolete_mkdir_used=true
4349c125d91Smrg      fi
4359c125d91Smrg    fi
4369c125d91Smrg  fi
4379c125d91Smrg
4389c125d91Smrg  if test -n "$dir_arg"; then
4399c125d91Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4409c125d91Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4419c125d91Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4429c125d91Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4439c125d91Smrg  else
4449c125d91Smrg
4459c125d91Smrg    # Make a couple of temp file names in the proper directory.
4469c125d91Smrg    dsttmp=$dstdir/_inst.$$_
4479c125d91Smrg    rmtmp=$dstdir/_rm.$$_
4489c125d91Smrg
4499c125d91Smrg    # Trap to clean up those temp files at exit.
4509c125d91Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4519c125d91Smrg
4529c125d91Smrg    # Copy the file name to the temp name.
4539c125d91Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4549c125d91Smrg
4559c125d91Smrg    # and set any options; do chmod last to preserve setuid bits.
4569c125d91Smrg    #
4579c125d91Smrg    # If any of these fail, we abort the whole thing.  If we want to
4589c125d91Smrg    # ignore errors from any of these, just make sure not to ignore
4599c125d91Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4609c125d91Smrg    #
4619c125d91Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4629c125d91Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4639c125d91Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4649c125d91Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4659c125d91Smrg
4669c125d91Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4679c125d91Smrg    if $copy_on_change &&
4689c125d91Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4699c125d91Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4709c125d91Smrg
4719c125d91Smrg       eval "$initialize_posix_glob" &&
4729c125d91Smrg       $posix_glob set -f &&
4739c125d91Smrg       set X $old && old=:$2:$4:$5:$6 &&
4749c125d91Smrg       set X $new && new=:$2:$4:$5:$6 &&
4759c125d91Smrg       $posix_glob set +f &&
4769c125d91Smrg
4779c125d91Smrg       test "$old" = "$new" &&
4789c125d91Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4799c125d91Smrg    then
4809c125d91Smrg      rm -f "$dsttmp"
4819c125d91Smrg    else
4829c125d91Smrg      # Rename the file to the real destination.
4839c125d91Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4849c125d91Smrg
4859c125d91Smrg      # The rename failed, perhaps because mv can't rename something else
4869c125d91Smrg      # to itself, or perhaps because mv is so ancient that it does not
4879c125d91Smrg      # support -f.
4889c125d91Smrg      {
4899c125d91Smrg	# Now remove or move aside any old file at destination location.
4909c125d91Smrg	# We try this two ways since rm can't unlink itself on some
4919c125d91Smrg	# systems and the destination file might be busy for other
4929c125d91Smrg	# reasons.  In this case, the final cleanup might fail but the new
4939c125d91Smrg	# file should still install successfully.
4949c125d91Smrg	{
4959c125d91Smrg	  test ! -f "$dst" ||
4969c125d91Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4979c125d91Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4989c125d91Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4999c125d91Smrg	  } ||
5009c125d91Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5019c125d91Smrg	    (exit 1); exit 1
5029c125d91Smrg	  }
5039c125d91Smrg	} &&
5049c125d91Smrg
5059c125d91Smrg	# Now rename the file to the real destination.
5069c125d91Smrg	$doit $mvcmd "$dsttmp" "$dst"
5079c125d91Smrg      }
5089c125d91Smrg    fi || exit 1
5099c125d91Smrg
5109c125d91Smrg    trap '' 0
5119c125d91Smrg  fi
5129c125d91Smrgdone
51316910e25Smrg
5149c125d91Smrg# Local variables:
5159c125d91Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5169c125d91Smrg# time-stamp-start: "scriptversion="
5179c125d91Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5189c125d91Smrg# time-stamp-time-zone: "UTC"
5199c125d91Smrg# time-stamp-end: "; # UTC"
5209c125d91Smrg# End:
521