install-sh revision fe2ac8d7
19027f4a0Smrg#!/bin/sh
29027f4a0Smrg# install - install a program, script, or datafile
39027f4a0Smrg
4fe2ac8d7Smrgscriptversion=2011-11-20.07; # UTC
59027f4a0Smrg
69027f4a0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79027f4a0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
89027f4a0Smrg# following copyright and license.
99027f4a0Smrg#
109027f4a0Smrg# Copyright (C) 1994 X Consortium
119027f4a0Smrg#
129027f4a0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139027f4a0Smrg# of this software and associated documentation files (the "Software"), to
149027f4a0Smrg# deal in the Software without restriction, including without limitation the
159027f4a0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169027f4a0Smrg# sell copies of the Software, and to permit persons to whom the Software is
179027f4a0Smrg# furnished to do so, subject to the following conditions:
189027f4a0Smrg#
199027f4a0Smrg# The above copyright notice and this permission notice shall be included in
209027f4a0Smrg# all copies or substantial portions of the Software.
219027f4a0Smrg#
229027f4a0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239027f4a0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249027f4a0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259027f4a0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269027f4a0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279027f4a0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289027f4a0Smrg#
299027f4a0Smrg# Except as contained in this notice, the name of the X Consortium shall not
309027f4a0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
319027f4a0Smrg# ings in this Software without prior written authorization from the X Consor-
329027f4a0Smrg# tium.
339027f4a0Smrg#
349027f4a0Smrg#
359027f4a0Smrg# FSF changes to this file are in the public domain.
369027f4a0Smrg#
379027f4a0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38fe2ac8d7Smrg# 'make' implicit rules from creating a file called install from it
399027f4a0Smrg# when there is no Makefile.
409027f4a0Smrg#
419027f4a0Smrg# This script is compatible with the BSD install script, but was written
423a72f200Smrg# from scratch.
433a72f200Smrg
443a72f200Smrgnl='
453a72f200Smrg'
463a72f200SmrgIFS=" ""	$nl"
479027f4a0Smrg
489027f4a0Smrg# set DOITPROG to echo to test this script
499027f4a0Smrg
509027f4a0Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
513a72f200Smrgdoit=${DOITPROG-}
523a72f200Smrgif test -z "$doit"; then
533a72f200Smrg  doit_exec=exec
543a72f200Smrgelse
553a72f200Smrg  doit_exec=$doit
563a72f200Smrgfi
579027f4a0Smrg
583a72f200Smrg# Put in absolute file names if you don't have them in your path;
593a72f200Smrg# or use environment vars.
603a72f200Smrg
613a72f200Smrgchgrpprog=${CHGRPPROG-chgrp}
623a72f200Smrgchmodprog=${CHMODPROG-chmod}
633a72f200Smrgchownprog=${CHOWNPROG-chown}
643a72f200Smrgcmpprog=${CMPPROG-cmp}
653a72f200Smrgcpprog=${CPPROG-cp}
663a72f200Smrgmkdirprog=${MKDIRPROG-mkdir}
673a72f200Smrgmvprog=${MVPROG-mv}
683a72f200Smrgrmprog=${RMPROG-rm}
693a72f200Smrgstripprog=${STRIPPROG-strip}
703a72f200Smrg
713a72f200Smrgposix_glob='?'
723a72f200Smrginitialize_posix_glob='
733a72f200Smrg  test "$posix_glob" != "?" || {
743a72f200Smrg    if (set -f) 2>/dev/null; then
753a72f200Smrg      posix_glob=
763a72f200Smrg    else
773a72f200Smrg      posix_glob=:
783a72f200Smrg    fi
793a72f200Smrg  }
803a72f200Smrg'
819027f4a0Smrg
823a72f200Smrgposix_mkdir=
833a72f200Smrg
843a72f200Smrg# Desired mode of installed file.
853a72f200Smrgmode=0755
869027f4a0Smrg
879027f4a0Smrgchgrpcmd=
883a72f200Smrgchmodcmd=$chmodprog
893a72f200Smrgchowncmd=
903a72f200Smrgmvcmd=$mvprog
919027f4a0Smrgrmcmd="$rmprog -f"
923a72f200Smrgstripcmd=
933a72f200Smrg
949027f4a0Smrgsrc=
959027f4a0Smrgdst=
969027f4a0Smrgdir_arg=
973a72f200Smrgdst_arg=
983a72f200Smrg
993a72f200Smrgcopy_on_change=false
1009027f4a0Smrgno_target_directory=
1019027f4a0Smrg
1023a72f200Smrgusage="\
1033a72f200SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1049027f4a0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1059027f4a0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1069027f4a0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1079027f4a0Smrg
1089027f4a0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1099027f4a0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1109027f4a0SmrgIn the 4th, create DIRECTORIES.
1119027f4a0Smrg
1129027f4a0SmrgOptions:
1133a72f200Smrg     --help     display this help and exit.
1143a72f200Smrg     --version  display version info and exit.
1153a72f200Smrg
1163a72f200Smrg  -c            (ignored)
1173a72f200Smrg  -C            install only if different (preserve the last data modification time)
1183a72f200Smrg  -d            create directories instead of installing files.
1193a72f200Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1203a72f200Smrg  -m MODE       $chmodprog installed files to MODE.
1213a72f200Smrg  -o USER       $chownprog installed files to USER.
1223a72f200Smrg  -s            $stripprog installed files.
1233a72f200Smrg  -t DIRECTORY  install into DIRECTORY.
1243a72f200Smrg  -T            report an error if DSTFILE is a directory.
1259027f4a0Smrg
1269027f4a0SmrgEnvironment variables override the default commands:
1273a72f200Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1283a72f200Smrg  RMPROG STRIPPROG
1299027f4a0Smrg"
1309027f4a0Smrg
1313a72f200Smrgwhile test $# -ne 0; do
1329027f4a0Smrg  case $1 in
1333a72f200Smrg    -c) ;;
1343a72f200Smrg
1353a72f200Smrg    -C) copy_on_change=true;;
1369027f4a0Smrg
1373a72f200Smrg    -d) dir_arg=true;;
1389027f4a0Smrg
1399027f4a0Smrg    -g) chgrpcmd="$chgrpprog $2"
1403a72f200Smrg	shift;;
1419027f4a0Smrg
1429027f4a0Smrg    --help) echo "$usage"; exit $?;;
1439027f4a0Smrg
1443a72f200Smrg    -m) mode=$2
1453a72f200Smrg	case $mode in
1463a72f200Smrg	  *' '* | *'	'* | *'
1473a72f200Smrg'*	  | *'*'* | *'?'* | *'['*)
1483a72f200Smrg	    echo "$0: invalid mode: $mode" >&2
1493a72f200Smrg	    exit 1;;
1503a72f200Smrg	esac
1513a72f200Smrg	shift;;
1529027f4a0Smrg
1539027f4a0Smrg    -o) chowncmd="$chownprog $2"
1543a72f200Smrg	shift;;
1559027f4a0Smrg
1563a72f200Smrg    -s) stripcmd=$stripprog;;
1579027f4a0Smrg
1583a72f200Smrg    -t) dst_arg=$2
159fe2ac8d7Smrg	# Protect names problematic for 'test' and other utilities.
160fe2ac8d7Smrg	case $dst_arg in
161fe2ac8d7Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162fe2ac8d7Smrg	esac
1633a72f200Smrg	shift;;
1649027f4a0Smrg
1653a72f200Smrg    -T) no_target_directory=true;;
1669027f4a0Smrg
1679027f4a0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1689027f4a0Smrg
1693a72f200Smrg    --)	shift
1709027f4a0Smrg	break;;
1713a72f200Smrg
1723a72f200Smrg    -*)	echo "$0: invalid option: $1" >&2
1733a72f200Smrg	exit 1;;
1743a72f200Smrg
1753a72f200Smrg    *)  break;;
1769027f4a0Smrg  esac
1773a72f200Smrg  shift
1789027f4a0Smrgdone
1799027f4a0Smrg
1803a72f200Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1813a72f200Smrg  # When -d is used, all remaining arguments are directories to create.
1823a72f200Smrg  # When -t is used, the destination is already specified.
1833a72f200Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1843a72f200Smrg  for arg
1853a72f200Smrg  do
1863a72f200Smrg    if test -n "$dst_arg"; then
1873a72f200Smrg      # $@ is not empty: it contains at least $arg.
1883a72f200Smrg      set fnord "$@" "$dst_arg"
1893a72f200Smrg      shift # fnord
1903a72f200Smrg    fi
1913a72f200Smrg    shift # arg
1923a72f200Smrg    dst_arg=$arg
193fe2ac8d7Smrg    # Protect names problematic for 'test' and other utilities.
194fe2ac8d7Smrg    case $dst_arg in
195fe2ac8d7Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196fe2ac8d7Smrg    esac
1973a72f200Smrg  done
1983a72f200Smrgfi
1993a72f200Smrg
2003a72f200Smrgif test $# -eq 0; then
2019027f4a0Smrg  if test -z "$dir_arg"; then
2029027f4a0Smrg    echo "$0: no input file specified." >&2
2039027f4a0Smrg    exit 1
2049027f4a0Smrg  fi
205fe2ac8d7Smrg  # It's OK to call 'install-sh -d' without argument.
2069027f4a0Smrg  # This can happen when creating conditional directories.
2079027f4a0Smrg  exit 0
2089027f4a0Smrgfi
2099027f4a0Smrg
2103a72f200Smrgif test -z "$dir_arg"; then
211fe2ac8d7Smrg  do_exit='(exit $ret); exit $ret'
212fe2ac8d7Smrg  trap "ret=129; $do_exit" 1
213fe2ac8d7Smrg  trap "ret=130; $do_exit" 2
214fe2ac8d7Smrg  trap "ret=141; $do_exit" 13
215fe2ac8d7Smrg  trap "ret=143; $do_exit" 15
2163a72f200Smrg
2173a72f200Smrg  # Set umask so as not to create temps with too-generous modes.
2183a72f200Smrg  # However, 'strip' requires both read and write access to temps.
2193a72f200Smrg  case $mode in
2203a72f200Smrg    # Optimize common cases.
2213a72f200Smrg    *644) cp_umask=133;;
2223a72f200Smrg    *755) cp_umask=22;;
2233a72f200Smrg
2243a72f200Smrg    *[0-7])
2253a72f200Smrg      if test -z "$stripcmd"; then
2263a72f200Smrg	u_plus_rw=
2273a72f200Smrg      else
2283a72f200Smrg	u_plus_rw='% 200'
2293a72f200Smrg      fi
2303a72f200Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2313a72f200Smrg    *)
2323a72f200Smrg      if test -z "$stripcmd"; then
2333a72f200Smrg	u_plus_rw=
2343a72f200Smrg      else
2353a72f200Smrg	u_plus_rw=,u+rw
2363a72f200Smrg      fi
2373a72f200Smrg      cp_umask=$mode$u_plus_rw;;
2383a72f200Smrg  esac
2393a72f200Smrgfi
2403a72f200Smrg
2419027f4a0Smrgfor src
2429027f4a0Smrgdo
243fe2ac8d7Smrg  # Protect names problematic for 'test' and other utilities.
2449027f4a0Smrg  case $src in
245fe2ac8d7Smrg    -* | [=\(\)!]) src=./$src;;
2469027f4a0Smrg  esac
2479027f4a0Smrg
2489027f4a0Smrg  if test -n "$dir_arg"; then
2499027f4a0Smrg    dst=$src
2503a72f200Smrg    dstdir=$dst
2513a72f200Smrg    test -d "$dstdir"
2523a72f200Smrg    dstdir_status=$?
2539027f4a0Smrg  else
2543a72f200Smrg
2559027f4a0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2569027f4a0Smrg    # might cause directories to be created, which would be especially bad
2579027f4a0Smrg    # if $src (and thus $dsttmp) contains '*'.
2589027f4a0Smrg    if test ! -f "$src" && test ! -d "$src"; then
2599027f4a0Smrg      echo "$0: $src does not exist." >&2
2609027f4a0Smrg      exit 1
2619027f4a0Smrg    fi
2629027f4a0Smrg
2633a72f200Smrg    if test -z "$dst_arg"; then
2649027f4a0Smrg      echo "$0: no destination specified." >&2
2659027f4a0Smrg      exit 1
2669027f4a0Smrg    fi
2673a72f200Smrg    dst=$dst_arg
2689027f4a0Smrg
2699027f4a0Smrg    # If destination is a directory, append the input filename; won't work
2709027f4a0Smrg    # if double slashes aren't ignored.
2719027f4a0Smrg    if test -d "$dst"; then
2729027f4a0Smrg      if test -n "$no_target_directory"; then
2733a72f200Smrg	echo "$0: $dst_arg: Is a directory" >&2
2749027f4a0Smrg	exit 1
2759027f4a0Smrg      fi
2763a72f200Smrg      dstdir=$dst
2773a72f200Smrg      dst=$dstdir/`basename "$src"`
2783a72f200Smrg      dstdir_status=0
2793a72f200Smrg    else
2803a72f200Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2813a72f200Smrg      dstdir=`
2823a72f200Smrg	(dirname "$dst") 2>/dev/null ||
2833a72f200Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2843a72f200Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2853a72f200Smrg	     X"$dst" : 'X\(//\)$' \| \
2863a72f200Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2873a72f200Smrg	echo X"$dst" |
2883a72f200Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2893a72f200Smrg		   s//\1/
2903a72f200Smrg		   q
2913a72f200Smrg		 }
2923a72f200Smrg		 /^X\(\/\/\)[^/].*/{
2933a72f200Smrg		   s//\1/
2943a72f200Smrg		   q
2953a72f200Smrg		 }
2963a72f200Smrg		 /^X\(\/\/\)$/{
2973a72f200Smrg		   s//\1/
2983a72f200Smrg		   q
2993a72f200Smrg		 }
3003a72f200Smrg		 /^X\(\/\).*/{
3013a72f200Smrg		   s//\1/
3023a72f200Smrg		   q
3033a72f200Smrg		 }
3043a72f200Smrg		 s/.*/./; q'
3053a72f200Smrg      `
3063a72f200Smrg
3073a72f200Smrg      test -d "$dstdir"
3083a72f200Smrg      dstdir_status=$?
3099027f4a0Smrg    fi
3109027f4a0Smrg  fi
3119027f4a0Smrg
3123a72f200Smrg  obsolete_mkdir_used=false
3133a72f200Smrg
3143a72f200Smrg  if test $dstdir_status != 0; then
3153a72f200Smrg    case $posix_mkdir in
3163a72f200Smrg      '')
3173a72f200Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3183a72f200Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3193a72f200Smrg	umask=`umask`
3203a72f200Smrg	case $stripcmd.$umask in
3213a72f200Smrg	  # Optimize common cases.
3223a72f200Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3233a72f200Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3243a72f200Smrg
3253a72f200Smrg	  *[0-7])
3263a72f200Smrg	    mkdir_umask=`expr $umask + 22 \
3273a72f200Smrg	      - $umask % 100 % 40 + $umask % 20 \
3283a72f200Smrg	      - $umask % 10 % 4 + $umask % 2
3293a72f200Smrg	    `;;
3303a72f200Smrg	  *) mkdir_umask=$umask,go-w;;
3313a72f200Smrg	esac
3323a72f200Smrg
3333a72f200Smrg	# With -d, create the new directory with the user-specified mode.
3343a72f200Smrg	# Otherwise, rely on $mkdir_umask.
3353a72f200Smrg	if test -n "$dir_arg"; then
3363a72f200Smrg	  mkdir_mode=-m$mode
3373a72f200Smrg	else
3383a72f200Smrg	  mkdir_mode=
3393a72f200Smrg	fi
3403a72f200Smrg
3413a72f200Smrg	posix_mkdir=false
3423a72f200Smrg	case $umask in
3433a72f200Smrg	  *[123567][0-7][0-7])
3443a72f200Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3453a72f200Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3463a72f200Smrg	    ;;
3473a72f200Smrg	  *)
3483a72f200Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3493a72f200Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3503a72f200Smrg
3513a72f200Smrg	    if (umask $mkdir_umask &&
3523a72f200Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3533a72f200Smrg	    then
3543a72f200Smrg	      if test -z "$dir_arg" || {
3553a72f200Smrg		   # Check for POSIX incompatibilities with -m.
3563a72f200Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357fe2ac8d7Smrg		   # other-writable bit of parent directory when it shouldn't.
3583a72f200Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3593a72f200Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3603a72f200Smrg		   case $ls_ld_tmpdir in
3613a72f200Smrg		     d????-?r-*) different_mode=700;;
3623a72f200Smrg		     d????-?--*) different_mode=755;;
3633a72f200Smrg		     *) false;;
3643a72f200Smrg		   esac &&
3653a72f200Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3663a72f200Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3673a72f200Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3683a72f200Smrg		   }
3693a72f200Smrg		 }
3703a72f200Smrg	      then posix_mkdir=:
3713a72f200Smrg	      fi
3723a72f200Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3733a72f200Smrg	    else
3743a72f200Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3753a72f200Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3763a72f200Smrg	    fi
3773a72f200Smrg	    trap '' 0;;
3783a72f200Smrg	esac;;
3793a72f200Smrg    esac
3809027f4a0Smrg
3813a72f200Smrg    if
3823a72f200Smrg      $posix_mkdir && (
3833a72f200Smrg	umask $mkdir_umask &&
3843a72f200Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3853a72f200Smrg      )
3863a72f200Smrg    then :
3873a72f200Smrg    else
3889027f4a0Smrg
3893a72f200Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3903a72f200Smrg      # or it failed possibly due to a race condition.  Create the
3913a72f200Smrg      # directory the slow way, step by step, checking for races as we go.
3929027f4a0Smrg
3933a72f200Smrg      case $dstdir in
3943a72f200Smrg	/*) prefix='/';;
395fe2ac8d7Smrg	[-=\(\)!]*) prefix='./';;
3963a72f200Smrg	*)  prefix='';;
3973a72f200Smrg      esac
3989027f4a0Smrg
3993a72f200Smrg      eval "$initialize_posix_glob"
4009027f4a0Smrg
4013a72f200Smrg      oIFS=$IFS
4023a72f200Smrg      IFS=/
4033a72f200Smrg      $posix_glob set -f
4043a72f200Smrg      set fnord $dstdir
4059027f4a0Smrg      shift
4063a72f200Smrg      $posix_glob set +f
4073a72f200Smrg      IFS=$oIFS
4083a72f200Smrg
4093a72f200Smrg      prefixes=
4103a72f200Smrg
4113a72f200Smrg      for d
4123a72f200Smrg      do
413fe2ac8d7Smrg	test X"$d" = X && continue
4143a72f200Smrg
4153a72f200Smrg	prefix=$prefix$d
4163a72f200Smrg	if test -d "$prefix"; then
4173a72f200Smrg	  prefixes=
4183a72f200Smrg	else
4193a72f200Smrg	  if $posix_mkdir; then
4203a72f200Smrg	    (umask=$mkdir_umask &&
4213a72f200Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4223a72f200Smrg	    # Don't fail if two instances are running concurrently.
4233a72f200Smrg	    test -d "$prefix" || exit 1
4243a72f200Smrg	  else
4253a72f200Smrg	    case $prefix in
4263a72f200Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4273a72f200Smrg	      *) qprefix=$prefix;;
4283a72f200Smrg	    esac
4293a72f200Smrg	    prefixes="$prefixes '$qprefix'"
4303a72f200Smrg	  fi
4313a72f200Smrg	fi
4323a72f200Smrg	prefix=$prefix/
4333a72f200Smrg      done
4343a72f200Smrg
4353a72f200Smrg      if test -n "$prefixes"; then
4363a72f200Smrg	# Don't fail if two instances are running concurrently.
4373a72f200Smrg	(umask $mkdir_umask &&
4383a72f200Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4393a72f200Smrg	  test -d "$dstdir" || exit 1
4403a72f200Smrg	obsolete_mkdir_used=true
4419027f4a0Smrg      fi
4423a72f200Smrg    fi
4439027f4a0Smrg  fi
4449027f4a0Smrg
4459027f4a0Smrg  if test -n "$dir_arg"; then
4463a72f200Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4473a72f200Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4483a72f200Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4493a72f200Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4509027f4a0Smrg  else
4519027f4a0Smrg
4529027f4a0Smrg    # Make a couple of temp file names in the proper directory.
4539027f4a0Smrg    dsttmp=$dstdir/_inst.$$_
4549027f4a0Smrg    rmtmp=$dstdir/_rm.$$_
4559027f4a0Smrg
4569027f4a0Smrg    # Trap to clean up those temp files at exit.
4579027f4a0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4589027f4a0Smrg
4599027f4a0Smrg    # Copy the file name to the temp name.
4603a72f200Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4619027f4a0Smrg
4629027f4a0Smrg    # and set any options; do chmod last to preserve setuid bits.
4639027f4a0Smrg    #
4649027f4a0Smrg    # If any of these fail, we abort the whole thing.  If we want to
4659027f4a0Smrg    # ignore errors from any of these, just make sure not to ignore
4669027f4a0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4679027f4a0Smrg    #
4683a72f200Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4693a72f200Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4703a72f200Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4713a72f200Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4723a72f200Smrg
4733a72f200Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4743a72f200Smrg    if $copy_on_change &&
4753a72f200Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4763a72f200Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4773a72f200Smrg
4783a72f200Smrg       eval "$initialize_posix_glob" &&
4793a72f200Smrg       $posix_glob set -f &&
4803a72f200Smrg       set X $old && old=:$2:$4:$5:$6 &&
4813a72f200Smrg       set X $new && new=:$2:$4:$5:$6 &&
4823a72f200Smrg       $posix_glob set +f &&
4833a72f200Smrg
4843a72f200Smrg       test "$old" = "$new" &&
4853a72f200Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4863a72f200Smrg    then
4873a72f200Smrg      rm -f "$dsttmp"
4883a72f200Smrg    else
4893a72f200Smrg      # Rename the file to the real destination.
4903a72f200Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4913a72f200Smrg
4923a72f200Smrg      # The rename failed, perhaps because mv can't rename something else
4933a72f200Smrg      # to itself, or perhaps because mv is so ancient that it does not
4943a72f200Smrg      # support -f.
4953a72f200Smrg      {
4963a72f200Smrg	# Now remove or move aside any old file at destination location.
4973a72f200Smrg	# We try this two ways since rm can't unlink itself on some
4983a72f200Smrg	# systems and the destination file might be busy for other
4993a72f200Smrg	# reasons.  In this case, the final cleanup might fail but the new
5003a72f200Smrg	# file should still install successfully.
5013a72f200Smrg	{
5023a72f200Smrg	  test ! -f "$dst" ||
5033a72f200Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5043a72f200Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5053a72f200Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5063a72f200Smrg	  } ||
5073a72f200Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5083a72f200Smrg	    (exit 1); exit 1
5093a72f200Smrg	  }
5103a72f200Smrg	} &&
5113a72f200Smrg
5123a72f200Smrg	# Now rename the file to the real destination.
5133a72f200Smrg	$doit $mvcmd "$dsttmp" "$dst"
5143a72f200Smrg      }
5153a72f200Smrg    fi || exit 1
5163a72f200Smrg
5173a72f200Smrg    trap '' 0
5183a72f200Smrg  fi
5199027f4a0Smrgdone
5209027f4a0Smrg
5219027f4a0Smrg# Local variables:
5229027f4a0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5239027f4a0Smrg# time-stamp-start: "scriptversion="
5249027f4a0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5253a72f200Smrg# time-stamp-time-zone: "UTC"
5263a72f200Smrg# time-stamp-end: "; # UTC"
5279027f4a0Smrg# End:
528