install-sh revision fb4ebca8
129459361Smrg#!/bin/sh
229459361Smrg# install - install a program, script, or datafile
329459361Smrg
4fb4ebca8Smrgscriptversion=2011-11-20.07; # UTC
529459361Smrg
629459361Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
729459361Smrg# later released in X11R6 (xc/config/util/install.sh) with the
829459361Smrg# following copyright and license.
929459361Smrg#
1029459361Smrg# Copyright (C) 1994 X Consortium
1129459361Smrg#
1229459361Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1329459361Smrg# of this software and associated documentation files (the "Software"), to
1429459361Smrg# deal in the Software without restriction, including without limitation the
1529459361Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1629459361Smrg# sell copies of the Software, and to permit persons to whom the Software is
1729459361Smrg# furnished to do so, subject to the following conditions:
1829459361Smrg#
1929459361Smrg# The above copyright notice and this permission notice shall be included in
2029459361Smrg# all copies or substantial portions of the Software.
2129459361Smrg#
2229459361Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2329459361Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2429459361Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2529459361Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2629459361Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2729459361Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2829459361Smrg#
2929459361Smrg# Except as contained in this notice, the name of the X Consortium shall not
3029459361Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3129459361Smrg# ings in this Software without prior written authorization from the X Consor-
3229459361Smrg# tium.
3329459361Smrg#
3429459361Smrg#
3529459361Smrg# FSF changes to this file are in the public domain.
3629459361Smrg#
3729459361Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38fb4ebca8Smrg# 'make' implicit rules from creating a file called install from it
3929459361Smrg# when there is no Makefile.
4029459361Smrg#
4129459361Smrg# This script is compatible with the BSD install script, but was written
4247202d7bSmrg# from scratch.
4347202d7bSmrg
4447202d7bSmrgnl='
4547202d7bSmrg'
4647202d7bSmrgIFS=" ""	$nl"
4729459361Smrg
4829459361Smrg# set DOITPROG to echo to test this script
4929459361Smrg
5029459361Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5147202d7bSmrgdoit=${DOITPROG-}
5247202d7bSmrgif test -z "$doit"; then
5347202d7bSmrg  doit_exec=exec
5447202d7bSmrgelse
5547202d7bSmrg  doit_exec=$doit
5647202d7bSmrgfi
5729459361Smrg
5847202d7bSmrg# Put in absolute file names if you don't have them in your path;
5947202d7bSmrg# or use environment vars.
6047202d7bSmrg
6147202d7bSmrgchgrpprog=${CHGRPPROG-chgrp}
6247202d7bSmrgchmodprog=${CHMODPROG-chmod}
6347202d7bSmrgchownprog=${CHOWNPROG-chown}
6447202d7bSmrgcmpprog=${CMPPROG-cmp}
6547202d7bSmrgcpprog=${CPPROG-cp}
6647202d7bSmrgmkdirprog=${MKDIRPROG-mkdir}
6747202d7bSmrgmvprog=${MVPROG-mv}
6847202d7bSmrgrmprog=${RMPROG-rm}
6947202d7bSmrgstripprog=${STRIPPROG-strip}
7047202d7bSmrg
7147202d7bSmrgposix_glob='?'
7247202d7bSmrginitialize_posix_glob='
7347202d7bSmrg  test "$posix_glob" != "?" || {
7447202d7bSmrg    if (set -f) 2>/dev/null; then
7547202d7bSmrg      posix_glob=
7647202d7bSmrg    else
7747202d7bSmrg      posix_glob=:
7847202d7bSmrg    fi
7947202d7bSmrg  }
8047202d7bSmrg'
8129459361Smrg
8247202d7bSmrgposix_mkdir=
8347202d7bSmrg
8447202d7bSmrg# Desired mode of installed file.
8547202d7bSmrgmode=0755
8629459361Smrg
8729459361Smrgchgrpcmd=
8847202d7bSmrgchmodcmd=$chmodprog
8947202d7bSmrgchowncmd=
9047202d7bSmrgmvcmd=$mvprog
9129459361Smrgrmcmd="$rmprog -f"
9247202d7bSmrgstripcmd=
9347202d7bSmrg
9429459361Smrgsrc=
9529459361Smrgdst=
9629459361Smrgdir_arg=
9747202d7bSmrgdst_arg=
9847202d7bSmrg
9947202d7bSmrgcopy_on_change=false
10029459361Smrgno_target_directory=
10129459361Smrg
10247202d7bSmrgusage="\
10347202d7bSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10429459361Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10529459361Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10629459361Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10729459361Smrg
10829459361SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10929459361SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11029459361SmrgIn the 4th, create DIRECTORIES.
11129459361Smrg
11229459361SmrgOptions:
11347202d7bSmrg     --help     display this help and exit.
11447202d7bSmrg     --version  display version info and exit.
11547202d7bSmrg
11647202d7bSmrg  -c            (ignored)
11747202d7bSmrg  -C            install only if different (preserve the last data modification time)
11847202d7bSmrg  -d            create directories instead of installing files.
11947202d7bSmrg  -g GROUP      $chgrpprog installed files to GROUP.
12047202d7bSmrg  -m MODE       $chmodprog installed files to MODE.
12147202d7bSmrg  -o USER       $chownprog installed files to USER.
12247202d7bSmrg  -s            $stripprog installed files.
12347202d7bSmrg  -t DIRECTORY  install into DIRECTORY.
12447202d7bSmrg  -T            report an error if DSTFILE is a directory.
12529459361Smrg
12629459361SmrgEnvironment variables override the default commands:
12747202d7bSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12847202d7bSmrg  RMPROG STRIPPROG
12929459361Smrg"
13029459361Smrg
13147202d7bSmrgwhile test $# -ne 0; do
13229459361Smrg  case $1 in
13347202d7bSmrg    -c) ;;
13447202d7bSmrg
13547202d7bSmrg    -C) copy_on_change=true;;
13629459361Smrg
13747202d7bSmrg    -d) dir_arg=true;;
13829459361Smrg
13929459361Smrg    -g) chgrpcmd="$chgrpprog $2"
14047202d7bSmrg	shift;;
14129459361Smrg
14229459361Smrg    --help) echo "$usage"; exit $?;;
14329459361Smrg
14447202d7bSmrg    -m) mode=$2
14547202d7bSmrg	case $mode in
14647202d7bSmrg	  *' '* | *'	'* | *'
14747202d7bSmrg'*	  | *'*'* | *'?'* | *'['*)
14847202d7bSmrg	    echo "$0: invalid mode: $mode" >&2
14947202d7bSmrg	    exit 1;;
15047202d7bSmrg	esac
15147202d7bSmrg	shift;;
15229459361Smrg
15329459361Smrg    -o) chowncmd="$chownprog $2"
15447202d7bSmrg	shift;;
15529459361Smrg
15647202d7bSmrg    -s) stripcmd=$stripprog;;
15729459361Smrg
15847202d7bSmrg    -t) dst_arg=$2
159fb4ebca8Smrg	# Protect names problematic for 'test' and other utilities.
1605dd2154eSmrg	case $dst_arg in
1615dd2154eSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1625dd2154eSmrg	esac
16347202d7bSmrg	shift;;
16429459361Smrg
16547202d7bSmrg    -T) no_target_directory=true;;
16629459361Smrg
16729459361Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16829459361Smrg
16947202d7bSmrg    --)	shift
17029459361Smrg	break;;
17147202d7bSmrg
17247202d7bSmrg    -*)	echo "$0: invalid option: $1" >&2
17347202d7bSmrg	exit 1;;
17447202d7bSmrg
17547202d7bSmrg    *)  break;;
17629459361Smrg  esac
17747202d7bSmrg  shift
17829459361Smrgdone
17929459361Smrg
18047202d7bSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
18147202d7bSmrg  # When -d is used, all remaining arguments are directories to create.
18247202d7bSmrg  # When -t is used, the destination is already specified.
18347202d7bSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18447202d7bSmrg  for arg
18547202d7bSmrg  do
18647202d7bSmrg    if test -n "$dst_arg"; then
18747202d7bSmrg      # $@ is not empty: it contains at least $arg.
18847202d7bSmrg      set fnord "$@" "$dst_arg"
18947202d7bSmrg      shift # fnord
19047202d7bSmrg    fi
19147202d7bSmrg    shift # arg
19247202d7bSmrg    dst_arg=$arg
193fb4ebca8Smrg    # Protect names problematic for 'test' and other utilities.
1945dd2154eSmrg    case $dst_arg in
1955dd2154eSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1965dd2154eSmrg    esac
19747202d7bSmrg  done
19847202d7bSmrgfi
19947202d7bSmrg
20047202d7bSmrgif test $# -eq 0; then
20129459361Smrg  if test -z "$dir_arg"; then
20229459361Smrg    echo "$0: no input file specified." >&2
20329459361Smrg    exit 1
20429459361Smrg  fi
205fb4ebca8Smrg  # It's OK to call 'install-sh -d' without argument.
20629459361Smrg  # This can happen when creating conditional directories.
20729459361Smrg  exit 0
20829459361Smrgfi
20929459361Smrg
21047202d7bSmrgif test -z "$dir_arg"; then
2115dd2154eSmrg  do_exit='(exit $ret); exit $ret'
2125dd2154eSmrg  trap "ret=129; $do_exit" 1
2135dd2154eSmrg  trap "ret=130; $do_exit" 2
2145dd2154eSmrg  trap "ret=141; $do_exit" 13
2155dd2154eSmrg  trap "ret=143; $do_exit" 15
21647202d7bSmrg
21747202d7bSmrg  # Set umask so as not to create temps with too-generous modes.
21847202d7bSmrg  # However, 'strip' requires both read and write access to temps.
21947202d7bSmrg  case $mode in
22047202d7bSmrg    # Optimize common cases.
22147202d7bSmrg    *644) cp_umask=133;;
22247202d7bSmrg    *755) cp_umask=22;;
22347202d7bSmrg
22447202d7bSmrg    *[0-7])
22547202d7bSmrg      if test -z "$stripcmd"; then
22647202d7bSmrg	u_plus_rw=
22747202d7bSmrg      else
22847202d7bSmrg	u_plus_rw='% 200'
22947202d7bSmrg      fi
23047202d7bSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23147202d7bSmrg    *)
23247202d7bSmrg      if test -z "$stripcmd"; then
23347202d7bSmrg	u_plus_rw=
23447202d7bSmrg      else
23547202d7bSmrg	u_plus_rw=,u+rw
23647202d7bSmrg      fi
23747202d7bSmrg      cp_umask=$mode$u_plus_rw;;
23847202d7bSmrg  esac
23947202d7bSmrgfi
24047202d7bSmrg
24129459361Smrgfor src
24229459361Smrgdo
243fb4ebca8Smrg  # Protect names problematic for 'test' and other utilities.
24429459361Smrg  case $src in
2455dd2154eSmrg    -* | [=\(\)!]) src=./$src;;
24629459361Smrg  esac
24729459361Smrg
24829459361Smrg  if test -n "$dir_arg"; then
24929459361Smrg    dst=$src
25047202d7bSmrg    dstdir=$dst
25147202d7bSmrg    test -d "$dstdir"
25247202d7bSmrg    dstdir_status=$?
25329459361Smrg  else
25447202d7bSmrg
25529459361Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
25629459361Smrg    # might cause directories to be created, which would be especially bad
25729459361Smrg    # if $src (and thus $dsttmp) contains '*'.
25829459361Smrg    if test ! -f "$src" && test ! -d "$src"; then
25929459361Smrg      echo "$0: $src does not exist." >&2
26029459361Smrg      exit 1
26129459361Smrg    fi
26229459361Smrg
26347202d7bSmrg    if test -z "$dst_arg"; then
26429459361Smrg      echo "$0: no destination specified." >&2
26529459361Smrg      exit 1
26629459361Smrg    fi
26747202d7bSmrg    dst=$dst_arg
26829459361Smrg
26929459361Smrg    # If destination is a directory, append the input filename; won't work
27029459361Smrg    # if double slashes aren't ignored.
27129459361Smrg    if test -d "$dst"; then
27229459361Smrg      if test -n "$no_target_directory"; then
27347202d7bSmrg	echo "$0: $dst_arg: Is a directory" >&2
27429459361Smrg	exit 1
27529459361Smrg      fi
27647202d7bSmrg      dstdir=$dst
27747202d7bSmrg      dst=$dstdir/`basename "$src"`
27847202d7bSmrg      dstdir_status=0
27947202d7bSmrg    else
28047202d7bSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
28147202d7bSmrg      dstdir=`
28247202d7bSmrg	(dirname "$dst") 2>/dev/null ||
28347202d7bSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
28447202d7bSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
28547202d7bSmrg	     X"$dst" : 'X\(//\)$' \| \
28647202d7bSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28747202d7bSmrg	echo X"$dst" |
28847202d7bSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28947202d7bSmrg		   s//\1/
29047202d7bSmrg		   q
29147202d7bSmrg		 }
29247202d7bSmrg		 /^X\(\/\/\)[^/].*/{
29347202d7bSmrg		   s//\1/
29447202d7bSmrg		   q
29547202d7bSmrg		 }
29647202d7bSmrg		 /^X\(\/\/\)$/{
29747202d7bSmrg		   s//\1/
29847202d7bSmrg		   q
29947202d7bSmrg		 }
30047202d7bSmrg		 /^X\(\/\).*/{
30147202d7bSmrg		   s//\1/
30247202d7bSmrg		   q
30347202d7bSmrg		 }
30447202d7bSmrg		 s/.*/./; q'
30547202d7bSmrg      `
30647202d7bSmrg
30747202d7bSmrg      test -d "$dstdir"
30847202d7bSmrg      dstdir_status=$?
30929459361Smrg    fi
31029459361Smrg  fi
31129459361Smrg
31247202d7bSmrg  obsolete_mkdir_used=false
31347202d7bSmrg
31447202d7bSmrg  if test $dstdir_status != 0; then
31547202d7bSmrg    case $posix_mkdir in
31647202d7bSmrg      '')
31747202d7bSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
31847202d7bSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
31947202d7bSmrg	umask=`umask`
32047202d7bSmrg	case $stripcmd.$umask in
32147202d7bSmrg	  # Optimize common cases.
32247202d7bSmrg	  *[2367][2367]) mkdir_umask=$umask;;
32347202d7bSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
32447202d7bSmrg
32547202d7bSmrg	  *[0-7])
32647202d7bSmrg	    mkdir_umask=`expr $umask + 22 \
32747202d7bSmrg	      - $umask % 100 % 40 + $umask % 20 \
32847202d7bSmrg	      - $umask % 10 % 4 + $umask % 2
32947202d7bSmrg	    `;;
33047202d7bSmrg	  *) mkdir_umask=$umask,go-w;;
33147202d7bSmrg	esac
33247202d7bSmrg
33347202d7bSmrg	# With -d, create the new directory with the user-specified mode.
33447202d7bSmrg	# Otherwise, rely on $mkdir_umask.
33547202d7bSmrg	if test -n "$dir_arg"; then
33647202d7bSmrg	  mkdir_mode=-m$mode
33747202d7bSmrg	else
33847202d7bSmrg	  mkdir_mode=
33947202d7bSmrg	fi
34047202d7bSmrg
34147202d7bSmrg	posix_mkdir=false
34247202d7bSmrg	case $umask in
34347202d7bSmrg	  *[123567][0-7][0-7])
34447202d7bSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
34547202d7bSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
34647202d7bSmrg	    ;;
34747202d7bSmrg	  *)
34847202d7bSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34947202d7bSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
35047202d7bSmrg
35147202d7bSmrg	    if (umask $mkdir_umask &&
35247202d7bSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
35347202d7bSmrg	    then
35447202d7bSmrg	      if test -z "$dir_arg" || {
35547202d7bSmrg		   # Check for POSIX incompatibilities with -m.
35647202d7bSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357fb4ebca8Smrg		   # other-writable bit of parent directory when it shouldn't.
35847202d7bSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35947202d7bSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
36047202d7bSmrg		   case $ls_ld_tmpdir in
36147202d7bSmrg		     d????-?r-*) different_mode=700;;
36247202d7bSmrg		     d????-?--*) different_mode=755;;
36347202d7bSmrg		     *) false;;
36447202d7bSmrg		   esac &&
36547202d7bSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
36647202d7bSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36747202d7bSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36847202d7bSmrg		   }
36947202d7bSmrg		 }
37047202d7bSmrg	      then posix_mkdir=:
37147202d7bSmrg	      fi
37247202d7bSmrg	      rmdir "$tmpdir/d" "$tmpdir"
37347202d7bSmrg	    else
37447202d7bSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
37547202d7bSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
37647202d7bSmrg	    fi
37747202d7bSmrg	    trap '' 0;;
37847202d7bSmrg	esac;;
37947202d7bSmrg    esac
38029459361Smrg
38147202d7bSmrg    if
38247202d7bSmrg      $posix_mkdir && (
38347202d7bSmrg	umask $mkdir_umask &&
38447202d7bSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38547202d7bSmrg      )
38647202d7bSmrg    then :
38747202d7bSmrg    else
38829459361Smrg
38947202d7bSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
39047202d7bSmrg      # or it failed possibly due to a race condition.  Create the
39147202d7bSmrg      # directory the slow way, step by step, checking for races as we go.
39229459361Smrg
39347202d7bSmrg      case $dstdir in
39447202d7bSmrg	/*) prefix='/';;
3955dd2154eSmrg	[-=\(\)!]*) prefix='./';;
39647202d7bSmrg	*)  prefix='';;
39747202d7bSmrg      esac
39829459361Smrg
39947202d7bSmrg      eval "$initialize_posix_glob"
40029459361Smrg
40147202d7bSmrg      oIFS=$IFS
40247202d7bSmrg      IFS=/
40347202d7bSmrg      $posix_glob set -f
40447202d7bSmrg      set fnord $dstdir
40529459361Smrg      shift
40647202d7bSmrg      $posix_glob set +f
40747202d7bSmrg      IFS=$oIFS
40847202d7bSmrg
40947202d7bSmrg      prefixes=
41047202d7bSmrg
41147202d7bSmrg      for d
41247202d7bSmrg      do
4135dd2154eSmrg	test X"$d" = X && continue
41447202d7bSmrg
41547202d7bSmrg	prefix=$prefix$d
41647202d7bSmrg	if test -d "$prefix"; then
41747202d7bSmrg	  prefixes=
41847202d7bSmrg	else
41947202d7bSmrg	  if $posix_mkdir; then
42047202d7bSmrg	    (umask=$mkdir_umask &&
42147202d7bSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42247202d7bSmrg	    # Don't fail if two instances are running concurrently.
42347202d7bSmrg	    test -d "$prefix" || exit 1
42447202d7bSmrg	  else
42547202d7bSmrg	    case $prefix in
42647202d7bSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42747202d7bSmrg	      *) qprefix=$prefix;;
42847202d7bSmrg	    esac
42947202d7bSmrg	    prefixes="$prefixes '$qprefix'"
43047202d7bSmrg	  fi
43147202d7bSmrg	fi
43247202d7bSmrg	prefix=$prefix/
43347202d7bSmrg      done
43447202d7bSmrg
43547202d7bSmrg      if test -n "$prefixes"; then
43647202d7bSmrg	# Don't fail if two instances are running concurrently.
43747202d7bSmrg	(umask $mkdir_umask &&
43847202d7bSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43947202d7bSmrg	  test -d "$dstdir" || exit 1
44047202d7bSmrg	obsolete_mkdir_used=true
44129459361Smrg      fi
44247202d7bSmrg    fi
44329459361Smrg  fi
44429459361Smrg
44529459361Smrg  if test -n "$dir_arg"; then
44647202d7bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44747202d7bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44847202d7bSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44947202d7bSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
45029459361Smrg  else
45129459361Smrg
45229459361Smrg    # Make a couple of temp file names in the proper directory.
45329459361Smrg    dsttmp=$dstdir/_inst.$$_
45429459361Smrg    rmtmp=$dstdir/_rm.$$_
45529459361Smrg
45629459361Smrg    # Trap to clean up those temp files at exit.
45729459361Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45829459361Smrg
45929459361Smrg    # Copy the file name to the temp name.
46047202d7bSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
46129459361Smrg
46229459361Smrg    # and set any options; do chmod last to preserve setuid bits.
46329459361Smrg    #
46429459361Smrg    # If any of these fail, we abort the whole thing.  If we want to
46529459361Smrg    # ignore errors from any of these, just make sure not to ignore
46629459361Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46729459361Smrg    #
46847202d7bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46947202d7bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
47047202d7bSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
47147202d7bSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
47247202d7bSmrg
47347202d7bSmrg    # If -C, don't bother to copy if it wouldn't change the file.
47447202d7bSmrg    if $copy_on_change &&
47547202d7bSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
47647202d7bSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47747202d7bSmrg
47847202d7bSmrg       eval "$initialize_posix_glob" &&
47947202d7bSmrg       $posix_glob set -f &&
48047202d7bSmrg       set X $old && old=:$2:$4:$5:$6 &&
48147202d7bSmrg       set X $new && new=:$2:$4:$5:$6 &&
48247202d7bSmrg       $posix_glob set +f &&
48347202d7bSmrg
48447202d7bSmrg       test "$old" = "$new" &&
48547202d7bSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
48647202d7bSmrg    then
48747202d7bSmrg      rm -f "$dsttmp"
48847202d7bSmrg    else
48947202d7bSmrg      # Rename the file to the real destination.
49047202d7bSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
49147202d7bSmrg
49247202d7bSmrg      # The rename failed, perhaps because mv can't rename something else
49347202d7bSmrg      # to itself, or perhaps because mv is so ancient that it does not
49447202d7bSmrg      # support -f.
49547202d7bSmrg      {
49647202d7bSmrg	# Now remove or move aside any old file at destination location.
49747202d7bSmrg	# We try this two ways since rm can't unlink itself on some
49847202d7bSmrg	# systems and the destination file might be busy for other
49947202d7bSmrg	# reasons.  In this case, the final cleanup might fail but the new
50047202d7bSmrg	# file should still install successfully.
50147202d7bSmrg	{
50247202d7bSmrg	  test ! -f "$dst" ||
50347202d7bSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
50447202d7bSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
50547202d7bSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
50647202d7bSmrg	  } ||
50747202d7bSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
50847202d7bSmrg	    (exit 1); exit 1
50947202d7bSmrg	  }
51047202d7bSmrg	} &&
51147202d7bSmrg
51247202d7bSmrg	# Now rename the file to the real destination.
51347202d7bSmrg	$doit $mvcmd "$dsttmp" "$dst"
51447202d7bSmrg      }
51547202d7bSmrg    fi || exit 1
51647202d7bSmrg
51747202d7bSmrg    trap '' 0
51847202d7bSmrg  fi
51929459361Smrgdone
52029459361Smrg
52129459361Smrg# Local variables:
52229459361Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
52329459361Smrg# time-stamp-start: "scriptversion="
52429459361Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
52547202d7bSmrg# time-stamp-time-zone: "UTC"
52647202d7bSmrg# time-stamp-end: "; # UTC"
52729459361Smrg# End:
528