install-sh revision 8e0ed500
117a48c7cSmrg#!/bin/sh
217a48c7cSmrg# install - install a program, script, or datafile
317a48c7cSmrg
48e0ed500Smrgscriptversion=2006-10-14.15
517a48c7cSmrg
617a48c7cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
717a48c7cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
817a48c7cSmrg# following copyright and license.
917a48c7cSmrg#
1017a48c7cSmrg# Copyright (C) 1994 X Consortium
1117a48c7cSmrg#
1217a48c7cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1317a48c7cSmrg# of this software and associated documentation files (the "Software"), to
1417a48c7cSmrg# deal in the Software without restriction, including without limitation the
1517a48c7cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1617a48c7cSmrg# sell copies of the Software, and to permit persons to whom the Software is
1717a48c7cSmrg# furnished to do so, subject to the following conditions:
1817a48c7cSmrg#
1917a48c7cSmrg# The above copyright notice and this permission notice shall be included in
2017a48c7cSmrg# all copies or substantial portions of the Software.
2117a48c7cSmrg#
2217a48c7cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2317a48c7cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2417a48c7cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2517a48c7cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2617a48c7cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2717a48c7cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2817a48c7cSmrg#
2917a48c7cSmrg# Except as contained in this notice, the name of the X Consortium shall not
3017a48c7cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3117a48c7cSmrg# ings in this Software without prior written authorization from the X Consor-
3217a48c7cSmrg# tium.
3317a48c7cSmrg#
3417a48c7cSmrg#
3517a48c7cSmrg# FSF changes to this file are in the public domain.
3617a48c7cSmrg#
3717a48c7cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3817a48c7cSmrg# `make' implicit rules from creating a file called install from it
3917a48c7cSmrg# when there is no Makefile.
4017a48c7cSmrg#
4117a48c7cSmrg# This script is compatible with the BSD install script, but was written
428e0ed500Smrg# from scratch.
438e0ed500Smrg
448e0ed500Smrgnl='
458e0ed500Smrg'
468e0ed500SmrgIFS=" ""	$nl"
4717a48c7cSmrg
4817a48c7cSmrg# set DOITPROG to echo to test this script
4917a48c7cSmrg
5017a48c7cSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5117a48c7cSmrgdoit="${DOITPROG-}"
528e0ed500Smrgif test -z "$doit"; then
538e0ed500Smrg  doit_exec=exec
548e0ed500Smrgelse
558e0ed500Smrg  doit_exec=$doit
568e0ed500Smrgfi
5717a48c7cSmrg
588e0ed500Smrg# Put in absolute file names if you don't have them in your path;
598e0ed500Smrg# or use environment vars.
6017a48c7cSmrg
6117a48c7cSmrgmvprog="${MVPROG-mv}"
6217a48c7cSmrgcpprog="${CPPROG-cp}"
6317a48c7cSmrgchmodprog="${CHMODPROG-chmod}"
6417a48c7cSmrgchownprog="${CHOWNPROG-chown}"
6517a48c7cSmrgchgrpprog="${CHGRPPROG-chgrp}"
6617a48c7cSmrgstripprog="${STRIPPROG-strip}"
6717a48c7cSmrgrmprog="${RMPROG-rm}"
6817a48c7cSmrgmkdirprog="${MKDIRPROG-mkdir}"
6917a48c7cSmrg
708e0ed500Smrgposix_glob=
718e0ed500Smrgposix_mkdir=
728e0ed500Smrg
738e0ed500Smrg# Desired mode of installed file.
748e0ed500Smrgmode=0755
758e0ed500Smrg
768e0ed500Smrgchmodcmd=$chmodprog
7717a48c7cSmrgchowncmd=
7817a48c7cSmrgchgrpcmd=
7917a48c7cSmrgstripcmd=
8017a48c7cSmrgrmcmd="$rmprog -f"
8117a48c7cSmrgmvcmd="$mvprog"
8217a48c7cSmrgsrc=
8317a48c7cSmrgdst=
8417a48c7cSmrgdir_arg=
8517a48c7cSmrgdstarg=
8617a48c7cSmrgno_target_directory=
8717a48c7cSmrg
8817a48c7cSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8917a48c7cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9017a48c7cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9117a48c7cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9217a48c7cSmrg
9317a48c7cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9417a48c7cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9517a48c7cSmrgIn the 4th, create DIRECTORIES.
9617a48c7cSmrg
9717a48c7cSmrgOptions:
9817a48c7cSmrg-c         (ignored)
9917a48c7cSmrg-d         create directories instead of installing files.
10017a48c7cSmrg-g GROUP   $chgrpprog installed files to GROUP.
10117a48c7cSmrg-m MODE    $chmodprog installed files to MODE.
10217a48c7cSmrg-o USER    $chownprog installed files to USER.
10317a48c7cSmrg-s         $stripprog installed files.
10417a48c7cSmrg-t DIRECTORY  install into DIRECTORY.
10517a48c7cSmrg-T         report an error if DSTFILE is a directory.
10617a48c7cSmrg--help     display this help and exit.
10717a48c7cSmrg--version  display version info and exit.
10817a48c7cSmrg
10917a48c7cSmrgEnvironment variables override the default commands:
11017a48c7cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
11117a48c7cSmrg"
11217a48c7cSmrg
1138e0ed500Smrgwhile test $# -ne 0; do
11417a48c7cSmrg  case $1 in
11517a48c7cSmrg    -c) shift
11617a48c7cSmrg        continue;;
11717a48c7cSmrg
11817a48c7cSmrg    -d) dir_arg=true
11917a48c7cSmrg        shift
12017a48c7cSmrg        continue;;
12117a48c7cSmrg
12217a48c7cSmrg    -g) chgrpcmd="$chgrpprog $2"
12317a48c7cSmrg        shift
12417a48c7cSmrg        shift
12517a48c7cSmrg        continue;;
12617a48c7cSmrg
12717a48c7cSmrg    --help) echo "$usage"; exit $?;;
12817a48c7cSmrg
1298e0ed500Smrg    -m) mode=$2
13017a48c7cSmrg        shift
13117a48c7cSmrg        shift
1328e0ed500Smrg	case $mode in
1338e0ed500Smrg	  *' '* | *'	'* | *'
1348e0ed500Smrg'*	  | *'*'* | *'?'* | *'['*)
1358e0ed500Smrg	    echo "$0: invalid mode: $mode" >&2
1368e0ed500Smrg	    exit 1;;
1378e0ed500Smrg	esac
13817a48c7cSmrg        continue;;
13917a48c7cSmrg
14017a48c7cSmrg    -o) chowncmd="$chownprog $2"
14117a48c7cSmrg        shift
14217a48c7cSmrg        shift
14317a48c7cSmrg        continue;;
14417a48c7cSmrg
14517a48c7cSmrg    -s) stripcmd=$stripprog
14617a48c7cSmrg        shift
14717a48c7cSmrg        continue;;
14817a48c7cSmrg
14917a48c7cSmrg    -t) dstarg=$2
15017a48c7cSmrg	shift
15117a48c7cSmrg	shift
15217a48c7cSmrg	continue;;
15317a48c7cSmrg
15417a48c7cSmrg    -T) no_target_directory=true
15517a48c7cSmrg	shift
15617a48c7cSmrg	continue;;
15717a48c7cSmrg
15817a48c7cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
15917a48c7cSmrg
1608e0ed500Smrg    --)	shift
16117a48c7cSmrg	break;;
1628e0ed500Smrg
1638e0ed500Smrg    -*)	echo "$0: invalid option: $1" >&2
1648e0ed500Smrg	exit 1;;
1658e0ed500Smrg
1668e0ed500Smrg    *)  break;;
16717a48c7cSmrg  esac
16817a48c7cSmrgdone
16917a48c7cSmrg
1708e0ed500Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
1718e0ed500Smrg  # When -d is used, all remaining arguments are directories to create.
1728e0ed500Smrg  # When -t is used, the destination is already specified.
1738e0ed500Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1748e0ed500Smrg  for arg
1758e0ed500Smrg  do
1768e0ed500Smrg    if test -n "$dstarg"; then
1778e0ed500Smrg      # $@ is not empty: it contains at least $arg.
1788e0ed500Smrg      set fnord "$@" "$dstarg"
1798e0ed500Smrg      shift # fnord
1808e0ed500Smrg    fi
1818e0ed500Smrg    shift # arg
1828e0ed500Smrg    dstarg=$arg
1838e0ed500Smrg  done
1848e0ed500Smrgfi
1858e0ed500Smrg
1868e0ed500Smrgif test $# -eq 0; then
18717a48c7cSmrg  if test -z "$dir_arg"; then
18817a48c7cSmrg    echo "$0: no input file specified." >&2
18917a48c7cSmrg    exit 1
19017a48c7cSmrg  fi
19117a48c7cSmrg  # It's OK to call `install-sh -d' without argument.
19217a48c7cSmrg  # This can happen when creating conditional directories.
19317a48c7cSmrg  exit 0
19417a48c7cSmrgfi
19517a48c7cSmrg
1968e0ed500Smrgif test -z "$dir_arg"; then
1978e0ed500Smrg  trap '(exit $?); exit' 1 2 13 15
1988e0ed500Smrg
1998e0ed500Smrg  # Set umask so as not to create temps with too-generous modes.
2008e0ed500Smrg  # However, 'strip' requires both read and write access to temps.
2018e0ed500Smrg  case $mode in
2028e0ed500Smrg    # Optimize common cases.
2038e0ed500Smrg    *644) cp_umask=133;;
2048e0ed500Smrg    *755) cp_umask=22;;
2058e0ed500Smrg
2068e0ed500Smrg    *[0-7])
2078e0ed500Smrg      if test -z "$stripcmd"; then
2088e0ed500Smrg	u_plus_rw=
2098e0ed500Smrg      else
2108e0ed500Smrg	u_plus_rw='% 200'
2118e0ed500Smrg      fi
2128e0ed500Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2138e0ed500Smrg    *)
2148e0ed500Smrg      if test -z "$stripcmd"; then
2158e0ed500Smrg	u_plus_rw=
2168e0ed500Smrg      else
2178e0ed500Smrg	u_plus_rw=,u+rw
2188e0ed500Smrg      fi
2198e0ed500Smrg      cp_umask=$mode$u_plus_rw;;
2208e0ed500Smrg  esac
2218e0ed500Smrgfi
2228e0ed500Smrg
22317a48c7cSmrgfor src
22417a48c7cSmrgdo
22517a48c7cSmrg  # Protect names starting with `-'.
22617a48c7cSmrg  case $src in
22717a48c7cSmrg    -*) src=./$src ;;
22817a48c7cSmrg  esac
22917a48c7cSmrg
23017a48c7cSmrg  if test -n "$dir_arg"; then
23117a48c7cSmrg    dst=$src
2328e0ed500Smrg    dstdir=$dst
2338e0ed500Smrg    test -d "$dstdir"
2348e0ed500Smrg    dstdir_status=$?
23517a48c7cSmrg  else
2368e0ed500Smrg
23717a48c7cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
23817a48c7cSmrg    # might cause directories to be created, which would be especially bad
23917a48c7cSmrg    # if $src (and thus $dsttmp) contains '*'.
24017a48c7cSmrg    if test ! -f "$src" && test ! -d "$src"; then
24117a48c7cSmrg      echo "$0: $src does not exist." >&2
24217a48c7cSmrg      exit 1
24317a48c7cSmrg    fi
24417a48c7cSmrg
24517a48c7cSmrg    if test -z "$dstarg"; then
24617a48c7cSmrg      echo "$0: no destination specified." >&2
24717a48c7cSmrg      exit 1
24817a48c7cSmrg    fi
24917a48c7cSmrg
25017a48c7cSmrg    dst=$dstarg
25117a48c7cSmrg    # Protect names starting with `-'.
25217a48c7cSmrg    case $dst in
25317a48c7cSmrg      -*) dst=./$dst ;;
25417a48c7cSmrg    esac
25517a48c7cSmrg
25617a48c7cSmrg    # If destination is a directory, append the input filename; won't work
25717a48c7cSmrg    # if double slashes aren't ignored.
25817a48c7cSmrg    if test -d "$dst"; then
25917a48c7cSmrg      if test -n "$no_target_directory"; then
26017a48c7cSmrg	echo "$0: $dstarg: Is a directory" >&2
26117a48c7cSmrg	exit 1
26217a48c7cSmrg      fi
2638e0ed500Smrg      dstdir=$dst
2648e0ed500Smrg      dst=$dstdir/`basename "$src"`
2658e0ed500Smrg      dstdir_status=0
2668e0ed500Smrg    else
2678e0ed500Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2688e0ed500Smrg      dstdir=`
2698e0ed500Smrg	(dirname "$dst") 2>/dev/null ||
2708e0ed500Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2718e0ed500Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2728e0ed500Smrg	     X"$dst" : 'X\(//\)$' \| \
2738e0ed500Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2748e0ed500Smrg	echo X"$dst" |
2758e0ed500Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2768e0ed500Smrg		   s//\1/
2778e0ed500Smrg		   q
2788e0ed500Smrg		 }
2798e0ed500Smrg		 /^X\(\/\/\)[^/].*/{
2808e0ed500Smrg		   s//\1/
2818e0ed500Smrg		   q
2828e0ed500Smrg		 }
2838e0ed500Smrg		 /^X\(\/\/\)$/{
2848e0ed500Smrg		   s//\1/
2858e0ed500Smrg		   q
2868e0ed500Smrg		 }
2878e0ed500Smrg		 /^X\(\/\).*/{
2888e0ed500Smrg		   s//\1/
2898e0ed500Smrg		   q
2908e0ed500Smrg		 }
2918e0ed500Smrg		 s/.*/./; q'
2928e0ed500Smrg      `
2938e0ed500Smrg
2948e0ed500Smrg      test -d "$dstdir"
2958e0ed500Smrg      dstdir_status=$?
29617a48c7cSmrg    fi
29717a48c7cSmrg  fi
29817a48c7cSmrg
2998e0ed500Smrg  obsolete_mkdir_used=false
3008e0ed500Smrg
3018e0ed500Smrg  if test $dstdir_status != 0; then
3028e0ed500Smrg    case $posix_mkdir in
3038e0ed500Smrg      '')
3048e0ed500Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3058e0ed500Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3068e0ed500Smrg	umask=`umask`
3078e0ed500Smrg	case $stripcmd.$umask in
3088e0ed500Smrg	  # Optimize common cases.
3098e0ed500Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3108e0ed500Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3118e0ed500Smrg
3128e0ed500Smrg	  *[0-7])
3138e0ed500Smrg	    mkdir_umask=`expr $umask + 22 \
3148e0ed500Smrg	      - $umask % 100 % 40 + $umask % 20 \
3158e0ed500Smrg	      - $umask % 10 % 4 + $umask % 2
3168e0ed500Smrg	    `;;
3178e0ed500Smrg	  *) mkdir_umask=$umask,go-w;;
3188e0ed500Smrg	esac
3198e0ed500Smrg
3208e0ed500Smrg	# With -d, create the new directory with the user-specified mode.
3218e0ed500Smrg	# Otherwise, rely on $mkdir_umask.
3228e0ed500Smrg	if test -n "$dir_arg"; then
3238e0ed500Smrg	  mkdir_mode=-m$mode
3248e0ed500Smrg	else
3258e0ed500Smrg	  mkdir_mode=
3268e0ed500Smrg	fi
3278e0ed500Smrg
3288e0ed500Smrg	posix_mkdir=false
3298e0ed500Smrg	case $umask in
3308e0ed500Smrg	  *[123567][0-7][0-7])
3318e0ed500Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3328e0ed500Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3338e0ed500Smrg	    ;;
3348e0ed500Smrg	  *)
3358e0ed500Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3368e0ed500Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3378e0ed500Smrg
3388e0ed500Smrg	    if (umask $mkdir_umask &&
3398e0ed500Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3408e0ed500Smrg	    then
3418e0ed500Smrg	      if test -z "$dir_arg" || {
3428e0ed500Smrg		   # Check for POSIX incompatibilities with -m.
3438e0ed500Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3448e0ed500Smrg		   # other-writeable bit of parent directory when it shouldn't.
3458e0ed500Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3468e0ed500Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3478e0ed500Smrg		   case $ls_ld_tmpdir in
3488e0ed500Smrg		     d????-?r-*) different_mode=700;;
3498e0ed500Smrg		     d????-?--*) different_mode=755;;
3508e0ed500Smrg		     *) false;;
3518e0ed500Smrg		   esac &&
3528e0ed500Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3538e0ed500Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3548e0ed500Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3558e0ed500Smrg		   }
3568e0ed500Smrg		 }
3578e0ed500Smrg	      then posix_mkdir=:
3588e0ed500Smrg	      fi
3598e0ed500Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3608e0ed500Smrg	    else
3618e0ed500Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3628e0ed500Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3638e0ed500Smrg	    fi
3648e0ed500Smrg	    trap '' 0;;
3658e0ed500Smrg	esac;;
3668e0ed500Smrg    esac
36717a48c7cSmrg
3688e0ed500Smrg    if
3698e0ed500Smrg      $posix_mkdir && (
3708e0ed500Smrg	umask $mkdir_umask &&
3718e0ed500Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3728e0ed500Smrg      )
3738e0ed500Smrg    then :
3748e0ed500Smrg    else
37517a48c7cSmrg
3768e0ed500Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3778e0ed500Smrg      # or it failed possibly due to a race condition.  Create the
3788e0ed500Smrg      # directory the slow way, step by step, checking for races as we go.
3798e0ed500Smrg
3808e0ed500Smrg      case $dstdir in
3818e0ed500Smrg	/*) prefix=/ ;;
3828e0ed500Smrg	-*) prefix=./ ;;
3838e0ed500Smrg	*)  prefix= ;;
3848e0ed500Smrg      esac
3858e0ed500Smrg
3868e0ed500Smrg      case $posix_glob in
3878e0ed500Smrg        '')
3888e0ed500Smrg	  if (set -f) 2>/dev/null; then
3898e0ed500Smrg	    posix_glob=true
3908e0ed500Smrg	  else
3918e0ed500Smrg	    posix_glob=false
3928e0ed500Smrg	  fi ;;
3938e0ed500Smrg      esac
3948e0ed500Smrg
3958e0ed500Smrg      oIFS=$IFS
3968e0ed500Smrg      IFS=/
3978e0ed500Smrg      $posix_glob && set -f
3988e0ed500Smrg      set fnord $dstdir
39917a48c7cSmrg      shift
4008e0ed500Smrg      $posix_glob && set +f
4018e0ed500Smrg      IFS=$oIFS
4028e0ed500Smrg
4038e0ed500Smrg      prefixes=
4048e0ed500Smrg
4058e0ed500Smrg      for d
4068e0ed500Smrg      do
4078e0ed500Smrg	test -z "$d" && continue
4088e0ed500Smrg
4098e0ed500Smrg	prefix=$prefix$d
4108e0ed500Smrg	if test -d "$prefix"; then
4118e0ed500Smrg	  prefixes=
4128e0ed500Smrg	else
4138e0ed500Smrg	  if $posix_mkdir; then
4148e0ed500Smrg	    (umask=$mkdir_umask &&
4158e0ed500Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4168e0ed500Smrg	    # Don't fail if two instances are running concurrently.
4178e0ed500Smrg	    test -d "$prefix" || exit 1
4188e0ed500Smrg	  else
4198e0ed500Smrg	    case $prefix in
4208e0ed500Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4218e0ed500Smrg	      *) qprefix=$prefix;;
4228e0ed500Smrg	    esac
4238e0ed500Smrg	    prefixes="$prefixes '$qprefix'"
4248e0ed500Smrg	  fi
4258e0ed500Smrg	fi
4268e0ed500Smrg	prefix=$prefix/
4278e0ed500Smrg      done
4288e0ed500Smrg
4298e0ed500Smrg      if test -n "$prefixes"; then
4308e0ed500Smrg	# Don't fail if two instances are running concurrently.
4318e0ed500Smrg	(umask $mkdir_umask &&
4328e0ed500Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4338e0ed500Smrg	  test -d "$dstdir" || exit 1
4348e0ed500Smrg	obsolete_mkdir_used=true
43517a48c7cSmrg      fi
4368e0ed500Smrg    fi
43717a48c7cSmrg  fi
43817a48c7cSmrg
43917a48c7cSmrg  if test -n "$dir_arg"; then
4408e0ed500Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4418e0ed500Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4428e0ed500Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4438e0ed500Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44417a48c7cSmrg  else
44517a48c7cSmrg
44617a48c7cSmrg    # Make a couple of temp file names in the proper directory.
44717a48c7cSmrg    dsttmp=$dstdir/_inst.$$_
44817a48c7cSmrg    rmtmp=$dstdir/_rm.$$_
44917a48c7cSmrg
45017a48c7cSmrg    # Trap to clean up those temp files at exit.
45117a48c7cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45217a48c7cSmrg
45317a48c7cSmrg    # Copy the file name to the temp name.
4548e0ed500Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45517a48c7cSmrg
45617a48c7cSmrg    # and set any options; do chmod last to preserve setuid bits.
45717a48c7cSmrg    #
45817a48c7cSmrg    # If any of these fail, we abort the whole thing.  If we want to
45917a48c7cSmrg    # ignore errors from any of these, just make sure not to ignore
46017a48c7cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46117a48c7cSmrg    #
46217a48c7cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
46317a48c7cSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
46417a48c7cSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
4658e0ed500Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46617a48c7cSmrg
46717a48c7cSmrg    # Now rename the file to the real destination.
4688e0ed500Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
46917a48c7cSmrg      || {
47017a48c7cSmrg	   # The rename failed, perhaps because mv can't rename something else
47117a48c7cSmrg	   # to itself, or perhaps because mv is so ancient that it does not
47217a48c7cSmrg	   # support -f.
47317a48c7cSmrg
47417a48c7cSmrg	   # Now remove or move aside any old file at destination location.
47517a48c7cSmrg	   # We try this two ways since rm can't unlink itself on some
47617a48c7cSmrg	   # systems and the destination file might be busy for other
47717a48c7cSmrg	   # reasons.  In this case, the final cleanup might fail but the new
47817a48c7cSmrg	   # file should still install successfully.
47917a48c7cSmrg	   {
4808e0ed500Smrg	     if test -f "$dst"; then
4818e0ed500Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
4828e0ed500Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
4838e0ed500Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
48417a48c7cSmrg	       || {
4858e0ed500Smrg		 echo "$0: cannot unlink or rename $dst" >&2
48617a48c7cSmrg		 (exit 1); exit 1
48717a48c7cSmrg	       }
48817a48c7cSmrg	     else
48917a48c7cSmrg	       :
49017a48c7cSmrg	     fi
49117a48c7cSmrg	   } &&
49217a48c7cSmrg
49317a48c7cSmrg	   # Now rename the file to the real destination.
4948e0ed500Smrg	   $doit $mvcmd "$dsttmp" "$dst"
49517a48c7cSmrg	 }
4968e0ed500Smrg    } || exit 1
49717a48c7cSmrg
4988e0ed500Smrg    trap '' 0
4998e0ed500Smrg  fi
5008e0ed500Smrgdone
50117a48c7cSmrg
50217a48c7cSmrg# Local variables:
50317a48c7cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50417a48c7cSmrg# time-stamp-start: "scriptversion="
50517a48c7cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
50617a48c7cSmrg# time-stamp-end: "$"
50717a48c7cSmrg# End:
508