install-sh revision b62cc08c
19fe995a9Smrg#!/bin/sh
29fe995a9Smrg# install - install a program, script, or datafile
39fe995a9Smrg
4b62cc08cSmrgscriptversion=2009-04-28.21; # UTC
59fe995a9Smrg
69fe995a9Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79fe995a9Smrg# later released in X11R6 (xc/config/util/install.sh) with the
89fe995a9Smrg# following copyright and license.
99fe995a9Smrg#
109fe995a9Smrg# Copyright (C) 1994 X Consortium
119fe995a9Smrg#
129fe995a9Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139fe995a9Smrg# of this software and associated documentation files (the "Software"), to
149fe995a9Smrg# deal in the Software without restriction, including without limitation the
159fe995a9Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169fe995a9Smrg# sell copies of the Software, and to permit persons to whom the Software is
179fe995a9Smrg# furnished to do so, subject to the following conditions:
189fe995a9Smrg#
199fe995a9Smrg# The above copyright notice and this permission notice shall be included in
209fe995a9Smrg# all copies or substantial portions of the Software.
219fe995a9Smrg#
229fe995a9Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239fe995a9Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249fe995a9Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259fe995a9Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269fe995a9Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279fe995a9Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289fe995a9Smrg#
299fe995a9Smrg# Except as contained in this notice, the name of the X Consortium shall not
309fe995a9Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
319fe995a9Smrg# ings in this Software without prior written authorization from the X Consor-
329fe995a9Smrg# tium.
339fe995a9Smrg#
349fe995a9Smrg#
359fe995a9Smrg# FSF changes to this file are in the public domain.
369fe995a9Smrg#
379fe995a9Smrg# Calling this script install-sh is preferred over install.sh, to prevent
389fe995a9Smrg# `make' implicit rules from creating a file called install from it
399fe995a9Smrg# when there is no Makefile.
409fe995a9Smrg#
419fe995a9Smrg# This script is compatible with the BSD install script, but was written
429fe995a9Smrg# from scratch.
439fe995a9Smrg
449fe995a9Smrgnl='
459fe995a9Smrg'
469fe995a9SmrgIFS=" ""	$nl"
479fe995a9Smrg
489fe995a9Smrg# set DOITPROG to echo to test this script
499fe995a9Smrg
509fe995a9Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51b62cc08cSmrgdoit=${DOITPROG-}
529fe995a9Smrgif test -z "$doit"; then
539fe995a9Smrg  doit_exec=exec
549fe995a9Smrgelse
559fe995a9Smrg  doit_exec=$doit
569fe995a9Smrgfi
579fe995a9Smrg
589fe995a9Smrg# Put in absolute file names if you don't have them in your path;
599fe995a9Smrg# or use environment vars.
609fe995a9Smrg
61b62cc08cSmrgchgrpprog=${CHGRPPROG-chgrp}
62b62cc08cSmrgchmodprog=${CHMODPROG-chmod}
63b62cc08cSmrgchownprog=${CHOWNPROG-chown}
64b62cc08cSmrgcmpprog=${CMPPROG-cmp}
65b62cc08cSmrgcpprog=${CPPROG-cp}
66b62cc08cSmrgmkdirprog=${MKDIRPROG-mkdir}
67b62cc08cSmrgmvprog=${MVPROG-mv}
68b62cc08cSmrgrmprog=${RMPROG-rm}
69b62cc08cSmrgstripprog=${STRIPPROG-strip}
70b62cc08cSmrg
71b62cc08cSmrgposix_glob='?'
72b62cc08cSmrginitialize_posix_glob='
73b62cc08cSmrg  test "$posix_glob" != "?" || {
74b62cc08cSmrg    if (set -f) 2>/dev/null; then
75b62cc08cSmrg      posix_glob=
76b62cc08cSmrg    else
77b62cc08cSmrg      posix_glob=:
78b62cc08cSmrg    fi
79b62cc08cSmrg  }
80b62cc08cSmrg'
819fe995a9Smrg
829fe995a9Smrgposix_mkdir=
839fe995a9Smrg
849fe995a9Smrg# Desired mode of installed file.
859fe995a9Smrgmode=0755
869fe995a9Smrg
87b62cc08cSmrgchgrpcmd=
889fe995a9Smrgchmodcmd=$chmodprog
899fe995a9Smrgchowncmd=
90b62cc08cSmrgmvcmd=$mvprog
919fe995a9Smrgrmcmd="$rmprog -f"
92b62cc08cSmrgstripcmd=
93b62cc08cSmrg
949fe995a9Smrgsrc=
959fe995a9Smrgdst=
969fe995a9Smrgdir_arg=
97b62cc08cSmrgdst_arg=
98b62cc08cSmrg
99b62cc08cSmrgcopy_on_change=false
1009fe995a9Smrgno_target_directory=
1019fe995a9Smrg
102b62cc08cSmrgusage="\
103b62cc08cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1049fe995a9Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1059fe995a9Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1069fe995a9Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1079fe995a9Smrg
1089fe995a9SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1099fe995a9SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1109fe995a9SmrgIn the 4th, create DIRECTORIES.
1119fe995a9Smrg
1129fe995a9SmrgOptions:
113b62cc08cSmrg     --help     display this help and exit.
114b62cc08cSmrg     --version  display version info and exit.
115b62cc08cSmrg
116b62cc08cSmrg  -c            (ignored)
117b62cc08cSmrg  -C            install only if different (preserve the last data modification time)
118b62cc08cSmrg  -d            create directories instead of installing files.
119b62cc08cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
120b62cc08cSmrg  -m MODE       $chmodprog installed files to MODE.
121b62cc08cSmrg  -o USER       $chownprog installed files to USER.
122b62cc08cSmrg  -s            $stripprog installed files.
123b62cc08cSmrg  -t DIRECTORY  install into DIRECTORY.
124b62cc08cSmrg  -T            report an error if DSTFILE is a directory.
1259fe995a9Smrg
1269fe995a9SmrgEnvironment variables override the default commands:
127b62cc08cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128b62cc08cSmrg  RMPROG STRIPPROG
1299fe995a9Smrg"
1309fe995a9Smrg
1319fe995a9Smrgwhile test $# -ne 0; do
1329fe995a9Smrg  case $1 in
133b62cc08cSmrg    -c) ;;
134b62cc08cSmrg
135b62cc08cSmrg    -C) copy_on_change=true;;
1369fe995a9Smrg
137b62cc08cSmrg    -d) dir_arg=true;;
1389fe995a9Smrg
1399fe995a9Smrg    -g) chgrpcmd="$chgrpprog $2"
140b62cc08cSmrg	shift;;
1419fe995a9Smrg
1429fe995a9Smrg    --help) echo "$usage"; exit $?;;
1439fe995a9Smrg
1449fe995a9Smrg    -m) mode=$2
1459fe995a9Smrg	case $mode in
1469fe995a9Smrg	  *' '* | *'	'* | *'
1479fe995a9Smrg'*	  | *'*'* | *'?'* | *'['*)
1489fe995a9Smrg	    echo "$0: invalid mode: $mode" >&2
1499fe995a9Smrg	    exit 1;;
1509fe995a9Smrg	esac
151b62cc08cSmrg	shift;;
1529fe995a9Smrg
1539fe995a9Smrg    -o) chowncmd="$chownprog $2"
154b62cc08cSmrg	shift;;
1559fe995a9Smrg
156b62cc08cSmrg    -s) stripcmd=$stripprog;;
1579fe995a9Smrg
158b62cc08cSmrg    -t) dst_arg=$2
159b62cc08cSmrg	shift;;
1609fe995a9Smrg
161b62cc08cSmrg    -T) no_target_directory=true;;
1629fe995a9Smrg
1639fe995a9Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1649fe995a9Smrg
1659fe995a9Smrg    --)	shift
1669fe995a9Smrg	break;;
1679fe995a9Smrg
1689fe995a9Smrg    -*)	echo "$0: invalid option: $1" >&2
1699fe995a9Smrg	exit 1;;
1709fe995a9Smrg
1719fe995a9Smrg    *)  break;;
1729fe995a9Smrg  esac
173b62cc08cSmrg  shift
1749fe995a9Smrgdone
1759fe995a9Smrg
176b62cc08cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1779fe995a9Smrg  # When -d is used, all remaining arguments are directories to create.
1789fe995a9Smrg  # When -t is used, the destination is already specified.
1799fe995a9Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1809fe995a9Smrg  for arg
1819fe995a9Smrg  do
182b62cc08cSmrg    if test -n "$dst_arg"; then
1839fe995a9Smrg      # $@ is not empty: it contains at least $arg.
184b62cc08cSmrg      set fnord "$@" "$dst_arg"
1859fe995a9Smrg      shift # fnord
1869fe995a9Smrg    fi
1879fe995a9Smrg    shift # arg
188b62cc08cSmrg    dst_arg=$arg
1899fe995a9Smrg  done
1909fe995a9Smrgfi
1919fe995a9Smrg
1929fe995a9Smrgif test $# -eq 0; then
1939fe995a9Smrg  if test -z "$dir_arg"; then
1949fe995a9Smrg    echo "$0: no input file specified." >&2
1959fe995a9Smrg    exit 1
1969fe995a9Smrg  fi
1979fe995a9Smrg  # It's OK to call `install-sh -d' without argument.
1989fe995a9Smrg  # This can happen when creating conditional directories.
1999fe995a9Smrg  exit 0
2009fe995a9Smrgfi
2019fe995a9Smrg
2029fe995a9Smrgif test -z "$dir_arg"; then
2039fe995a9Smrg  trap '(exit $?); exit' 1 2 13 15
2049fe995a9Smrg
2059fe995a9Smrg  # Set umask so as not to create temps with too-generous modes.
2069fe995a9Smrg  # However, 'strip' requires both read and write access to temps.
2079fe995a9Smrg  case $mode in
2089fe995a9Smrg    # Optimize common cases.
2099fe995a9Smrg    *644) cp_umask=133;;
2109fe995a9Smrg    *755) cp_umask=22;;
2119fe995a9Smrg
2129fe995a9Smrg    *[0-7])
2139fe995a9Smrg      if test -z "$stripcmd"; then
2149fe995a9Smrg	u_plus_rw=
2159fe995a9Smrg      else
2169fe995a9Smrg	u_plus_rw='% 200'
2179fe995a9Smrg      fi
2189fe995a9Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2199fe995a9Smrg    *)
2209fe995a9Smrg      if test -z "$stripcmd"; then
2219fe995a9Smrg	u_plus_rw=
2229fe995a9Smrg      else
2239fe995a9Smrg	u_plus_rw=,u+rw
2249fe995a9Smrg      fi
2259fe995a9Smrg      cp_umask=$mode$u_plus_rw;;
2269fe995a9Smrg  esac
2279fe995a9Smrgfi
2289fe995a9Smrg
2299fe995a9Smrgfor src
2309fe995a9Smrgdo
2319fe995a9Smrg  # Protect names starting with `-'.
2329fe995a9Smrg  case $src in
233b62cc08cSmrg    -*) src=./$src;;
2349fe995a9Smrg  esac
2359fe995a9Smrg
2369fe995a9Smrg  if test -n "$dir_arg"; then
2379fe995a9Smrg    dst=$src
2389fe995a9Smrg    dstdir=$dst
2399fe995a9Smrg    test -d "$dstdir"
2409fe995a9Smrg    dstdir_status=$?
2419fe995a9Smrg  else
2429fe995a9Smrg
2439fe995a9Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2449fe995a9Smrg    # might cause directories to be created, which would be especially bad
2459fe995a9Smrg    # if $src (and thus $dsttmp) contains '*'.
2469fe995a9Smrg    if test ! -f "$src" && test ! -d "$src"; then
2479fe995a9Smrg      echo "$0: $src does not exist." >&2
2489fe995a9Smrg      exit 1
2499fe995a9Smrg    fi
2509fe995a9Smrg
251b62cc08cSmrg    if test -z "$dst_arg"; then
2529fe995a9Smrg      echo "$0: no destination specified." >&2
2539fe995a9Smrg      exit 1
2549fe995a9Smrg    fi
2559fe995a9Smrg
256b62cc08cSmrg    dst=$dst_arg
2579fe995a9Smrg    # Protect names starting with `-'.
2589fe995a9Smrg    case $dst in
259b62cc08cSmrg      -*) dst=./$dst;;
2609fe995a9Smrg    esac
2619fe995a9Smrg
2629fe995a9Smrg    # If destination is a directory, append the input filename; won't work
2639fe995a9Smrg    # if double slashes aren't ignored.
2649fe995a9Smrg    if test -d "$dst"; then
2659fe995a9Smrg      if test -n "$no_target_directory"; then
266b62cc08cSmrg	echo "$0: $dst_arg: Is a directory" >&2
2679fe995a9Smrg	exit 1
2689fe995a9Smrg      fi
2699fe995a9Smrg      dstdir=$dst
2709fe995a9Smrg      dst=$dstdir/`basename "$src"`
2719fe995a9Smrg      dstdir_status=0
2729fe995a9Smrg    else
2739fe995a9Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2749fe995a9Smrg      dstdir=`
2759fe995a9Smrg	(dirname "$dst") 2>/dev/null ||
2769fe995a9Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2779fe995a9Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2789fe995a9Smrg	     X"$dst" : 'X\(//\)$' \| \
2799fe995a9Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2809fe995a9Smrg	echo X"$dst" |
2819fe995a9Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2829fe995a9Smrg		   s//\1/
2839fe995a9Smrg		   q
2849fe995a9Smrg		 }
2859fe995a9Smrg		 /^X\(\/\/\)[^/].*/{
2869fe995a9Smrg		   s//\1/
2879fe995a9Smrg		   q
2889fe995a9Smrg		 }
2899fe995a9Smrg		 /^X\(\/\/\)$/{
2909fe995a9Smrg		   s//\1/
2919fe995a9Smrg		   q
2929fe995a9Smrg		 }
2939fe995a9Smrg		 /^X\(\/\).*/{
2949fe995a9Smrg		   s//\1/
2959fe995a9Smrg		   q
2969fe995a9Smrg		 }
2979fe995a9Smrg		 s/.*/./; q'
2989fe995a9Smrg      `
2999fe995a9Smrg
3009fe995a9Smrg      test -d "$dstdir"
3019fe995a9Smrg      dstdir_status=$?
3029fe995a9Smrg    fi
3039fe995a9Smrg  fi
3049fe995a9Smrg
3059fe995a9Smrg  obsolete_mkdir_used=false
3069fe995a9Smrg
3079fe995a9Smrg  if test $dstdir_status != 0; then
3089fe995a9Smrg    case $posix_mkdir in
3099fe995a9Smrg      '')
3109fe995a9Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3119fe995a9Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3129fe995a9Smrg	umask=`umask`
3139fe995a9Smrg	case $stripcmd.$umask in
3149fe995a9Smrg	  # Optimize common cases.
3159fe995a9Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3169fe995a9Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3179fe995a9Smrg
3189fe995a9Smrg	  *[0-7])
3199fe995a9Smrg	    mkdir_umask=`expr $umask + 22 \
3209fe995a9Smrg	      - $umask % 100 % 40 + $umask % 20 \
3219fe995a9Smrg	      - $umask % 10 % 4 + $umask % 2
3229fe995a9Smrg	    `;;
3239fe995a9Smrg	  *) mkdir_umask=$umask,go-w;;
3249fe995a9Smrg	esac
3259fe995a9Smrg
3269fe995a9Smrg	# With -d, create the new directory with the user-specified mode.
3279fe995a9Smrg	# Otherwise, rely on $mkdir_umask.
3289fe995a9Smrg	if test -n "$dir_arg"; then
3299fe995a9Smrg	  mkdir_mode=-m$mode
3309fe995a9Smrg	else
3319fe995a9Smrg	  mkdir_mode=
3329fe995a9Smrg	fi
3339fe995a9Smrg
3349fe995a9Smrg	posix_mkdir=false
3359fe995a9Smrg	case $umask in
3369fe995a9Smrg	  *[123567][0-7][0-7])
3379fe995a9Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3389fe995a9Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3399fe995a9Smrg	    ;;
3409fe995a9Smrg	  *)
3419fe995a9Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3429fe995a9Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3439fe995a9Smrg
3449fe995a9Smrg	    if (umask $mkdir_umask &&
3459fe995a9Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3469fe995a9Smrg	    then
3479fe995a9Smrg	      if test -z "$dir_arg" || {
3489fe995a9Smrg		   # Check for POSIX incompatibilities with -m.
3499fe995a9Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3509fe995a9Smrg		   # other-writeable bit of parent directory when it shouldn't.
3519fe995a9Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3529fe995a9Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3539fe995a9Smrg		   case $ls_ld_tmpdir in
3549fe995a9Smrg		     d????-?r-*) different_mode=700;;
3559fe995a9Smrg		     d????-?--*) different_mode=755;;
3569fe995a9Smrg		     *) false;;
3579fe995a9Smrg		   esac &&
3589fe995a9Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3599fe995a9Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3609fe995a9Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3619fe995a9Smrg		   }
3629fe995a9Smrg		 }
3639fe995a9Smrg	      then posix_mkdir=:
3649fe995a9Smrg	      fi
3659fe995a9Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3669fe995a9Smrg	    else
3679fe995a9Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3689fe995a9Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3699fe995a9Smrg	    fi
3709fe995a9Smrg	    trap '' 0;;
3719fe995a9Smrg	esac;;
3729fe995a9Smrg    esac
3739fe995a9Smrg
3749fe995a9Smrg    if
3759fe995a9Smrg      $posix_mkdir && (
3769fe995a9Smrg	umask $mkdir_umask &&
3779fe995a9Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3789fe995a9Smrg      )
3799fe995a9Smrg    then :
3809fe995a9Smrg    else
3819fe995a9Smrg
3829fe995a9Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3839fe995a9Smrg      # or it failed possibly due to a race condition.  Create the
3849fe995a9Smrg      # directory the slow way, step by step, checking for races as we go.
3859fe995a9Smrg
3869fe995a9Smrg      case $dstdir in
387b62cc08cSmrg	/*) prefix='/';;
388b62cc08cSmrg	-*) prefix='./';;
389b62cc08cSmrg	*)  prefix='';;
3909fe995a9Smrg      esac
3919fe995a9Smrg
392b62cc08cSmrg      eval "$initialize_posix_glob"
3939fe995a9Smrg
3949fe995a9Smrg      oIFS=$IFS
3959fe995a9Smrg      IFS=/
396b62cc08cSmrg      $posix_glob set -f
3979fe995a9Smrg      set fnord $dstdir
3989fe995a9Smrg      shift
399b62cc08cSmrg      $posix_glob set +f
4009fe995a9Smrg      IFS=$oIFS
4019fe995a9Smrg
4029fe995a9Smrg      prefixes=
4039fe995a9Smrg
4049fe995a9Smrg      for d
4059fe995a9Smrg      do
4069fe995a9Smrg	test -z "$d" && continue
4079fe995a9Smrg
4089fe995a9Smrg	prefix=$prefix$d
4099fe995a9Smrg	if test -d "$prefix"; then
4109fe995a9Smrg	  prefixes=
4119fe995a9Smrg	else
4129fe995a9Smrg	  if $posix_mkdir; then
4139fe995a9Smrg	    (umask=$mkdir_umask &&
4149fe995a9Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4159fe995a9Smrg	    # Don't fail if two instances are running concurrently.
4169fe995a9Smrg	    test -d "$prefix" || exit 1
4179fe995a9Smrg	  else
4189fe995a9Smrg	    case $prefix in
4199fe995a9Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4209fe995a9Smrg	      *) qprefix=$prefix;;
4219fe995a9Smrg	    esac
4229fe995a9Smrg	    prefixes="$prefixes '$qprefix'"
4239fe995a9Smrg	  fi
4249fe995a9Smrg	fi
4259fe995a9Smrg	prefix=$prefix/
4269fe995a9Smrg      done
4279fe995a9Smrg
4289fe995a9Smrg      if test -n "$prefixes"; then
4299fe995a9Smrg	# Don't fail if two instances are running concurrently.
4309fe995a9Smrg	(umask $mkdir_umask &&
4319fe995a9Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4329fe995a9Smrg	  test -d "$dstdir" || exit 1
4339fe995a9Smrg	obsolete_mkdir_used=true
4349fe995a9Smrg      fi
4359fe995a9Smrg    fi
4369fe995a9Smrg  fi
4379fe995a9Smrg
4389fe995a9Smrg  if test -n "$dir_arg"; then
4399fe995a9Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4409fe995a9Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4419fe995a9Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4429fe995a9Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4439fe995a9Smrg  else
4449fe995a9Smrg
4459fe995a9Smrg    # Make a couple of temp file names in the proper directory.
4469fe995a9Smrg    dsttmp=$dstdir/_inst.$$_
4479fe995a9Smrg    rmtmp=$dstdir/_rm.$$_
4489fe995a9Smrg
4499fe995a9Smrg    # Trap to clean up those temp files at exit.
4509fe995a9Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4519fe995a9Smrg
4529fe995a9Smrg    # Copy the file name to the temp name.
4539fe995a9Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4549fe995a9Smrg
4559fe995a9Smrg    # and set any options; do chmod last to preserve setuid bits.
4569fe995a9Smrg    #
4579fe995a9Smrg    # If any of these fail, we abort the whole thing.  If we want to
4589fe995a9Smrg    # ignore errors from any of these, just make sure not to ignore
4599fe995a9Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4609fe995a9Smrg    #
461b62cc08cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462b62cc08cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463b62cc08cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464b62cc08cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465b62cc08cSmrg
466b62cc08cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
467b62cc08cSmrg    if $copy_on_change &&
468b62cc08cSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469b62cc08cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470b62cc08cSmrg
471b62cc08cSmrg       eval "$initialize_posix_glob" &&
472b62cc08cSmrg       $posix_glob set -f &&
473b62cc08cSmrg       set X $old && old=:$2:$4:$5:$6 &&
474b62cc08cSmrg       set X $new && new=:$2:$4:$5:$6 &&
475b62cc08cSmrg       $posix_glob set +f &&
476b62cc08cSmrg
477b62cc08cSmrg       test "$old" = "$new" &&
478b62cc08cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479b62cc08cSmrg    then
480b62cc08cSmrg      rm -f "$dsttmp"
481b62cc08cSmrg    else
482b62cc08cSmrg      # Rename the file to the real destination.
483b62cc08cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484b62cc08cSmrg
485b62cc08cSmrg      # The rename failed, perhaps because mv can't rename something else
486b62cc08cSmrg      # to itself, or perhaps because mv is so ancient that it does not
487b62cc08cSmrg      # support -f.
488b62cc08cSmrg      {
489b62cc08cSmrg	# Now remove or move aside any old file at destination location.
490b62cc08cSmrg	# We try this two ways since rm can't unlink itself on some
491b62cc08cSmrg	# systems and the destination file might be busy for other
492b62cc08cSmrg	# reasons.  In this case, the final cleanup might fail but the new
493b62cc08cSmrg	# file should still install successfully.
494b62cc08cSmrg	{
495b62cc08cSmrg	  test ! -f "$dst" ||
496b62cc08cSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497b62cc08cSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498b62cc08cSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499b62cc08cSmrg	  } ||
500b62cc08cSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
501b62cc08cSmrg	    (exit 1); exit 1
502b62cc08cSmrg	  }
503b62cc08cSmrg	} &&
504b62cc08cSmrg
505b62cc08cSmrg	# Now rename the file to the real destination.
506b62cc08cSmrg	$doit $mvcmd "$dsttmp" "$dst"
507b62cc08cSmrg      }
508b62cc08cSmrg    fi || exit 1
5099fe995a9Smrg
5109fe995a9Smrg    trap '' 0
5119fe995a9Smrg  fi
5129fe995a9Smrgdone
5139fe995a9Smrg
5149fe995a9Smrg# Local variables:
5159fe995a9Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5169fe995a9Smrg# time-stamp-start: "scriptversion="
5179fe995a9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518b62cc08cSmrg# time-stamp-time-zone: "UTC"
519b62cc08cSmrg# time-stamp-end: "; # UTC"
5209fe995a9Smrg# End:
521