install-sh revision 2ceb2653
1b7d53ee5Smrg#!/bin/sh
2b7d53ee5Smrg# install - install a program, script, or datafile
32ceb2653Smrg
42ceb2653Smrgscriptversion=2009-04-28.21; # UTC
52ceb2653Smrg
6b7d53ee5Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7b7d53ee5Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8b7d53ee5Smrg# following copyright and license.
9b7d53ee5Smrg#
10b7d53ee5Smrg# Copyright (C) 1994 X Consortium
11b7d53ee5Smrg#
12b7d53ee5Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13b7d53ee5Smrg# of this software and associated documentation files (the "Software"), to
14b7d53ee5Smrg# deal in the Software without restriction, including without limitation the
15b7d53ee5Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16b7d53ee5Smrg# sell copies of the Software, and to permit persons to whom the Software is
17b7d53ee5Smrg# furnished to do so, subject to the following conditions:
18b7d53ee5Smrg#
19b7d53ee5Smrg# The above copyright notice and this permission notice shall be included in
20b7d53ee5Smrg# all copies or substantial portions of the Software.
21b7d53ee5Smrg#
22b7d53ee5Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23b7d53ee5Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24b7d53ee5Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25b7d53ee5Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26b7d53ee5Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27b7d53ee5Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28b7d53ee5Smrg#
29b7d53ee5Smrg# Except as contained in this notice, the name of the X Consortium shall not
30b7d53ee5Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31b7d53ee5Smrg# ings in this Software without prior written authorization from the X Consor-
32b7d53ee5Smrg# tium.
33b7d53ee5Smrg#
34b7d53ee5Smrg#
35b7d53ee5Smrg# FSF changes to this file are in the public domain.
36b7d53ee5Smrg#
37b7d53ee5Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38b7d53ee5Smrg# `make' implicit rules from creating a file called install from it
39b7d53ee5Smrg# when there is no Makefile.
40b7d53ee5Smrg#
41b7d53ee5Smrg# This script is compatible with the BSD install script, but was written
422ceb2653Smrg# from scratch.
43b7d53ee5Smrg
442ceb2653Smrgnl='
452ceb2653Smrg'
462ceb2653SmrgIFS=" ""	$nl"
47b7d53ee5Smrg
48b7d53ee5Smrg# set DOITPROG to echo to test this script
49b7d53ee5Smrg
50b7d53ee5Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
512ceb2653Smrgdoit=${DOITPROG-}
522ceb2653Smrgif test -z "$doit"; then
532ceb2653Smrg  doit_exec=exec
54b7d53ee5Smrgelse
552ceb2653Smrg  doit_exec=$doit
56b7d53ee5Smrgfi
57b7d53ee5Smrg
582ceb2653Smrg# Put in absolute file names if you don't have them in your path;
592ceb2653Smrg# or use environment vars.
602ceb2653Smrg
612ceb2653Smrgchgrpprog=${CHGRPPROG-chgrp}
622ceb2653Smrgchmodprog=${CHMODPROG-chmod}
632ceb2653Smrgchownprog=${CHOWNPROG-chown}
642ceb2653Smrgcmpprog=${CMPPROG-cmp}
652ceb2653Smrgcpprog=${CPPROG-cp}
662ceb2653Smrgmkdirprog=${MKDIRPROG-mkdir}
672ceb2653Smrgmvprog=${MVPROG-mv}
682ceb2653Smrgrmprog=${RMPROG-rm}
692ceb2653Smrgstripprog=${STRIPPROG-strip}
702ceb2653Smrg
712ceb2653Smrgposix_glob='?'
722ceb2653Smrginitialize_posix_glob='
732ceb2653Smrg  test "$posix_glob" != "?" || {
742ceb2653Smrg    if (set -f) 2>/dev/null; then
752ceb2653Smrg      posix_glob=
762ceb2653Smrg    else
772ceb2653Smrg      posix_glob=:
782ceb2653Smrg    fi
792ceb2653Smrg  }
802ceb2653Smrg'
812ceb2653Smrg
822ceb2653Smrgposix_mkdir=
832ceb2653Smrg
842ceb2653Smrg# Desired mode of installed file.
852ceb2653Smrgmode=0755
862ceb2653Smrg
872ceb2653Smrgchgrpcmd=
882ceb2653Smrgchmodcmd=$chmodprog
892ceb2653Smrgchowncmd=
902ceb2653Smrgmvcmd=$mvprog
912ceb2653Smrgrmcmd="$rmprog -f"
922ceb2653Smrgstripcmd=
93b7d53ee5Smrg
942ceb2653Smrgsrc=
952ceb2653Smrgdst=
962ceb2653Smrgdir_arg=
972ceb2653Smrgdst_arg=
98b7d53ee5Smrg
992ceb2653Smrgcopy_on_change=false
1002ceb2653Smrgno_target_directory=
101b7d53ee5Smrg
1022ceb2653Smrgusage="\
1032ceb2653SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1042ceb2653Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1052ceb2653Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1062ceb2653Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107b7d53ee5Smrg
1082ceb2653SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1092ceb2653SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1102ceb2653SmrgIn the 4th, create DIRECTORIES.
111b7d53ee5Smrg
1122ceb2653SmrgOptions:
1132ceb2653Smrg     --help     display this help and exit.
1142ceb2653Smrg     --version  display version info and exit.
115b7d53ee5Smrg
1162ceb2653Smrg  -c            (ignored)
1172ceb2653Smrg  -C            install only if different (preserve the last data modification time)
1182ceb2653Smrg  -d            create directories instead of installing files.
1192ceb2653Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1202ceb2653Smrg  -m MODE       $chmodprog installed files to MODE.
1212ceb2653Smrg  -o USER       $chownprog installed files to USER.
1222ceb2653Smrg  -s            $stripprog installed files.
1232ceb2653Smrg  -t DIRECTORY  install into DIRECTORY.
1242ceb2653Smrg  -T            report an error if DSTFILE is a directory.
125b7d53ee5Smrg
1262ceb2653SmrgEnvironment variables override the default commands:
1272ceb2653Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282ceb2653Smrg  RMPROG STRIPPROG
1292ceb2653Smrg"
130b7d53ee5Smrg
1312ceb2653Smrgwhile test $# -ne 0; do
1322ceb2653Smrg  case $1 in
1332ceb2653Smrg    -c) ;;
134b7d53ee5Smrg
1352ceb2653Smrg    -C) copy_on_change=true;;
136b7d53ee5Smrg
1372ceb2653Smrg    -d) dir_arg=true;;
138b7d53ee5Smrg
1392ceb2653Smrg    -g) chgrpcmd="$chgrpprog $2"
1402ceb2653Smrg	shift;;
141b7d53ee5Smrg
1422ceb2653Smrg    --help) echo "$usage"; exit $?;;
143b7d53ee5Smrg
1442ceb2653Smrg    -m) mode=$2
1452ceb2653Smrg	case $mode in
1462ceb2653Smrg	  *' '* | *'	'* | *'
1472ceb2653Smrg'*	  | *'*'* | *'?'* | *'['*)
1482ceb2653Smrg	    echo "$0: invalid mode: $mode" >&2
1492ceb2653Smrg	    exit 1;;
1502ceb2653Smrg	esac
1512ceb2653Smrg	shift;;
152b7d53ee5Smrg
1532ceb2653Smrg    -o) chowncmd="$chownprog $2"
1542ceb2653Smrg	shift;;
155b7d53ee5Smrg
1562ceb2653Smrg    -s) stripcmd=$stripprog;;
157b7d53ee5Smrg
1582ceb2653Smrg    -t) dst_arg=$2
1592ceb2653Smrg	shift;;
160b7d53ee5Smrg
1612ceb2653Smrg    -T) no_target_directory=true;;
162b7d53ee5Smrg
1632ceb2653Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164b7d53ee5Smrg
1652ceb2653Smrg    --)	shift
1662ceb2653Smrg	break;;
167b7d53ee5Smrg
1682ceb2653Smrg    -*)	echo "$0: invalid option: $1" >&2
1692ceb2653Smrg	exit 1;;
170b7d53ee5Smrg
1712ceb2653Smrg    *)  break;;
1722ceb2653Smrg  esac
1732ceb2653Smrg  shift
1742ceb2653Smrgdone
175b7d53ee5Smrg
1762ceb2653Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772ceb2653Smrg  # When -d is used, all remaining arguments are directories to create.
1782ceb2653Smrg  # When -t is used, the destination is already specified.
1792ceb2653Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802ceb2653Smrg  for arg
1812ceb2653Smrg  do
1822ceb2653Smrg    if test -n "$dst_arg"; then
1832ceb2653Smrg      # $@ is not empty: it contains at least $arg.
1842ceb2653Smrg      set fnord "$@" "$dst_arg"
1852ceb2653Smrg      shift # fnord
1862ceb2653Smrg    fi
1872ceb2653Smrg    shift # arg
1882ceb2653Smrg    dst_arg=$arg
1892ceb2653Smrg  done
1902ceb2653Smrgfi
191b7d53ee5Smrg
1922ceb2653Smrgif test $# -eq 0; then
1932ceb2653Smrg  if test -z "$dir_arg"; then
1942ceb2653Smrg    echo "$0: no input file specified." >&2
1952ceb2653Smrg    exit 1
1962ceb2653Smrg  fi
1972ceb2653Smrg  # It's OK to call `install-sh -d' without argument.
1982ceb2653Smrg  # This can happen when creating conditional directories.
1992ceb2653Smrg  exit 0
2002ceb2653Smrgfi
201b7d53ee5Smrg
2022ceb2653Smrgif test -z "$dir_arg"; then
2032ceb2653Smrg  trap '(exit $?); exit' 1 2 13 15
2042ceb2653Smrg
2052ceb2653Smrg  # Set umask so as not to create temps with too-generous modes.
2062ceb2653Smrg  # However, 'strip' requires both read and write access to temps.
2072ceb2653Smrg  case $mode in
2082ceb2653Smrg    # Optimize common cases.
2092ceb2653Smrg    *644) cp_umask=133;;
2102ceb2653Smrg    *755) cp_umask=22;;
2112ceb2653Smrg
2122ceb2653Smrg    *[0-7])
2132ceb2653Smrg      if test -z "$stripcmd"; then
2142ceb2653Smrg	u_plus_rw=
2152ceb2653Smrg      else
2162ceb2653Smrg	u_plus_rw='% 200'
2172ceb2653Smrg      fi
2182ceb2653Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2192ceb2653Smrg    *)
2202ceb2653Smrg      if test -z "$stripcmd"; then
2212ceb2653Smrg	u_plus_rw=
2222ceb2653Smrg      else
2232ceb2653Smrg	u_plus_rw=,u+rw
2242ceb2653Smrg      fi
2252ceb2653Smrg      cp_umask=$mode$u_plus_rw;;
2262ceb2653Smrg  esac
2272ceb2653Smrgfi
228b7d53ee5Smrg
2292ceb2653Smrgfor src
2302ceb2653Smrgdo
2312ceb2653Smrg  # Protect names starting with `-'.
2322ceb2653Smrg  case $src in
2332ceb2653Smrg    -*) src=./$src;;
2342ceb2653Smrg  esac
2352ceb2653Smrg
2362ceb2653Smrg  if test -n "$dir_arg"; then
2372ceb2653Smrg    dst=$src
2382ceb2653Smrg    dstdir=$dst
2392ceb2653Smrg    test -d "$dstdir"
2402ceb2653Smrg    dstdir_status=$?
2412ceb2653Smrg  else
2422ceb2653Smrg
2432ceb2653Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2442ceb2653Smrg    # might cause directories to be created, which would be especially bad
2452ceb2653Smrg    # if $src (and thus $dsttmp) contains '*'.
2462ceb2653Smrg    if test ! -f "$src" && test ! -d "$src"; then
2472ceb2653Smrg      echo "$0: $src does not exist." >&2
2482ceb2653Smrg      exit 1
2492ceb2653Smrg    fi
2502ceb2653Smrg
2512ceb2653Smrg    if test -z "$dst_arg"; then
2522ceb2653Smrg      echo "$0: no destination specified." >&2
2532ceb2653Smrg      exit 1
2542ceb2653Smrg    fi
2552ceb2653Smrg
2562ceb2653Smrg    dst=$dst_arg
2572ceb2653Smrg    # Protect names starting with `-'.
2582ceb2653Smrg    case $dst in
2592ceb2653Smrg      -*) dst=./$dst;;
2602ceb2653Smrg    esac
261b7d53ee5Smrg
2622ceb2653Smrg    # If destination is a directory, append the input filename; won't work
2632ceb2653Smrg    # if double slashes aren't ignored.
2642ceb2653Smrg    if test -d "$dst"; then
2652ceb2653Smrg      if test -n "$no_target_directory"; then
2662ceb2653Smrg	echo "$0: $dst_arg: Is a directory" >&2
2672ceb2653Smrg	exit 1
2682ceb2653Smrg      fi
2692ceb2653Smrg      dstdir=$dst
2702ceb2653Smrg      dst=$dstdir/`basename "$src"`
2712ceb2653Smrg      dstdir_status=0
2722ceb2653Smrg    else
2732ceb2653Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2742ceb2653Smrg      dstdir=`
2752ceb2653Smrg	(dirname "$dst") 2>/dev/null ||
2762ceb2653Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2772ceb2653Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2782ceb2653Smrg	     X"$dst" : 'X\(//\)$' \| \
2792ceb2653Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2802ceb2653Smrg	echo X"$dst" |
2812ceb2653Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2822ceb2653Smrg		   s//\1/
2832ceb2653Smrg		   q
2842ceb2653Smrg		 }
2852ceb2653Smrg		 /^X\(\/\/\)[^/].*/{
2862ceb2653Smrg		   s//\1/
2872ceb2653Smrg		   q
2882ceb2653Smrg		 }
2892ceb2653Smrg		 /^X\(\/\/\)$/{
2902ceb2653Smrg		   s//\1/
2912ceb2653Smrg		   q
2922ceb2653Smrg		 }
2932ceb2653Smrg		 /^X\(\/\).*/{
2942ceb2653Smrg		   s//\1/
2952ceb2653Smrg		   q
2962ceb2653Smrg		 }
2972ceb2653Smrg		 s/.*/./; q'
2982ceb2653Smrg      `
2992ceb2653Smrg
3002ceb2653Smrg      test -d "$dstdir"
3012ceb2653Smrg      dstdir_status=$?
3022ceb2653Smrg    fi
3032ceb2653Smrg  fi
3042ceb2653Smrg
3052ceb2653Smrg  obsolete_mkdir_used=false
3062ceb2653Smrg
3072ceb2653Smrg  if test $dstdir_status != 0; then
3082ceb2653Smrg    case $posix_mkdir in
3092ceb2653Smrg      '')
3102ceb2653Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3112ceb2653Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3122ceb2653Smrg	umask=`umask`
3132ceb2653Smrg	case $stripcmd.$umask in
3142ceb2653Smrg	  # Optimize common cases.
3152ceb2653Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3162ceb2653Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3172ceb2653Smrg
3182ceb2653Smrg	  *[0-7])
3192ceb2653Smrg	    mkdir_umask=`expr $umask + 22 \
3202ceb2653Smrg	      - $umask % 100 % 40 + $umask % 20 \
3212ceb2653Smrg	      - $umask % 10 % 4 + $umask % 2
3222ceb2653Smrg	    `;;
3232ceb2653Smrg	  *) mkdir_umask=$umask,go-w;;
3242ceb2653Smrg	esac
3252ceb2653Smrg
3262ceb2653Smrg	# With -d, create the new directory with the user-specified mode.
3272ceb2653Smrg	# Otherwise, rely on $mkdir_umask.
3282ceb2653Smrg	if test -n "$dir_arg"; then
3292ceb2653Smrg	  mkdir_mode=-m$mode
330b7d53ee5Smrg	else
3312ceb2653Smrg	  mkdir_mode=
332b7d53ee5Smrg	fi
333b7d53ee5Smrg
3342ceb2653Smrg	posix_mkdir=false
3352ceb2653Smrg	case $umask in
3362ceb2653Smrg	  *[123567][0-7][0-7])
3372ceb2653Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3382ceb2653Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3392ceb2653Smrg	    ;;
3402ceb2653Smrg	  *)
3412ceb2653Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3422ceb2653Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3432ceb2653Smrg
3442ceb2653Smrg	    if (umask $mkdir_umask &&
3452ceb2653Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3462ceb2653Smrg	    then
3472ceb2653Smrg	      if test -z "$dir_arg" || {
3482ceb2653Smrg		   # Check for POSIX incompatibilities with -m.
3492ceb2653Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3502ceb2653Smrg		   # other-writeable bit of parent directory when it shouldn't.
3512ceb2653Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3522ceb2653Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3532ceb2653Smrg		   case $ls_ld_tmpdir in
3542ceb2653Smrg		     d????-?r-*) different_mode=700;;
3552ceb2653Smrg		     d????-?--*) different_mode=755;;
3562ceb2653Smrg		     *) false;;
3572ceb2653Smrg		   esac &&
3582ceb2653Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3592ceb2653Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3602ceb2653Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3612ceb2653Smrg		   }
3622ceb2653Smrg		 }
3632ceb2653Smrg	      then posix_mkdir=:
3642ceb2653Smrg	      fi
3652ceb2653Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3662ceb2653Smrg	    else
3672ceb2653Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3682ceb2653Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3692ceb2653Smrg	    fi
3702ceb2653Smrg	    trap '' 0;;
3712ceb2653Smrg	esac;;
3722ceb2653Smrg    esac
373b7d53ee5Smrg
3742ceb2653Smrg    if
3752ceb2653Smrg      $posix_mkdir && (
3762ceb2653Smrg	umask $mkdir_umask &&
3772ceb2653Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3782ceb2653Smrg      )
3792ceb2653Smrg    then :
3802ceb2653Smrg    else
3812ceb2653Smrg
3822ceb2653Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3832ceb2653Smrg      # or it failed possibly due to a race condition.  Create the
3842ceb2653Smrg      # directory the slow way, step by step, checking for races as we go.
3852ceb2653Smrg
3862ceb2653Smrg      case $dstdir in
3872ceb2653Smrg	/*) prefix='/';;
3882ceb2653Smrg	-*) prefix='./';;
3892ceb2653Smrg	*)  prefix='';;
3902ceb2653Smrg      esac
3912ceb2653Smrg
3922ceb2653Smrg      eval "$initialize_posix_glob"
3932ceb2653Smrg
3942ceb2653Smrg      oIFS=$IFS
3952ceb2653Smrg      IFS=/
3962ceb2653Smrg      $posix_glob set -f
3972ceb2653Smrg      set fnord $dstdir
3982ceb2653Smrg      shift
3992ceb2653Smrg      $posix_glob set +f
4002ceb2653Smrg      IFS=$oIFS
4012ceb2653Smrg
4022ceb2653Smrg      prefixes=
4032ceb2653Smrg
4042ceb2653Smrg      for d
4052ceb2653Smrg      do
4062ceb2653Smrg	test -z "$d" && continue
4072ceb2653Smrg
4082ceb2653Smrg	prefix=$prefix$d
4092ceb2653Smrg	if test -d "$prefix"; then
4102ceb2653Smrg	  prefixes=
4112ceb2653Smrg	else
4122ceb2653Smrg	  if $posix_mkdir; then
4132ceb2653Smrg	    (umask=$mkdir_umask &&
4142ceb2653Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4152ceb2653Smrg	    # Don't fail if two instances are running concurrently.
4162ceb2653Smrg	    test -d "$prefix" || exit 1
4172ceb2653Smrg	  else
4182ceb2653Smrg	    case $prefix in
4192ceb2653Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4202ceb2653Smrg	      *) qprefix=$prefix;;
4212ceb2653Smrg	    esac
4222ceb2653Smrg	    prefixes="$prefixes '$qprefix'"
4232ceb2653Smrg	  fi
4242ceb2653Smrg	fi
4252ceb2653Smrg	prefix=$prefix/
4262ceb2653Smrg      done
4272ceb2653Smrg
4282ceb2653Smrg      if test -n "$prefixes"; then
4292ceb2653Smrg	# Don't fail if two instances are running concurrently.
4302ceb2653Smrg	(umask $mkdir_umask &&
4312ceb2653Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4322ceb2653Smrg	  test -d "$dstdir" || exit 1
4332ceb2653Smrg	obsolete_mkdir_used=true
4342ceb2653Smrg      fi
4352ceb2653Smrg    fi
4362ceb2653Smrg  fi
4372ceb2653Smrg
4382ceb2653Smrg  if test -n "$dir_arg"; then
4392ceb2653Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4402ceb2653Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4412ceb2653Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4422ceb2653Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4432ceb2653Smrg  else
4442ceb2653Smrg
4452ceb2653Smrg    # Make a couple of temp file names in the proper directory.
4462ceb2653Smrg    dsttmp=$dstdir/_inst.$$_
4472ceb2653Smrg    rmtmp=$dstdir/_rm.$$_
4482ceb2653Smrg
4492ceb2653Smrg    # Trap to clean up those temp files at exit.
4502ceb2653Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4512ceb2653Smrg
4522ceb2653Smrg    # Copy the file name to the temp name.
4532ceb2653Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4542ceb2653Smrg
4552ceb2653Smrg    # and set any options; do chmod last to preserve setuid bits.
4562ceb2653Smrg    #
4572ceb2653Smrg    # If any of these fail, we abort the whole thing.  If we want to
4582ceb2653Smrg    # ignore errors from any of these, just make sure not to ignore
4592ceb2653Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4602ceb2653Smrg    #
4612ceb2653Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4622ceb2653Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4632ceb2653Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4642ceb2653Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4652ceb2653Smrg
4662ceb2653Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4672ceb2653Smrg    if $copy_on_change &&
4682ceb2653Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4692ceb2653Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4702ceb2653Smrg
4712ceb2653Smrg       eval "$initialize_posix_glob" &&
4722ceb2653Smrg       $posix_glob set -f &&
4732ceb2653Smrg       set X $old && old=:$2:$4:$5:$6 &&
4742ceb2653Smrg       set X $new && new=:$2:$4:$5:$6 &&
4752ceb2653Smrg       $posix_glob set +f &&
4762ceb2653Smrg
4772ceb2653Smrg       test "$old" = "$new" &&
4782ceb2653Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4792ceb2653Smrg    then
4802ceb2653Smrg      rm -f "$dsttmp"
4812ceb2653Smrg    else
4822ceb2653Smrg      # Rename the file to the real destination.
4832ceb2653Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4842ceb2653Smrg
4852ceb2653Smrg      # The rename failed, perhaps because mv can't rename something else
4862ceb2653Smrg      # to itself, or perhaps because mv is so ancient that it does not
4872ceb2653Smrg      # support -f.
4882ceb2653Smrg      {
4892ceb2653Smrg	# Now remove or move aside any old file at destination location.
4902ceb2653Smrg	# We try this two ways since rm can't unlink itself on some
4912ceb2653Smrg	# systems and the destination file might be busy for other
4922ceb2653Smrg	# reasons.  In this case, the final cleanup might fail but the new
4932ceb2653Smrg	# file should still install successfully.
4942ceb2653Smrg	{
4952ceb2653Smrg	  test ! -f "$dst" ||
4962ceb2653Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4972ceb2653Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4982ceb2653Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4992ceb2653Smrg	  } ||
5002ceb2653Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5012ceb2653Smrg	    (exit 1); exit 1
5022ceb2653Smrg	  }
5032ceb2653Smrg	} &&
5042ceb2653Smrg
5052ceb2653Smrg	# Now rename the file to the real destination.
5062ceb2653Smrg	$doit $mvcmd "$dsttmp" "$dst"
5072ceb2653Smrg      }
5082ceb2653Smrg    fi || exit 1
5092ceb2653Smrg
5102ceb2653Smrg    trap '' 0
5112ceb2653Smrg  fi
5122ceb2653Smrgdone
513b7d53ee5Smrg
5142ceb2653Smrg# Local variables:
5152ceb2653Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5162ceb2653Smrg# time-stamp-start: "scriptversion="
5172ceb2653Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182ceb2653Smrg# time-stamp-time-zone: "UTC"
5192ceb2653Smrg# time-stamp-end: "; # UTC"
5202ceb2653Smrg# End:
521