install-sh revision 05a1a286
1d9c7fc6cSmrg#!/bin/sh
2d9c7fc6cSmrg# install - install a program, script, or datafile
3d9c7fc6cSmrg
405a1a286Smrgscriptversion=2009-04-28.21; # UTC
5d9c7fc6cSmrg
6d9c7fc6cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d9c7fc6cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8d9c7fc6cSmrg# following copyright and license.
9d9c7fc6cSmrg#
10d9c7fc6cSmrg# Copyright (C) 1994 X Consortium
11d9c7fc6cSmrg#
12d9c7fc6cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d9c7fc6cSmrg# of this software and associated documentation files (the "Software"), to
14d9c7fc6cSmrg# deal in the Software without restriction, including without limitation the
15d9c7fc6cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d9c7fc6cSmrg# sell copies of the Software, and to permit persons to whom the Software is
17d9c7fc6cSmrg# furnished to do so, subject to the following conditions:
18d9c7fc6cSmrg#
19d9c7fc6cSmrg# The above copyright notice and this permission notice shall be included in
20d9c7fc6cSmrg# all copies or substantial portions of the Software.
21d9c7fc6cSmrg#
22d9c7fc6cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d9c7fc6cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d9c7fc6cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d9c7fc6cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d9c7fc6cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d9c7fc6cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d9c7fc6cSmrg#
29d9c7fc6cSmrg# Except as contained in this notice, the name of the X Consortium shall not
30d9c7fc6cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d9c7fc6cSmrg# ings in this Software without prior written authorization from the X Consor-
32d9c7fc6cSmrg# tium.
33d9c7fc6cSmrg#
34d9c7fc6cSmrg#
35d9c7fc6cSmrg# FSF changes to this file are in the public domain.
36d9c7fc6cSmrg#
37d9c7fc6cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38d9c7fc6cSmrg# `make' implicit rules from creating a file called install from it
39d9c7fc6cSmrg# when there is no Makefile.
40d9c7fc6cSmrg#
41d9c7fc6cSmrg# This script is compatible with the BSD install script, but was written
4205a1a286Smrg# from scratch.
4305a1a286Smrg
4405a1a286Smrgnl='
4505a1a286Smrg'
4605a1a286SmrgIFS=" ""	$nl"
47d9c7fc6cSmrg
48d9c7fc6cSmrg# set DOITPROG to echo to test this script
49d9c7fc6cSmrg
50d9c7fc6cSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5105a1a286Smrgdoit=${DOITPROG-}
5205a1a286Smrgif test -z "$doit"; then
5305a1a286Smrg  doit_exec=exec
5405a1a286Smrgelse
5505a1a286Smrg  doit_exec=$doit
5605a1a286Smrgfi
57d9c7fc6cSmrg
5805a1a286Smrg# Put in absolute file names if you don't have them in your path;
5905a1a286Smrg# or use environment vars.
6005a1a286Smrg
6105a1a286Smrgchgrpprog=${CHGRPPROG-chgrp}
6205a1a286Smrgchmodprog=${CHMODPROG-chmod}
6305a1a286Smrgchownprog=${CHOWNPROG-chown}
6405a1a286Smrgcmpprog=${CMPPROG-cmp}
6505a1a286Smrgcpprog=${CPPROG-cp}
6605a1a286Smrgmkdirprog=${MKDIRPROG-mkdir}
6705a1a286Smrgmvprog=${MVPROG-mv}
6805a1a286Smrgrmprog=${RMPROG-rm}
6905a1a286Smrgstripprog=${STRIPPROG-strip}
7005a1a286Smrg
7105a1a286Smrgposix_glob='?'
7205a1a286Smrginitialize_posix_glob='
7305a1a286Smrg  test "$posix_glob" != "?" || {
7405a1a286Smrg    if (set -f) 2>/dev/null; then
7505a1a286Smrg      posix_glob=
7605a1a286Smrg    else
7705a1a286Smrg      posix_glob=:
7805a1a286Smrg    fi
7905a1a286Smrg  }
8005a1a286Smrg'
81d9c7fc6cSmrg
8205a1a286Smrgposix_mkdir=
8305a1a286Smrg
8405a1a286Smrg# Desired mode of installed file.
8505a1a286Smrgmode=0755
86d9c7fc6cSmrg
87d9c7fc6cSmrgchgrpcmd=
8805a1a286Smrgchmodcmd=$chmodprog
8905a1a286Smrgchowncmd=
9005a1a286Smrgmvcmd=$mvprog
91d9c7fc6cSmrgrmcmd="$rmprog -f"
9205a1a286Smrgstripcmd=
9305a1a286Smrg
94d9c7fc6cSmrgsrc=
95d9c7fc6cSmrgdst=
96d9c7fc6cSmrgdir_arg=
9705a1a286Smrgdst_arg=
9805a1a286Smrg
9905a1a286Smrgcopy_on_change=false
100d9c7fc6cSmrgno_target_directory=
101d9c7fc6cSmrg
10205a1a286Smrgusage="\
10305a1a286SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104d9c7fc6cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105d9c7fc6cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106d9c7fc6cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107d9c7fc6cSmrg
108d9c7fc6cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109d9c7fc6cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110d9c7fc6cSmrgIn the 4th, create DIRECTORIES.
111d9c7fc6cSmrg
112d9c7fc6cSmrgOptions:
11305a1a286Smrg     --help     display this help and exit.
11405a1a286Smrg     --version  display version info and exit.
11505a1a286Smrg
11605a1a286Smrg  -c            (ignored)
11705a1a286Smrg  -C            install only if different (preserve the last data modification time)
11805a1a286Smrg  -d            create directories instead of installing files.
11905a1a286Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12005a1a286Smrg  -m MODE       $chmodprog installed files to MODE.
12105a1a286Smrg  -o USER       $chownprog installed files to USER.
12205a1a286Smrg  -s            $stripprog installed files.
12305a1a286Smrg  -t DIRECTORY  install into DIRECTORY.
12405a1a286Smrg  -T            report an error if DSTFILE is a directory.
125d9c7fc6cSmrg
126d9c7fc6cSmrgEnvironment variables override the default commands:
12705a1a286Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12805a1a286Smrg  RMPROG STRIPPROG
129d9c7fc6cSmrg"
130d9c7fc6cSmrg
13105a1a286Smrgwhile test $# -ne 0; do
132d9c7fc6cSmrg  case $1 in
13305a1a286Smrg    -c) ;;
13405a1a286Smrg
13505a1a286Smrg    -C) copy_on_change=true;;
136d9c7fc6cSmrg
13705a1a286Smrg    -d) dir_arg=true;;
138d9c7fc6cSmrg
139d9c7fc6cSmrg    -g) chgrpcmd="$chgrpprog $2"
14005a1a286Smrg	shift;;
141d9c7fc6cSmrg
142d9c7fc6cSmrg    --help) echo "$usage"; exit $?;;
143d9c7fc6cSmrg
14405a1a286Smrg    -m) mode=$2
14505a1a286Smrg	case $mode in
14605a1a286Smrg	  *' '* | *'	'* | *'
14705a1a286Smrg'*	  | *'*'* | *'?'* | *'['*)
14805a1a286Smrg	    echo "$0: invalid mode: $mode" >&2
14905a1a286Smrg	    exit 1;;
15005a1a286Smrg	esac
15105a1a286Smrg	shift;;
152d9c7fc6cSmrg
153d9c7fc6cSmrg    -o) chowncmd="$chownprog $2"
15405a1a286Smrg	shift;;
155d9c7fc6cSmrg
15605a1a286Smrg    -s) stripcmd=$stripprog;;
157d9c7fc6cSmrg
15805a1a286Smrg    -t) dst_arg=$2
15905a1a286Smrg	shift;;
160d9c7fc6cSmrg
16105a1a286Smrg    -T) no_target_directory=true;;
162d9c7fc6cSmrg
163d9c7fc6cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164d9c7fc6cSmrg
16505a1a286Smrg    --)	shift
166d9c7fc6cSmrg	break;;
16705a1a286Smrg
16805a1a286Smrg    -*)	echo "$0: invalid option: $1" >&2
16905a1a286Smrg	exit 1;;
17005a1a286Smrg
17105a1a286Smrg    *)  break;;
172d9c7fc6cSmrg  esac
17305a1a286Smrg  shift
174d9c7fc6cSmrgdone
175d9c7fc6cSmrg
17605a1a286Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17705a1a286Smrg  # When -d is used, all remaining arguments are directories to create.
17805a1a286Smrg  # When -t is used, the destination is already specified.
17905a1a286Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18005a1a286Smrg  for arg
18105a1a286Smrg  do
18205a1a286Smrg    if test -n "$dst_arg"; then
18305a1a286Smrg      # $@ is not empty: it contains at least $arg.
18405a1a286Smrg      set fnord "$@" "$dst_arg"
18505a1a286Smrg      shift # fnord
18605a1a286Smrg    fi
18705a1a286Smrg    shift # arg
18805a1a286Smrg    dst_arg=$arg
18905a1a286Smrg  done
19005a1a286Smrgfi
19105a1a286Smrg
19205a1a286Smrgif test $# -eq 0; then
193d9c7fc6cSmrg  if test -z "$dir_arg"; then
194d9c7fc6cSmrg    echo "$0: no input file specified." >&2
195d9c7fc6cSmrg    exit 1
196d9c7fc6cSmrg  fi
197d9c7fc6cSmrg  # It's OK to call `install-sh -d' without argument.
198d9c7fc6cSmrg  # This can happen when creating conditional directories.
199d9c7fc6cSmrg  exit 0
200d9c7fc6cSmrgfi
201d9c7fc6cSmrg
20205a1a286Smrgif test -z "$dir_arg"; then
20305a1a286Smrg  trap '(exit $?); exit' 1 2 13 15
20405a1a286Smrg
20505a1a286Smrg  # Set umask so as not to create temps with too-generous modes.
20605a1a286Smrg  # However, 'strip' requires both read and write access to temps.
20705a1a286Smrg  case $mode in
20805a1a286Smrg    # Optimize common cases.
20905a1a286Smrg    *644) cp_umask=133;;
21005a1a286Smrg    *755) cp_umask=22;;
21105a1a286Smrg
21205a1a286Smrg    *[0-7])
21305a1a286Smrg      if test -z "$stripcmd"; then
21405a1a286Smrg	u_plus_rw=
21505a1a286Smrg      else
21605a1a286Smrg	u_plus_rw='% 200'
21705a1a286Smrg      fi
21805a1a286Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21905a1a286Smrg    *)
22005a1a286Smrg      if test -z "$stripcmd"; then
22105a1a286Smrg	u_plus_rw=
22205a1a286Smrg      else
22305a1a286Smrg	u_plus_rw=,u+rw
22405a1a286Smrg      fi
22505a1a286Smrg      cp_umask=$mode$u_plus_rw;;
22605a1a286Smrg  esac
22705a1a286Smrgfi
22805a1a286Smrg
229d9c7fc6cSmrgfor src
230d9c7fc6cSmrgdo
231d9c7fc6cSmrg  # Protect names starting with `-'.
232d9c7fc6cSmrg  case $src in
23305a1a286Smrg    -*) src=./$src;;
234d9c7fc6cSmrg  esac
235d9c7fc6cSmrg
236d9c7fc6cSmrg  if test -n "$dir_arg"; then
237d9c7fc6cSmrg    dst=$src
23805a1a286Smrg    dstdir=$dst
23905a1a286Smrg    test -d "$dstdir"
24005a1a286Smrg    dstdir_status=$?
241d9c7fc6cSmrg  else
24205a1a286Smrg
243d9c7fc6cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244d9c7fc6cSmrg    # might cause directories to be created, which would be especially bad
245d9c7fc6cSmrg    # if $src (and thus $dsttmp) contains '*'.
246d9c7fc6cSmrg    if test ! -f "$src" && test ! -d "$src"; then
247d9c7fc6cSmrg      echo "$0: $src does not exist." >&2
248d9c7fc6cSmrg      exit 1
249d9c7fc6cSmrg    fi
250d9c7fc6cSmrg
25105a1a286Smrg    if test -z "$dst_arg"; then
252d9c7fc6cSmrg      echo "$0: no destination specified." >&2
253d9c7fc6cSmrg      exit 1
254d9c7fc6cSmrg    fi
255d9c7fc6cSmrg
25605a1a286Smrg    dst=$dst_arg
257d9c7fc6cSmrg    # Protect names starting with `-'.
258d9c7fc6cSmrg    case $dst in
25905a1a286Smrg      -*) dst=./$dst;;
260d9c7fc6cSmrg    esac
261d9c7fc6cSmrg
262d9c7fc6cSmrg    # If destination is a directory, append the input filename; won't work
263d9c7fc6cSmrg    # if double slashes aren't ignored.
264d9c7fc6cSmrg    if test -d "$dst"; then
265d9c7fc6cSmrg      if test -n "$no_target_directory"; then
26605a1a286Smrg	echo "$0: $dst_arg: Is a directory" >&2
267d9c7fc6cSmrg	exit 1
268d9c7fc6cSmrg      fi
26905a1a286Smrg      dstdir=$dst
27005a1a286Smrg      dst=$dstdir/`basename "$src"`
27105a1a286Smrg      dstdir_status=0
27205a1a286Smrg    else
27305a1a286Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27405a1a286Smrg      dstdir=`
27505a1a286Smrg	(dirname "$dst") 2>/dev/null ||
27605a1a286Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27705a1a286Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27805a1a286Smrg	     X"$dst" : 'X\(//\)$' \| \
27905a1a286Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28005a1a286Smrg	echo X"$dst" |
28105a1a286Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28205a1a286Smrg		   s//\1/
28305a1a286Smrg		   q
28405a1a286Smrg		 }
28505a1a286Smrg		 /^X\(\/\/\)[^/].*/{
28605a1a286Smrg		   s//\1/
28705a1a286Smrg		   q
28805a1a286Smrg		 }
28905a1a286Smrg		 /^X\(\/\/\)$/{
29005a1a286Smrg		   s//\1/
29105a1a286Smrg		   q
29205a1a286Smrg		 }
29305a1a286Smrg		 /^X\(\/\).*/{
29405a1a286Smrg		   s//\1/
29505a1a286Smrg		   q
29605a1a286Smrg		 }
29705a1a286Smrg		 s/.*/./; q'
29805a1a286Smrg      `
29905a1a286Smrg
30005a1a286Smrg      test -d "$dstdir"
30105a1a286Smrg      dstdir_status=$?
302d9c7fc6cSmrg    fi
303d9c7fc6cSmrg  fi
304d9c7fc6cSmrg
30505a1a286Smrg  obsolete_mkdir_used=false
30605a1a286Smrg
30705a1a286Smrg  if test $dstdir_status != 0; then
30805a1a286Smrg    case $posix_mkdir in
30905a1a286Smrg      '')
31005a1a286Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31105a1a286Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31205a1a286Smrg	umask=`umask`
31305a1a286Smrg	case $stripcmd.$umask in
31405a1a286Smrg	  # Optimize common cases.
31505a1a286Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31605a1a286Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31705a1a286Smrg
31805a1a286Smrg	  *[0-7])
31905a1a286Smrg	    mkdir_umask=`expr $umask + 22 \
32005a1a286Smrg	      - $umask % 100 % 40 + $umask % 20 \
32105a1a286Smrg	      - $umask % 10 % 4 + $umask % 2
32205a1a286Smrg	    `;;
32305a1a286Smrg	  *) mkdir_umask=$umask,go-w;;
32405a1a286Smrg	esac
32505a1a286Smrg
32605a1a286Smrg	# With -d, create the new directory with the user-specified mode.
32705a1a286Smrg	# Otherwise, rely on $mkdir_umask.
32805a1a286Smrg	if test -n "$dir_arg"; then
32905a1a286Smrg	  mkdir_mode=-m$mode
33005a1a286Smrg	else
33105a1a286Smrg	  mkdir_mode=
33205a1a286Smrg	fi
33305a1a286Smrg
33405a1a286Smrg	posix_mkdir=false
33505a1a286Smrg	case $umask in
33605a1a286Smrg	  *[123567][0-7][0-7])
33705a1a286Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33805a1a286Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33905a1a286Smrg	    ;;
34005a1a286Smrg	  *)
34105a1a286Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34205a1a286Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34305a1a286Smrg
34405a1a286Smrg	    if (umask $mkdir_umask &&
34505a1a286Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34605a1a286Smrg	    then
34705a1a286Smrg	      if test -z "$dir_arg" || {
34805a1a286Smrg		   # Check for POSIX incompatibilities with -m.
34905a1a286Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35005a1a286Smrg		   # other-writeable bit of parent directory when it shouldn't.
35105a1a286Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35205a1a286Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35305a1a286Smrg		   case $ls_ld_tmpdir in
35405a1a286Smrg		     d????-?r-*) different_mode=700;;
35505a1a286Smrg		     d????-?--*) different_mode=755;;
35605a1a286Smrg		     *) false;;
35705a1a286Smrg		   esac &&
35805a1a286Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35905a1a286Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36005a1a286Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36105a1a286Smrg		   }
36205a1a286Smrg		 }
36305a1a286Smrg	      then posix_mkdir=:
36405a1a286Smrg	      fi
36505a1a286Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36605a1a286Smrg	    else
36705a1a286Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36805a1a286Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36905a1a286Smrg	    fi
37005a1a286Smrg	    trap '' 0;;
37105a1a286Smrg	esac;;
37205a1a286Smrg    esac
373d9c7fc6cSmrg
37405a1a286Smrg    if
37505a1a286Smrg      $posix_mkdir && (
37605a1a286Smrg	umask $mkdir_umask &&
37705a1a286Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37805a1a286Smrg      )
37905a1a286Smrg    then :
38005a1a286Smrg    else
381d9c7fc6cSmrg
38205a1a286Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38305a1a286Smrg      # or it failed possibly due to a race condition.  Create the
38405a1a286Smrg      # directory the slow way, step by step, checking for races as we go.
385d9c7fc6cSmrg
38605a1a286Smrg      case $dstdir in
38705a1a286Smrg	/*) prefix='/';;
38805a1a286Smrg	-*) prefix='./';;
38905a1a286Smrg	*)  prefix='';;
39005a1a286Smrg      esac
391d9c7fc6cSmrg
39205a1a286Smrg      eval "$initialize_posix_glob"
393d9c7fc6cSmrg
39405a1a286Smrg      oIFS=$IFS
39505a1a286Smrg      IFS=/
39605a1a286Smrg      $posix_glob set -f
39705a1a286Smrg      set fnord $dstdir
398d9c7fc6cSmrg      shift
39905a1a286Smrg      $posix_glob set +f
40005a1a286Smrg      IFS=$oIFS
40105a1a286Smrg
40205a1a286Smrg      prefixes=
40305a1a286Smrg
40405a1a286Smrg      for d
40505a1a286Smrg      do
40605a1a286Smrg	test -z "$d" && continue
40705a1a286Smrg
40805a1a286Smrg	prefix=$prefix$d
40905a1a286Smrg	if test -d "$prefix"; then
41005a1a286Smrg	  prefixes=
41105a1a286Smrg	else
41205a1a286Smrg	  if $posix_mkdir; then
41305a1a286Smrg	    (umask=$mkdir_umask &&
41405a1a286Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41505a1a286Smrg	    # Don't fail if two instances are running concurrently.
41605a1a286Smrg	    test -d "$prefix" || exit 1
41705a1a286Smrg	  else
41805a1a286Smrg	    case $prefix in
41905a1a286Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42005a1a286Smrg	      *) qprefix=$prefix;;
42105a1a286Smrg	    esac
42205a1a286Smrg	    prefixes="$prefixes '$qprefix'"
42305a1a286Smrg	  fi
42405a1a286Smrg	fi
42505a1a286Smrg	prefix=$prefix/
42605a1a286Smrg      done
42705a1a286Smrg
42805a1a286Smrg      if test -n "$prefixes"; then
42905a1a286Smrg	# Don't fail if two instances are running concurrently.
43005a1a286Smrg	(umask $mkdir_umask &&
43105a1a286Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43205a1a286Smrg	  test -d "$dstdir" || exit 1
43305a1a286Smrg	obsolete_mkdir_used=true
434d9c7fc6cSmrg      fi
43505a1a286Smrg    fi
436d9c7fc6cSmrg  fi
437d9c7fc6cSmrg
438d9c7fc6cSmrg  if test -n "$dir_arg"; then
43905a1a286Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44005a1a286Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44105a1a286Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44205a1a286Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443d9c7fc6cSmrg  else
444d9c7fc6cSmrg
445d9c7fc6cSmrg    # Make a couple of temp file names in the proper directory.
446d9c7fc6cSmrg    dsttmp=$dstdir/_inst.$$_
447d9c7fc6cSmrg    rmtmp=$dstdir/_rm.$$_
448d9c7fc6cSmrg
449d9c7fc6cSmrg    # Trap to clean up those temp files at exit.
450d9c7fc6cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451d9c7fc6cSmrg
452d9c7fc6cSmrg    # Copy the file name to the temp name.
45305a1a286Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454d9c7fc6cSmrg
455d9c7fc6cSmrg    # and set any options; do chmod last to preserve setuid bits.
456d9c7fc6cSmrg    #
457d9c7fc6cSmrg    # If any of these fail, we abort the whole thing.  If we want to
458d9c7fc6cSmrg    # ignore errors from any of these, just make sure not to ignore
459d9c7fc6cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460d9c7fc6cSmrg    #
46105a1a286Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46205a1a286Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46305a1a286Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46405a1a286Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46505a1a286Smrg
46605a1a286Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46705a1a286Smrg    if $copy_on_change &&
46805a1a286Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46905a1a286Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47005a1a286Smrg
47105a1a286Smrg       eval "$initialize_posix_glob" &&
47205a1a286Smrg       $posix_glob set -f &&
47305a1a286Smrg       set X $old && old=:$2:$4:$5:$6 &&
47405a1a286Smrg       set X $new && new=:$2:$4:$5:$6 &&
47505a1a286Smrg       $posix_glob set +f &&
47605a1a286Smrg
47705a1a286Smrg       test "$old" = "$new" &&
47805a1a286Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47905a1a286Smrg    then
48005a1a286Smrg      rm -f "$dsttmp"
48105a1a286Smrg    else
48205a1a286Smrg      # Rename the file to the real destination.
48305a1a286Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48405a1a286Smrg
48505a1a286Smrg      # The rename failed, perhaps because mv can't rename something else
48605a1a286Smrg      # to itself, or perhaps because mv is so ancient that it does not
48705a1a286Smrg      # support -f.
48805a1a286Smrg      {
48905a1a286Smrg	# Now remove or move aside any old file at destination location.
49005a1a286Smrg	# We try this two ways since rm can't unlink itself on some
49105a1a286Smrg	# systems and the destination file might be busy for other
49205a1a286Smrg	# reasons.  In this case, the final cleanup might fail but the new
49305a1a286Smrg	# file should still install successfully.
49405a1a286Smrg	{
49505a1a286Smrg	  test ! -f "$dst" ||
49605a1a286Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49705a1a286Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49805a1a286Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49905a1a286Smrg	  } ||
50005a1a286Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50105a1a286Smrg	    (exit 1); exit 1
50205a1a286Smrg	  }
50305a1a286Smrg	} &&
50405a1a286Smrg
50505a1a286Smrg	# Now rename the file to the real destination.
50605a1a286Smrg	$doit $mvcmd "$dsttmp" "$dst"
50705a1a286Smrg      }
50805a1a286Smrg    fi || exit 1
50905a1a286Smrg
51005a1a286Smrg    trap '' 0
51105a1a286Smrg  fi
512d9c7fc6cSmrgdone
513d9c7fc6cSmrg
514d9c7fc6cSmrg# Local variables:
515d9c7fc6cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516d9c7fc6cSmrg# time-stamp-start: "scriptversion="
517d9c7fc6cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51805a1a286Smrg# time-stamp-time-zone: "UTC"
51905a1a286Smrg# time-stamp-end: "; # UTC"
520d9c7fc6cSmrg# End:
521