install-sh revision 9d794632
19fe995a9Smrg#!/bin/sh
29fe995a9Smrg# install - install a program, script, or datafile
39fe995a9Smrg
49d794632Smrgscriptversion=2011-11-20.07; # 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
389d794632Smrg# '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
1599d794632Smrg	# Protect names problematic for 'test' and other utilities.
1609d794632Smrg	case $dst_arg in
1619d794632Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1629d794632Smrg	esac
163b62cc08cSmrg	shift;;
1649fe995a9Smrg
165b62cc08cSmrg    -T) no_target_directory=true;;
1669fe995a9Smrg
1679fe995a9Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1689fe995a9Smrg
1699fe995a9Smrg    --)	shift
1709fe995a9Smrg	break;;
1719fe995a9Smrg
1729fe995a9Smrg    -*)	echo "$0: invalid option: $1" >&2
1739fe995a9Smrg	exit 1;;
1749fe995a9Smrg
1759fe995a9Smrg    *)  break;;
1769fe995a9Smrg  esac
177b62cc08cSmrg  shift
1789fe995a9Smrgdone
1799fe995a9Smrg
180b62cc08cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1819fe995a9Smrg  # When -d is used, all remaining arguments are directories to create.
1829fe995a9Smrg  # When -t is used, the destination is already specified.
1839fe995a9Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1849fe995a9Smrg  for arg
1859fe995a9Smrg  do
186b62cc08cSmrg    if test -n "$dst_arg"; then
1879fe995a9Smrg      # $@ is not empty: it contains at least $arg.
188b62cc08cSmrg      set fnord "$@" "$dst_arg"
1899fe995a9Smrg      shift # fnord
1909fe995a9Smrg    fi
1919fe995a9Smrg    shift # arg
192b62cc08cSmrg    dst_arg=$arg
1939d794632Smrg    # Protect names problematic for 'test' and other utilities.
1949d794632Smrg    case $dst_arg in
1959d794632Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1969d794632Smrg    esac
1979fe995a9Smrg  done
1989fe995a9Smrgfi
1999fe995a9Smrg
2009fe995a9Smrgif test $# -eq 0; then
2019fe995a9Smrg  if test -z "$dir_arg"; then
2029fe995a9Smrg    echo "$0: no input file specified." >&2
2039fe995a9Smrg    exit 1
2049fe995a9Smrg  fi
2059d794632Smrg  # It's OK to call 'install-sh -d' without argument.
2069fe995a9Smrg  # This can happen when creating conditional directories.
2079fe995a9Smrg  exit 0
2089fe995a9Smrgfi
2099fe995a9Smrg
2109fe995a9Smrgif test -z "$dir_arg"; then
2119d794632Smrg  do_exit='(exit $ret); exit $ret'
2129d794632Smrg  trap "ret=129; $do_exit" 1
2139d794632Smrg  trap "ret=130; $do_exit" 2
2149d794632Smrg  trap "ret=141; $do_exit" 13
2159d794632Smrg  trap "ret=143; $do_exit" 15
2169fe995a9Smrg
2179fe995a9Smrg  # Set umask so as not to create temps with too-generous modes.
2189fe995a9Smrg  # However, 'strip' requires both read and write access to temps.
2199fe995a9Smrg  case $mode in
2209fe995a9Smrg    # Optimize common cases.
2219fe995a9Smrg    *644) cp_umask=133;;
2229fe995a9Smrg    *755) cp_umask=22;;
2239fe995a9Smrg
2249fe995a9Smrg    *[0-7])
2259fe995a9Smrg      if test -z "$stripcmd"; then
2269fe995a9Smrg	u_plus_rw=
2279fe995a9Smrg      else
2289fe995a9Smrg	u_plus_rw='% 200'
2299fe995a9Smrg      fi
2309fe995a9Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2319fe995a9Smrg    *)
2329fe995a9Smrg      if test -z "$stripcmd"; then
2339fe995a9Smrg	u_plus_rw=
2349fe995a9Smrg      else
2359fe995a9Smrg	u_plus_rw=,u+rw
2369fe995a9Smrg      fi
2379fe995a9Smrg      cp_umask=$mode$u_plus_rw;;
2389fe995a9Smrg  esac
2399fe995a9Smrgfi
2409fe995a9Smrg
2419fe995a9Smrgfor src
2429fe995a9Smrgdo
2439d794632Smrg  # Protect names problematic for 'test' and other utilities.
2449fe995a9Smrg  case $src in
2459d794632Smrg    -* | [=\(\)!]) src=./$src;;
2469fe995a9Smrg  esac
2479fe995a9Smrg
2489fe995a9Smrg  if test -n "$dir_arg"; then
2499fe995a9Smrg    dst=$src
2509fe995a9Smrg    dstdir=$dst
2519fe995a9Smrg    test -d "$dstdir"
2529fe995a9Smrg    dstdir_status=$?
2539fe995a9Smrg  else
2549fe995a9Smrg
2559fe995a9Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2569fe995a9Smrg    # might cause directories to be created, which would be especially bad
2579fe995a9Smrg    # if $src (and thus $dsttmp) contains '*'.
2589fe995a9Smrg    if test ! -f "$src" && test ! -d "$src"; then
2599fe995a9Smrg      echo "$0: $src does not exist." >&2
2609fe995a9Smrg      exit 1
2619fe995a9Smrg    fi
2629fe995a9Smrg
263b62cc08cSmrg    if test -z "$dst_arg"; then
2649fe995a9Smrg      echo "$0: no destination specified." >&2
2659fe995a9Smrg      exit 1
2669fe995a9Smrg    fi
267b62cc08cSmrg    dst=$dst_arg
2689fe995a9Smrg
2699fe995a9Smrg    # If destination is a directory, append the input filename; won't work
2709fe995a9Smrg    # if double slashes aren't ignored.
2719fe995a9Smrg    if test -d "$dst"; then
2729fe995a9Smrg      if test -n "$no_target_directory"; then
273b62cc08cSmrg	echo "$0: $dst_arg: Is a directory" >&2
2749fe995a9Smrg	exit 1
2759fe995a9Smrg      fi
2769fe995a9Smrg      dstdir=$dst
2779fe995a9Smrg      dst=$dstdir/`basename "$src"`
2789fe995a9Smrg      dstdir_status=0
2799fe995a9Smrg    else
2809fe995a9Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2819fe995a9Smrg      dstdir=`
2829fe995a9Smrg	(dirname "$dst") 2>/dev/null ||
2839fe995a9Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2849fe995a9Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2859fe995a9Smrg	     X"$dst" : 'X\(//\)$' \| \
2869fe995a9Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2879fe995a9Smrg	echo X"$dst" |
2889fe995a9Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2899fe995a9Smrg		   s//\1/
2909fe995a9Smrg		   q
2919fe995a9Smrg		 }
2929fe995a9Smrg		 /^X\(\/\/\)[^/].*/{
2939fe995a9Smrg		   s//\1/
2949fe995a9Smrg		   q
2959fe995a9Smrg		 }
2969fe995a9Smrg		 /^X\(\/\/\)$/{
2979fe995a9Smrg		   s//\1/
2989fe995a9Smrg		   q
2999fe995a9Smrg		 }
3009fe995a9Smrg		 /^X\(\/\).*/{
3019fe995a9Smrg		   s//\1/
3029fe995a9Smrg		   q
3039fe995a9Smrg		 }
3049fe995a9Smrg		 s/.*/./; q'
3059fe995a9Smrg      `
3069fe995a9Smrg
3079fe995a9Smrg      test -d "$dstdir"
3089fe995a9Smrg      dstdir_status=$?
3099fe995a9Smrg    fi
3109fe995a9Smrg  fi
3119fe995a9Smrg
3129fe995a9Smrg  obsolete_mkdir_used=false
3139fe995a9Smrg
3149fe995a9Smrg  if test $dstdir_status != 0; then
3159fe995a9Smrg    case $posix_mkdir in
3169fe995a9Smrg      '')
3179fe995a9Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3189fe995a9Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3199fe995a9Smrg	umask=`umask`
3209fe995a9Smrg	case $stripcmd.$umask in
3219fe995a9Smrg	  # Optimize common cases.
3229fe995a9Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3239fe995a9Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3249fe995a9Smrg
3259fe995a9Smrg	  *[0-7])
3269fe995a9Smrg	    mkdir_umask=`expr $umask + 22 \
3279fe995a9Smrg	      - $umask % 100 % 40 + $umask % 20 \
3289fe995a9Smrg	      - $umask % 10 % 4 + $umask % 2
3299fe995a9Smrg	    `;;
3309fe995a9Smrg	  *) mkdir_umask=$umask,go-w;;
3319fe995a9Smrg	esac
3329fe995a9Smrg
3339fe995a9Smrg	# With -d, create the new directory with the user-specified mode.
3349fe995a9Smrg	# Otherwise, rely on $mkdir_umask.
3359fe995a9Smrg	if test -n "$dir_arg"; then
3369fe995a9Smrg	  mkdir_mode=-m$mode
3379fe995a9Smrg	else
3389fe995a9Smrg	  mkdir_mode=
3399fe995a9Smrg	fi
3409fe995a9Smrg
3419fe995a9Smrg	posix_mkdir=false
3429fe995a9Smrg	case $umask in
3439fe995a9Smrg	  *[123567][0-7][0-7])
3449fe995a9Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3459fe995a9Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3469fe995a9Smrg	    ;;
3479fe995a9Smrg	  *)
3489fe995a9Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3499fe995a9Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3509fe995a9Smrg
3519fe995a9Smrg	    if (umask $mkdir_umask &&
3529fe995a9Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3539fe995a9Smrg	    then
3549fe995a9Smrg	      if test -z "$dir_arg" || {
3559fe995a9Smrg		   # Check for POSIX incompatibilities with -m.
3569fe995a9Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3579d794632Smrg		   # other-writable bit of parent directory when it shouldn't.
3589fe995a9Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3599fe995a9Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3609fe995a9Smrg		   case $ls_ld_tmpdir in
3619fe995a9Smrg		     d????-?r-*) different_mode=700;;
3629fe995a9Smrg		     d????-?--*) different_mode=755;;
3639fe995a9Smrg		     *) false;;
3649fe995a9Smrg		   esac &&
3659fe995a9Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3669fe995a9Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3679fe995a9Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3689fe995a9Smrg		   }
3699fe995a9Smrg		 }
3709fe995a9Smrg	      then posix_mkdir=:
3719fe995a9Smrg	      fi
3729fe995a9Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3739fe995a9Smrg	    else
3749fe995a9Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3759fe995a9Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3769fe995a9Smrg	    fi
3779fe995a9Smrg	    trap '' 0;;
3789fe995a9Smrg	esac;;
3799fe995a9Smrg    esac
3809fe995a9Smrg
3819fe995a9Smrg    if
3829fe995a9Smrg      $posix_mkdir && (
3839fe995a9Smrg	umask $mkdir_umask &&
3849fe995a9Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3859fe995a9Smrg      )
3869fe995a9Smrg    then :
3879fe995a9Smrg    else
3889fe995a9Smrg
3899fe995a9Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3909fe995a9Smrg      # or it failed possibly due to a race condition.  Create the
3919fe995a9Smrg      # directory the slow way, step by step, checking for races as we go.
3929fe995a9Smrg
3939fe995a9Smrg      case $dstdir in
394b62cc08cSmrg	/*) prefix='/';;
3959d794632Smrg	[-=\(\)!]*) prefix='./';;
396b62cc08cSmrg	*)  prefix='';;
3979fe995a9Smrg      esac
3989fe995a9Smrg
399b62cc08cSmrg      eval "$initialize_posix_glob"
4009fe995a9Smrg
4019fe995a9Smrg      oIFS=$IFS
4029fe995a9Smrg      IFS=/
403b62cc08cSmrg      $posix_glob set -f
4049fe995a9Smrg      set fnord $dstdir
4059fe995a9Smrg      shift
406b62cc08cSmrg      $posix_glob set +f
4079fe995a9Smrg      IFS=$oIFS
4089fe995a9Smrg
4099fe995a9Smrg      prefixes=
4109fe995a9Smrg
4119fe995a9Smrg      for d
4129fe995a9Smrg      do
4139d794632Smrg	test X"$d" = X && continue
4149fe995a9Smrg
4159fe995a9Smrg	prefix=$prefix$d
4169fe995a9Smrg	if test -d "$prefix"; then
4179fe995a9Smrg	  prefixes=
4189fe995a9Smrg	else
4199fe995a9Smrg	  if $posix_mkdir; then
4209fe995a9Smrg	    (umask=$mkdir_umask &&
4219fe995a9Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4229fe995a9Smrg	    # Don't fail if two instances are running concurrently.
4239fe995a9Smrg	    test -d "$prefix" || exit 1
4249fe995a9Smrg	  else
4259fe995a9Smrg	    case $prefix in
4269fe995a9Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4279fe995a9Smrg	      *) qprefix=$prefix;;
4289fe995a9Smrg	    esac
4299fe995a9Smrg	    prefixes="$prefixes '$qprefix'"
4309fe995a9Smrg	  fi
4319fe995a9Smrg	fi
4329fe995a9Smrg	prefix=$prefix/
4339fe995a9Smrg      done
4349fe995a9Smrg
4359fe995a9Smrg      if test -n "$prefixes"; then
4369fe995a9Smrg	# Don't fail if two instances are running concurrently.
4379fe995a9Smrg	(umask $mkdir_umask &&
4389fe995a9Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4399fe995a9Smrg	  test -d "$dstdir" || exit 1
4409fe995a9Smrg	obsolete_mkdir_used=true
4419fe995a9Smrg      fi
4429fe995a9Smrg    fi
4439fe995a9Smrg  fi
4449fe995a9Smrg
4459fe995a9Smrg  if test -n "$dir_arg"; then
4469fe995a9Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4479fe995a9Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4489fe995a9Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4499fe995a9Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4509fe995a9Smrg  else
4519fe995a9Smrg
4529fe995a9Smrg    # Make a couple of temp file names in the proper directory.
4539fe995a9Smrg    dsttmp=$dstdir/_inst.$$_
4549fe995a9Smrg    rmtmp=$dstdir/_rm.$$_
4559fe995a9Smrg
4569fe995a9Smrg    # Trap to clean up those temp files at exit.
4579fe995a9Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4589fe995a9Smrg
4599fe995a9Smrg    # Copy the file name to the temp name.
4609fe995a9Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4619fe995a9Smrg
4629fe995a9Smrg    # and set any options; do chmod last to preserve setuid bits.
4639fe995a9Smrg    #
4649fe995a9Smrg    # If any of these fail, we abort the whole thing.  If we want to
4659fe995a9Smrg    # ignore errors from any of these, just make sure not to ignore
4669fe995a9Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4679fe995a9Smrg    #
468b62cc08cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469b62cc08cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470b62cc08cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471b62cc08cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472b62cc08cSmrg
473b62cc08cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
474b62cc08cSmrg    if $copy_on_change &&
475b62cc08cSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476b62cc08cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477b62cc08cSmrg
478b62cc08cSmrg       eval "$initialize_posix_glob" &&
479b62cc08cSmrg       $posix_glob set -f &&
480b62cc08cSmrg       set X $old && old=:$2:$4:$5:$6 &&
481b62cc08cSmrg       set X $new && new=:$2:$4:$5:$6 &&
482b62cc08cSmrg       $posix_glob set +f &&
483b62cc08cSmrg
484b62cc08cSmrg       test "$old" = "$new" &&
485b62cc08cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486b62cc08cSmrg    then
487b62cc08cSmrg      rm -f "$dsttmp"
488b62cc08cSmrg    else
489b62cc08cSmrg      # Rename the file to the real destination.
490b62cc08cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491b62cc08cSmrg
492b62cc08cSmrg      # The rename failed, perhaps because mv can't rename something else
493b62cc08cSmrg      # to itself, or perhaps because mv is so ancient that it does not
494b62cc08cSmrg      # support -f.
495b62cc08cSmrg      {
496b62cc08cSmrg	# Now remove or move aside any old file at destination location.
497b62cc08cSmrg	# We try this two ways since rm can't unlink itself on some
498b62cc08cSmrg	# systems and the destination file might be busy for other
499b62cc08cSmrg	# reasons.  In this case, the final cleanup might fail but the new
500b62cc08cSmrg	# file should still install successfully.
501b62cc08cSmrg	{
502b62cc08cSmrg	  test ! -f "$dst" ||
503b62cc08cSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504b62cc08cSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505b62cc08cSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506b62cc08cSmrg	  } ||
507b62cc08cSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
508b62cc08cSmrg	    (exit 1); exit 1
509b62cc08cSmrg	  }
510b62cc08cSmrg	} &&
511b62cc08cSmrg
512b62cc08cSmrg	# Now rename the file to the real destination.
513b62cc08cSmrg	$doit $mvcmd "$dsttmp" "$dst"
514b62cc08cSmrg      }
515b62cc08cSmrg    fi || exit 1
5169fe995a9Smrg
5179fe995a9Smrg    trap '' 0
5189fe995a9Smrg  fi
5199fe995a9Smrgdone
5209fe995a9Smrg
5219fe995a9Smrg# Local variables:
5229fe995a9Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5239fe995a9Smrg# time-stamp-start: "scriptversion="
5249fe995a9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525b62cc08cSmrg# time-stamp-time-zone: "UTC"
526b62cc08cSmrg# time-stamp-end: "; # UTC"
5279fe995a9Smrg# End:
528