install-sh revision 1a30de1f
11a30de1fSmrg#!/bin/sh
21a30de1fSmrg# install - install a program, script, or datafile
31a30de1fSmrg
41a30de1fSmrgscriptversion=2006-10-14.15
51a30de1fSmrg
61a30de1fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71a30de1fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
81a30de1fSmrg# following copyright and license.
91a30de1fSmrg#
101a30de1fSmrg# Copyright (C) 1994 X Consortium
111a30de1fSmrg#
121a30de1fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131a30de1fSmrg# of this software and associated documentation files (the "Software"), to
141a30de1fSmrg# deal in the Software without restriction, including without limitation the
151a30de1fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161a30de1fSmrg# sell copies of the Software, and to permit persons to whom the Software is
171a30de1fSmrg# furnished to do so, subject to the following conditions:
181a30de1fSmrg#
191a30de1fSmrg# The above copyright notice and this permission notice shall be included in
201a30de1fSmrg# all copies or substantial portions of the Software.
211a30de1fSmrg#
221a30de1fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231a30de1fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241a30de1fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251a30de1fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261a30de1fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271a30de1fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281a30de1fSmrg#
291a30de1fSmrg# Except as contained in this notice, the name of the X Consortium shall not
301a30de1fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
311a30de1fSmrg# ings in this Software without prior written authorization from the X Consor-
321a30de1fSmrg# tium.
331a30de1fSmrg#
341a30de1fSmrg#
351a30de1fSmrg# FSF changes to this file are in the public domain.
361a30de1fSmrg#
371a30de1fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
381a30de1fSmrg# `make' implicit rules from creating a file called install from it
391a30de1fSmrg# when there is no Makefile.
401a30de1fSmrg#
411a30de1fSmrg# This script is compatible with the BSD install script, but was written
421a30de1fSmrg# from scratch.
431a30de1fSmrg
441a30de1fSmrgnl='
451a30de1fSmrg'
461a30de1fSmrgIFS=" ""	$nl"
471a30de1fSmrg
481a30de1fSmrg# set DOITPROG to echo to test this script
491a30de1fSmrg
501a30de1fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
511a30de1fSmrgdoit="${DOITPROG-}"
521a30de1fSmrgif test -z "$doit"; then
531a30de1fSmrg  doit_exec=exec
541a30de1fSmrgelse
551a30de1fSmrg  doit_exec=$doit
561a30de1fSmrgfi
571a30de1fSmrg
581a30de1fSmrg# Put in absolute file names if you don't have them in your path;
591a30de1fSmrg# or use environment vars.
601a30de1fSmrg
611a30de1fSmrgmvprog="${MVPROG-mv}"
621a30de1fSmrgcpprog="${CPPROG-cp}"
631a30de1fSmrgchmodprog="${CHMODPROG-chmod}"
641a30de1fSmrgchownprog="${CHOWNPROG-chown}"
651a30de1fSmrgchgrpprog="${CHGRPPROG-chgrp}"
661a30de1fSmrgstripprog="${STRIPPROG-strip}"
671a30de1fSmrgrmprog="${RMPROG-rm}"
681a30de1fSmrgmkdirprog="${MKDIRPROG-mkdir}"
691a30de1fSmrg
701a30de1fSmrgposix_glob=
711a30de1fSmrgposix_mkdir=
721a30de1fSmrg
731a30de1fSmrg# Desired mode of installed file.
741a30de1fSmrgmode=0755
751a30de1fSmrg
761a30de1fSmrgchmodcmd=$chmodprog
771a30de1fSmrgchowncmd=
781a30de1fSmrgchgrpcmd=
791a30de1fSmrgstripcmd=
801a30de1fSmrgrmcmd="$rmprog -f"
811a30de1fSmrgmvcmd="$mvprog"
821a30de1fSmrgsrc=
831a30de1fSmrgdst=
841a30de1fSmrgdir_arg=
851a30de1fSmrgdstarg=
861a30de1fSmrgno_target_directory=
871a30de1fSmrg
881a30de1fSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
891a30de1fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
901a30de1fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
911a30de1fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
921a30de1fSmrg
931a30de1fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
941a30de1fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
951a30de1fSmrgIn the 4th, create DIRECTORIES.
961a30de1fSmrg
971a30de1fSmrgOptions:
981a30de1fSmrg-c         (ignored)
991a30de1fSmrg-d         create directories instead of installing files.
1001a30de1fSmrg-g GROUP   $chgrpprog installed files to GROUP.
1011a30de1fSmrg-m MODE    $chmodprog installed files to MODE.
1021a30de1fSmrg-o USER    $chownprog installed files to USER.
1031a30de1fSmrg-s         $stripprog installed files.
1041a30de1fSmrg-t DIRECTORY  install into DIRECTORY.
1051a30de1fSmrg-T         report an error if DSTFILE is a directory.
1061a30de1fSmrg--help     display this help and exit.
1071a30de1fSmrg--version  display version info and exit.
1081a30de1fSmrg
1091a30de1fSmrgEnvironment variables override the default commands:
1101a30de1fSmrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
1111a30de1fSmrg"
1121a30de1fSmrg
1131a30de1fSmrgwhile test $# -ne 0; do
1141a30de1fSmrg  case $1 in
1151a30de1fSmrg    -c) shift
1161a30de1fSmrg        continue;;
1171a30de1fSmrg
1181a30de1fSmrg    -d) dir_arg=true
1191a30de1fSmrg        shift
1201a30de1fSmrg        continue;;
1211a30de1fSmrg
1221a30de1fSmrg    -g) chgrpcmd="$chgrpprog $2"
1231a30de1fSmrg        shift
1241a30de1fSmrg        shift
1251a30de1fSmrg        continue;;
1261a30de1fSmrg
1271a30de1fSmrg    --help) echo "$usage"; exit $?;;
1281a30de1fSmrg
1291a30de1fSmrg    -m) mode=$2
1301a30de1fSmrg        shift
1311a30de1fSmrg        shift
1321a30de1fSmrg	case $mode in
1331a30de1fSmrg	  *' '* | *'	'* | *'
1341a30de1fSmrg'*	  | *'*'* | *'?'* | *'['*)
1351a30de1fSmrg	    echo "$0: invalid mode: $mode" >&2
1361a30de1fSmrg	    exit 1;;
1371a30de1fSmrg	esac
1381a30de1fSmrg        continue;;
1391a30de1fSmrg
1401a30de1fSmrg    -o) chowncmd="$chownprog $2"
1411a30de1fSmrg        shift
1421a30de1fSmrg        shift
1431a30de1fSmrg        continue;;
1441a30de1fSmrg
1451a30de1fSmrg    -s) stripcmd=$stripprog
1461a30de1fSmrg        shift
1471a30de1fSmrg        continue;;
1481a30de1fSmrg
1491a30de1fSmrg    -t) dstarg=$2
1501a30de1fSmrg	shift
1511a30de1fSmrg	shift
1521a30de1fSmrg	continue;;
1531a30de1fSmrg
1541a30de1fSmrg    -T) no_target_directory=true
1551a30de1fSmrg	shift
1561a30de1fSmrg	continue;;
1571a30de1fSmrg
1581a30de1fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1591a30de1fSmrg
1601a30de1fSmrg    --)	shift
1611a30de1fSmrg	break;;
1621a30de1fSmrg
1631a30de1fSmrg    -*)	echo "$0: invalid option: $1" >&2
1641a30de1fSmrg	exit 1;;
1651a30de1fSmrg
1661a30de1fSmrg    *)  break;;
1671a30de1fSmrg  esac
1681a30de1fSmrgdone
1691a30de1fSmrg
1701a30de1fSmrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
1711a30de1fSmrg  # When -d is used, all remaining arguments are directories to create.
1721a30de1fSmrg  # When -t is used, the destination is already specified.
1731a30de1fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1741a30de1fSmrg  for arg
1751a30de1fSmrg  do
1761a30de1fSmrg    if test -n "$dstarg"; then
1771a30de1fSmrg      # $@ is not empty: it contains at least $arg.
1781a30de1fSmrg      set fnord "$@" "$dstarg"
1791a30de1fSmrg      shift # fnord
1801a30de1fSmrg    fi
1811a30de1fSmrg    shift # arg
1821a30de1fSmrg    dstarg=$arg
1831a30de1fSmrg  done
1841a30de1fSmrgfi
1851a30de1fSmrg
1861a30de1fSmrgif test $# -eq 0; then
1871a30de1fSmrg  if test -z "$dir_arg"; then
1881a30de1fSmrg    echo "$0: no input file specified." >&2
1891a30de1fSmrg    exit 1
1901a30de1fSmrg  fi
1911a30de1fSmrg  # It's OK to call `install-sh -d' without argument.
1921a30de1fSmrg  # This can happen when creating conditional directories.
1931a30de1fSmrg  exit 0
1941a30de1fSmrgfi
1951a30de1fSmrg
1961a30de1fSmrgif test -z "$dir_arg"; then
1971a30de1fSmrg  trap '(exit $?); exit' 1 2 13 15
1981a30de1fSmrg
1991a30de1fSmrg  # Set umask so as not to create temps with too-generous modes.
2001a30de1fSmrg  # However, 'strip' requires both read and write access to temps.
2011a30de1fSmrg  case $mode in
2021a30de1fSmrg    # Optimize common cases.
2031a30de1fSmrg    *644) cp_umask=133;;
2041a30de1fSmrg    *755) cp_umask=22;;
2051a30de1fSmrg
2061a30de1fSmrg    *[0-7])
2071a30de1fSmrg      if test -z "$stripcmd"; then
2081a30de1fSmrg	u_plus_rw=
2091a30de1fSmrg      else
2101a30de1fSmrg	u_plus_rw='% 200'
2111a30de1fSmrg      fi
2121a30de1fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2131a30de1fSmrg    *)
2141a30de1fSmrg      if test -z "$stripcmd"; then
2151a30de1fSmrg	u_plus_rw=
2161a30de1fSmrg      else
2171a30de1fSmrg	u_plus_rw=,u+rw
2181a30de1fSmrg      fi
2191a30de1fSmrg      cp_umask=$mode$u_plus_rw;;
2201a30de1fSmrg  esac
2211a30de1fSmrgfi
2221a30de1fSmrg
2231a30de1fSmrgfor src
2241a30de1fSmrgdo
2251a30de1fSmrg  # Protect names starting with `-'.
2261a30de1fSmrg  case $src in
2271a30de1fSmrg    -*) src=./$src ;;
2281a30de1fSmrg  esac
2291a30de1fSmrg
2301a30de1fSmrg  if test -n "$dir_arg"; then
2311a30de1fSmrg    dst=$src
2321a30de1fSmrg    dstdir=$dst
2331a30de1fSmrg    test -d "$dstdir"
2341a30de1fSmrg    dstdir_status=$?
2351a30de1fSmrg  else
2361a30de1fSmrg
2371a30de1fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2381a30de1fSmrg    # might cause directories to be created, which would be especially bad
2391a30de1fSmrg    # if $src (and thus $dsttmp) contains '*'.
2401a30de1fSmrg    if test ! -f "$src" && test ! -d "$src"; then
2411a30de1fSmrg      echo "$0: $src does not exist." >&2
2421a30de1fSmrg      exit 1
2431a30de1fSmrg    fi
2441a30de1fSmrg
2451a30de1fSmrg    if test -z "$dstarg"; then
2461a30de1fSmrg      echo "$0: no destination specified." >&2
2471a30de1fSmrg      exit 1
2481a30de1fSmrg    fi
2491a30de1fSmrg
2501a30de1fSmrg    dst=$dstarg
2511a30de1fSmrg    # Protect names starting with `-'.
2521a30de1fSmrg    case $dst in
2531a30de1fSmrg      -*) dst=./$dst ;;
2541a30de1fSmrg    esac
2551a30de1fSmrg
2561a30de1fSmrg    # If destination is a directory, append the input filename; won't work
2571a30de1fSmrg    # if double slashes aren't ignored.
2581a30de1fSmrg    if test -d "$dst"; then
2591a30de1fSmrg      if test -n "$no_target_directory"; then
2601a30de1fSmrg	echo "$0: $dstarg: Is a directory" >&2
2611a30de1fSmrg	exit 1
2621a30de1fSmrg      fi
2631a30de1fSmrg      dstdir=$dst
2641a30de1fSmrg      dst=$dstdir/`basename "$src"`
2651a30de1fSmrg      dstdir_status=0
2661a30de1fSmrg    else
2671a30de1fSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2681a30de1fSmrg      dstdir=`
2691a30de1fSmrg	(dirname "$dst") 2>/dev/null ||
2701a30de1fSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2711a30de1fSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2721a30de1fSmrg	     X"$dst" : 'X\(//\)$' \| \
2731a30de1fSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2741a30de1fSmrg	echo X"$dst" |
2751a30de1fSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2761a30de1fSmrg		   s//\1/
2771a30de1fSmrg		   q
2781a30de1fSmrg		 }
2791a30de1fSmrg		 /^X\(\/\/\)[^/].*/{
2801a30de1fSmrg		   s//\1/
2811a30de1fSmrg		   q
2821a30de1fSmrg		 }
2831a30de1fSmrg		 /^X\(\/\/\)$/{
2841a30de1fSmrg		   s//\1/
2851a30de1fSmrg		   q
2861a30de1fSmrg		 }
2871a30de1fSmrg		 /^X\(\/\).*/{
2881a30de1fSmrg		   s//\1/
2891a30de1fSmrg		   q
2901a30de1fSmrg		 }
2911a30de1fSmrg		 s/.*/./; q'
2921a30de1fSmrg      `
2931a30de1fSmrg
2941a30de1fSmrg      test -d "$dstdir"
2951a30de1fSmrg      dstdir_status=$?
2961a30de1fSmrg    fi
2971a30de1fSmrg  fi
2981a30de1fSmrg
2991a30de1fSmrg  obsolete_mkdir_used=false
3001a30de1fSmrg
3011a30de1fSmrg  if test $dstdir_status != 0; then
3021a30de1fSmrg    case $posix_mkdir in
3031a30de1fSmrg      '')
3041a30de1fSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3051a30de1fSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3061a30de1fSmrg	umask=`umask`
3071a30de1fSmrg	case $stripcmd.$umask in
3081a30de1fSmrg	  # Optimize common cases.
3091a30de1fSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3101a30de1fSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3111a30de1fSmrg
3121a30de1fSmrg	  *[0-7])
3131a30de1fSmrg	    mkdir_umask=`expr $umask + 22 \
3141a30de1fSmrg	      - $umask % 100 % 40 + $umask % 20 \
3151a30de1fSmrg	      - $umask % 10 % 4 + $umask % 2
3161a30de1fSmrg	    `;;
3171a30de1fSmrg	  *) mkdir_umask=$umask,go-w;;
3181a30de1fSmrg	esac
3191a30de1fSmrg
3201a30de1fSmrg	# With -d, create the new directory with the user-specified mode.
3211a30de1fSmrg	# Otherwise, rely on $mkdir_umask.
3221a30de1fSmrg	if test -n "$dir_arg"; then
3231a30de1fSmrg	  mkdir_mode=-m$mode
3241a30de1fSmrg	else
3251a30de1fSmrg	  mkdir_mode=
3261a30de1fSmrg	fi
3271a30de1fSmrg
3281a30de1fSmrg	posix_mkdir=false
3291a30de1fSmrg	case $umask in
3301a30de1fSmrg	  *[123567][0-7][0-7])
3311a30de1fSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3321a30de1fSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3331a30de1fSmrg	    ;;
3341a30de1fSmrg	  *)
3351a30de1fSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3361a30de1fSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3371a30de1fSmrg
3381a30de1fSmrg	    if (umask $mkdir_umask &&
3391a30de1fSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3401a30de1fSmrg	    then
3411a30de1fSmrg	      if test -z "$dir_arg" || {
3421a30de1fSmrg		   # Check for POSIX incompatibilities with -m.
3431a30de1fSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3441a30de1fSmrg		   # other-writeable bit of parent directory when it shouldn't.
3451a30de1fSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3461a30de1fSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3471a30de1fSmrg		   case $ls_ld_tmpdir in
3481a30de1fSmrg		     d????-?r-*) different_mode=700;;
3491a30de1fSmrg		     d????-?--*) different_mode=755;;
3501a30de1fSmrg		     *) false;;
3511a30de1fSmrg		   esac &&
3521a30de1fSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3531a30de1fSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3541a30de1fSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3551a30de1fSmrg		   }
3561a30de1fSmrg		 }
3571a30de1fSmrg	      then posix_mkdir=:
3581a30de1fSmrg	      fi
3591a30de1fSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3601a30de1fSmrg	    else
3611a30de1fSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3621a30de1fSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3631a30de1fSmrg	    fi
3641a30de1fSmrg	    trap '' 0;;
3651a30de1fSmrg	esac;;
3661a30de1fSmrg    esac
3671a30de1fSmrg
3681a30de1fSmrg    if
3691a30de1fSmrg      $posix_mkdir && (
3701a30de1fSmrg	umask $mkdir_umask &&
3711a30de1fSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3721a30de1fSmrg      )
3731a30de1fSmrg    then :
3741a30de1fSmrg    else
3751a30de1fSmrg
3761a30de1fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3771a30de1fSmrg      # or it failed possibly due to a race condition.  Create the
3781a30de1fSmrg      # directory the slow way, step by step, checking for races as we go.
3791a30de1fSmrg
3801a30de1fSmrg      case $dstdir in
3811a30de1fSmrg	/*) prefix=/ ;;
3821a30de1fSmrg	-*) prefix=./ ;;
3831a30de1fSmrg	*)  prefix= ;;
3841a30de1fSmrg      esac
3851a30de1fSmrg
3861a30de1fSmrg      case $posix_glob in
3871a30de1fSmrg        '')
3881a30de1fSmrg	  if (set -f) 2>/dev/null; then
3891a30de1fSmrg	    posix_glob=true
3901a30de1fSmrg	  else
3911a30de1fSmrg	    posix_glob=false
3921a30de1fSmrg	  fi ;;
3931a30de1fSmrg      esac
3941a30de1fSmrg
3951a30de1fSmrg      oIFS=$IFS
3961a30de1fSmrg      IFS=/
3971a30de1fSmrg      $posix_glob && set -f
3981a30de1fSmrg      set fnord $dstdir
3991a30de1fSmrg      shift
4001a30de1fSmrg      $posix_glob && set +f
4011a30de1fSmrg      IFS=$oIFS
4021a30de1fSmrg
4031a30de1fSmrg      prefixes=
4041a30de1fSmrg
4051a30de1fSmrg      for d
4061a30de1fSmrg      do
4071a30de1fSmrg	test -z "$d" && continue
4081a30de1fSmrg
4091a30de1fSmrg	prefix=$prefix$d
4101a30de1fSmrg	if test -d "$prefix"; then
4111a30de1fSmrg	  prefixes=
4121a30de1fSmrg	else
4131a30de1fSmrg	  if $posix_mkdir; then
4141a30de1fSmrg	    (umask=$mkdir_umask &&
4151a30de1fSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4161a30de1fSmrg	    # Don't fail if two instances are running concurrently.
4171a30de1fSmrg	    test -d "$prefix" || exit 1
4181a30de1fSmrg	  else
4191a30de1fSmrg	    case $prefix in
4201a30de1fSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4211a30de1fSmrg	      *) qprefix=$prefix;;
4221a30de1fSmrg	    esac
4231a30de1fSmrg	    prefixes="$prefixes '$qprefix'"
4241a30de1fSmrg	  fi
4251a30de1fSmrg	fi
4261a30de1fSmrg	prefix=$prefix/
4271a30de1fSmrg      done
4281a30de1fSmrg
4291a30de1fSmrg      if test -n "$prefixes"; then
4301a30de1fSmrg	# Don't fail if two instances are running concurrently.
4311a30de1fSmrg	(umask $mkdir_umask &&
4321a30de1fSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4331a30de1fSmrg	  test -d "$dstdir" || exit 1
4341a30de1fSmrg	obsolete_mkdir_used=true
4351a30de1fSmrg      fi
4361a30de1fSmrg    fi
4371a30de1fSmrg  fi
4381a30de1fSmrg
4391a30de1fSmrg  if test -n "$dir_arg"; then
4401a30de1fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4411a30de1fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4421a30de1fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4431a30de1fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4441a30de1fSmrg  else
4451a30de1fSmrg
4461a30de1fSmrg    # Make a couple of temp file names in the proper directory.
4471a30de1fSmrg    dsttmp=$dstdir/_inst.$$_
4481a30de1fSmrg    rmtmp=$dstdir/_rm.$$_
4491a30de1fSmrg
4501a30de1fSmrg    # Trap to clean up those temp files at exit.
4511a30de1fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4521a30de1fSmrg
4531a30de1fSmrg    # Copy the file name to the temp name.
4541a30de1fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4551a30de1fSmrg
4561a30de1fSmrg    # and set any options; do chmod last to preserve setuid bits.
4571a30de1fSmrg    #
4581a30de1fSmrg    # If any of these fail, we abort the whole thing.  If we want to
4591a30de1fSmrg    # ignore errors from any of these, just make sure not to ignore
4601a30de1fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4611a30de1fSmrg    #
4621a30de1fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
4631a30de1fSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
4641a30de1fSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
4651a30de1fSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4661a30de1fSmrg
4671a30de1fSmrg    # Now rename the file to the real destination.
4681a30de1fSmrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
4691a30de1fSmrg      || {
4701a30de1fSmrg	   # The rename failed, perhaps because mv can't rename something else
4711a30de1fSmrg	   # to itself, or perhaps because mv is so ancient that it does not
4721a30de1fSmrg	   # support -f.
4731a30de1fSmrg
4741a30de1fSmrg	   # Now remove or move aside any old file at destination location.
4751a30de1fSmrg	   # We try this two ways since rm can't unlink itself on some
4761a30de1fSmrg	   # systems and the destination file might be busy for other
4771a30de1fSmrg	   # reasons.  In this case, the final cleanup might fail but the new
4781a30de1fSmrg	   # file should still install successfully.
4791a30de1fSmrg	   {
4801a30de1fSmrg	     if test -f "$dst"; then
4811a30de1fSmrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
4821a30de1fSmrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
4831a30de1fSmrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
4841a30de1fSmrg	       || {
4851a30de1fSmrg		 echo "$0: cannot unlink or rename $dst" >&2
4861a30de1fSmrg		 (exit 1); exit 1
4871a30de1fSmrg	       }
4881a30de1fSmrg	     else
4891a30de1fSmrg	       :
4901a30de1fSmrg	     fi
4911a30de1fSmrg	   } &&
4921a30de1fSmrg
4931a30de1fSmrg	   # Now rename the file to the real destination.
4941a30de1fSmrg	   $doit $mvcmd "$dsttmp" "$dst"
4951a30de1fSmrg	 }
4961a30de1fSmrg    } || exit 1
4971a30de1fSmrg
4981a30de1fSmrg    trap '' 0
4991a30de1fSmrg  fi
5001a30de1fSmrgdone
5011a30de1fSmrg
5021a30de1fSmrg# Local variables:
5031a30de1fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5041a30de1fSmrg# time-stamp-start: "scriptversion="
5051a30de1fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5061a30de1fSmrg# time-stamp-end: "$"
5071a30de1fSmrg# End:
508