install-sh revision d8556812
176888252Smrg#!/bin/sh
276888252Smrg# install - install a program, script, or datafile
376888252Smrg
4d8556812Smrgscriptversion=2009-04-28.21; # UTC
576888252Smrg
676888252Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
776888252Smrg# later released in X11R6 (xc/config/util/install.sh) with the
876888252Smrg# following copyright and license.
976888252Smrg#
1076888252Smrg# Copyright (C) 1994 X Consortium
1176888252Smrg#
1276888252Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1376888252Smrg# of this software and associated documentation files (the "Software"), to
1476888252Smrg# deal in the Software without restriction, including without limitation the
1576888252Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1676888252Smrg# sell copies of the Software, and to permit persons to whom the Software is
1776888252Smrg# furnished to do so, subject to the following conditions:
1876888252Smrg#
1976888252Smrg# The above copyright notice and this permission notice shall be included in
2076888252Smrg# all copies or substantial portions of the Software.
2176888252Smrg#
2276888252Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2376888252Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2476888252Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2576888252Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2676888252Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2776888252Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2876888252Smrg#
2976888252Smrg# Except as contained in this notice, the name of the X Consortium shall not
3076888252Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3176888252Smrg# ings in this Software without prior written authorization from the X Consor-
3276888252Smrg# tium.
3376888252Smrg#
3476888252Smrg#
3576888252Smrg# FSF changes to this file are in the public domain.
3676888252Smrg#
3776888252Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3876888252Smrg# `make' implicit rules from creating a file called install from it
3976888252Smrg# when there is no Makefile.
4076888252Smrg#
4176888252Smrg# This script is compatible with the BSD install script, but was written
421ae1b5e8Smrg# from scratch.
431ae1b5e8Smrg
441ae1b5e8Smrgnl='
451ae1b5e8Smrg'
461ae1b5e8SmrgIFS=" ""	$nl"
4776888252Smrg
4876888252Smrg# set DOITPROG to echo to test this script
4976888252Smrg
5076888252Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5186dafe34Smrgdoit=${DOITPROG-}
521ae1b5e8Smrgif test -z "$doit"; then
531ae1b5e8Smrg  doit_exec=exec
541ae1b5e8Smrgelse
551ae1b5e8Smrg  doit_exec=$doit
561ae1b5e8Smrgfi
5776888252Smrg
581ae1b5e8Smrg# Put in absolute file names if you don't have them in your path;
591ae1b5e8Smrg# or use environment vars.
6076888252Smrg
6186dafe34Smrgchgrpprog=${CHGRPPROG-chgrp}
6286dafe34Smrgchmodprog=${CHMODPROG-chmod}
6386dafe34Smrgchownprog=${CHOWNPROG-chown}
6486dafe34Smrgcmpprog=${CMPPROG-cmp}
6586dafe34Smrgcpprog=${CPPROG-cp}
6686dafe34Smrgmkdirprog=${MKDIRPROG-mkdir}
6786dafe34Smrgmvprog=${MVPROG-mv}
6886dafe34Smrgrmprog=${RMPROG-rm}
6986dafe34Smrgstripprog=${STRIPPROG-strip}
7086dafe34Smrg
7186dafe34Smrgposix_glob='?'
7286dafe34Smrginitialize_posix_glob='
7386dafe34Smrg  test "$posix_glob" != "?" || {
7486dafe34Smrg    if (set -f) 2>/dev/null; then
7586dafe34Smrg      posix_glob=
7686dafe34Smrg    else
7786dafe34Smrg      posix_glob=:
7886dafe34Smrg    fi
7986dafe34Smrg  }
8086dafe34Smrg'
8176888252Smrg
821ae1b5e8Smrgposix_mkdir=
831ae1b5e8Smrg
841ae1b5e8Smrg# Desired mode of installed file.
851ae1b5e8Smrgmode=0755
861ae1b5e8Smrg
8786dafe34Smrgchgrpcmd=
881ae1b5e8Smrgchmodcmd=$chmodprog
8976888252Smrgchowncmd=
9086dafe34Smrgmvcmd=$mvprog
9176888252Smrgrmcmd="$rmprog -f"
9286dafe34Smrgstripcmd=
9386dafe34Smrg
9476888252Smrgsrc=
9576888252Smrgdst=
9676888252Smrgdir_arg=
9786dafe34Smrgdst_arg=
9886dafe34Smrg
9986dafe34Smrgcopy_on_change=false
10076888252Smrgno_target_directory=
10176888252Smrg
10286dafe34Smrgusage="\
10386dafe34SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10476888252Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10576888252Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10676888252Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10776888252Smrg
10876888252SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10976888252SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11076888252SmrgIn the 4th, create DIRECTORIES.
11176888252Smrg
11276888252SmrgOptions:
11386dafe34Smrg     --help     display this help and exit.
11486dafe34Smrg     --version  display version info and exit.
11586dafe34Smrg
11686dafe34Smrg  -c            (ignored)
11786dafe34Smrg  -C            install only if different (preserve the last data modification time)
11886dafe34Smrg  -d            create directories instead of installing files.
11986dafe34Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12086dafe34Smrg  -m MODE       $chmodprog installed files to MODE.
12186dafe34Smrg  -o USER       $chownprog installed files to USER.
12286dafe34Smrg  -s            $stripprog installed files.
12386dafe34Smrg  -t DIRECTORY  install into DIRECTORY.
12486dafe34Smrg  -T            report an error if DSTFILE is a directory.
12576888252Smrg
12676888252SmrgEnvironment variables override the default commands:
12786dafe34Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12886dafe34Smrg  RMPROG STRIPPROG
12976888252Smrg"
13076888252Smrg
1311ae1b5e8Smrgwhile test $# -ne 0; do
13276888252Smrg  case $1 in
13386dafe34Smrg    -c) ;;
13486dafe34Smrg
13586dafe34Smrg    -C) copy_on_change=true;;
13676888252Smrg
13786dafe34Smrg    -d) dir_arg=true;;
13876888252Smrg
13976888252Smrg    -g) chgrpcmd="$chgrpprog $2"
14086dafe34Smrg	shift;;
14176888252Smrg
14276888252Smrg    --help) echo "$usage"; exit $?;;
14376888252Smrg
1441ae1b5e8Smrg    -m) mode=$2
1451ae1b5e8Smrg	case $mode in
1461ae1b5e8Smrg	  *' '* | *'	'* | *'
1471ae1b5e8Smrg'*	  | *'*'* | *'?'* | *'['*)
1481ae1b5e8Smrg	    echo "$0: invalid mode: $mode" >&2
1491ae1b5e8Smrg	    exit 1;;
1501ae1b5e8Smrg	esac
15186dafe34Smrg	shift;;
15276888252Smrg
15376888252Smrg    -o) chowncmd="$chownprog $2"
15486dafe34Smrg	shift;;
15576888252Smrg
15686dafe34Smrg    -s) stripcmd=$stripprog;;
15776888252Smrg
15886dafe34Smrg    -t) dst_arg=$2
15986dafe34Smrg	shift;;
16076888252Smrg
16186dafe34Smrg    -T) no_target_directory=true;;
16276888252Smrg
16376888252Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16476888252Smrg
1651ae1b5e8Smrg    --)	shift
16676888252Smrg	break;;
1671ae1b5e8Smrg
1681ae1b5e8Smrg    -*)	echo "$0: invalid option: $1" >&2
1691ae1b5e8Smrg	exit 1;;
1701ae1b5e8Smrg
1711ae1b5e8Smrg    *)  break;;
17276888252Smrg  esac
17386dafe34Smrg  shift
17476888252Smrgdone
17576888252Smrg
17686dafe34Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771ae1b5e8Smrg  # When -d is used, all remaining arguments are directories to create.
1781ae1b5e8Smrg  # When -t is used, the destination is already specified.
1791ae1b5e8Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801ae1b5e8Smrg  for arg
1811ae1b5e8Smrg  do
18286dafe34Smrg    if test -n "$dst_arg"; then
1831ae1b5e8Smrg      # $@ is not empty: it contains at least $arg.
18486dafe34Smrg      set fnord "$@" "$dst_arg"
1851ae1b5e8Smrg      shift # fnord
1861ae1b5e8Smrg    fi
1871ae1b5e8Smrg    shift # arg
18886dafe34Smrg    dst_arg=$arg
1891ae1b5e8Smrg  done
1901ae1b5e8Smrgfi
1911ae1b5e8Smrg
1921ae1b5e8Smrgif test $# -eq 0; then
19376888252Smrg  if test -z "$dir_arg"; then
19476888252Smrg    echo "$0: no input file specified." >&2
19576888252Smrg    exit 1
19676888252Smrg  fi
19776888252Smrg  # It's OK to call `install-sh -d' without argument.
19876888252Smrg  # This can happen when creating conditional directories.
19976888252Smrg  exit 0
20076888252Smrgfi
20176888252Smrg
2021ae1b5e8Smrgif test -z "$dir_arg"; then
2031ae1b5e8Smrg  trap '(exit $?); exit' 1 2 13 15
2041ae1b5e8Smrg
2051ae1b5e8Smrg  # Set umask so as not to create temps with too-generous modes.
2061ae1b5e8Smrg  # However, 'strip' requires both read and write access to temps.
2071ae1b5e8Smrg  case $mode in
2081ae1b5e8Smrg    # Optimize common cases.
2091ae1b5e8Smrg    *644) cp_umask=133;;
2101ae1b5e8Smrg    *755) cp_umask=22;;
2111ae1b5e8Smrg
2121ae1b5e8Smrg    *[0-7])
2131ae1b5e8Smrg      if test -z "$stripcmd"; then
2141ae1b5e8Smrg	u_plus_rw=
2151ae1b5e8Smrg      else
2161ae1b5e8Smrg	u_plus_rw='% 200'
2171ae1b5e8Smrg      fi
2181ae1b5e8Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2191ae1b5e8Smrg    *)
2201ae1b5e8Smrg      if test -z "$stripcmd"; then
2211ae1b5e8Smrg	u_plus_rw=
2221ae1b5e8Smrg      else
2231ae1b5e8Smrg	u_plus_rw=,u+rw
2241ae1b5e8Smrg      fi
2251ae1b5e8Smrg      cp_umask=$mode$u_plus_rw;;
2261ae1b5e8Smrg  esac
2271ae1b5e8Smrgfi
2281ae1b5e8Smrg
22976888252Smrgfor src
23076888252Smrgdo
23176888252Smrg  # Protect names starting with `-'.
23276888252Smrg  case $src in
23386dafe34Smrg    -*) src=./$src;;
23476888252Smrg  esac
23576888252Smrg
23676888252Smrg  if test -n "$dir_arg"; then
23776888252Smrg    dst=$src
2381ae1b5e8Smrg    dstdir=$dst
2391ae1b5e8Smrg    test -d "$dstdir"
2401ae1b5e8Smrg    dstdir_status=$?
24176888252Smrg  else
2421ae1b5e8Smrg
24376888252Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24476888252Smrg    # might cause directories to be created, which would be especially bad
24576888252Smrg    # if $src (and thus $dsttmp) contains '*'.
24676888252Smrg    if test ! -f "$src" && test ! -d "$src"; then
24776888252Smrg      echo "$0: $src does not exist." >&2
24876888252Smrg      exit 1
24976888252Smrg    fi
25076888252Smrg
25186dafe34Smrg    if test -z "$dst_arg"; then
25276888252Smrg      echo "$0: no destination specified." >&2
25376888252Smrg      exit 1
25476888252Smrg    fi
25576888252Smrg
25686dafe34Smrg    dst=$dst_arg
25776888252Smrg    # Protect names starting with `-'.
25876888252Smrg    case $dst in
25986dafe34Smrg      -*) dst=./$dst;;
26076888252Smrg    esac
26176888252Smrg
26276888252Smrg    # If destination is a directory, append the input filename; won't work
26376888252Smrg    # if double slashes aren't ignored.
26476888252Smrg    if test -d "$dst"; then
26576888252Smrg      if test -n "$no_target_directory"; then
26686dafe34Smrg	echo "$0: $dst_arg: Is a directory" >&2
26776888252Smrg	exit 1
26876888252Smrg      fi
2691ae1b5e8Smrg      dstdir=$dst
2701ae1b5e8Smrg      dst=$dstdir/`basename "$src"`
2711ae1b5e8Smrg      dstdir_status=0
2721ae1b5e8Smrg    else
2731ae1b5e8Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2741ae1b5e8Smrg      dstdir=`
2751ae1b5e8Smrg	(dirname "$dst") 2>/dev/null ||
2761ae1b5e8Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2771ae1b5e8Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2781ae1b5e8Smrg	     X"$dst" : 'X\(//\)$' \| \
2791ae1b5e8Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2801ae1b5e8Smrg	echo X"$dst" |
2811ae1b5e8Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2821ae1b5e8Smrg		   s//\1/
2831ae1b5e8Smrg		   q
2841ae1b5e8Smrg		 }
2851ae1b5e8Smrg		 /^X\(\/\/\)[^/].*/{
2861ae1b5e8Smrg		   s//\1/
2871ae1b5e8Smrg		   q
2881ae1b5e8Smrg		 }
2891ae1b5e8Smrg		 /^X\(\/\/\)$/{
2901ae1b5e8Smrg		   s//\1/
2911ae1b5e8Smrg		   q
2921ae1b5e8Smrg		 }
2931ae1b5e8Smrg		 /^X\(\/\).*/{
2941ae1b5e8Smrg		   s//\1/
2951ae1b5e8Smrg		   q
2961ae1b5e8Smrg		 }
2971ae1b5e8Smrg		 s/.*/./; q'
2981ae1b5e8Smrg      `
2991ae1b5e8Smrg
3001ae1b5e8Smrg      test -d "$dstdir"
3011ae1b5e8Smrg      dstdir_status=$?
30276888252Smrg    fi
30376888252Smrg  fi
30476888252Smrg
3051ae1b5e8Smrg  obsolete_mkdir_used=false
3061ae1b5e8Smrg
3071ae1b5e8Smrg  if test $dstdir_status != 0; then
3081ae1b5e8Smrg    case $posix_mkdir in
3091ae1b5e8Smrg      '')
3101ae1b5e8Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3111ae1b5e8Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3121ae1b5e8Smrg	umask=`umask`
3131ae1b5e8Smrg	case $stripcmd.$umask in
3141ae1b5e8Smrg	  # Optimize common cases.
3151ae1b5e8Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3161ae1b5e8Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3171ae1b5e8Smrg
3181ae1b5e8Smrg	  *[0-7])
3191ae1b5e8Smrg	    mkdir_umask=`expr $umask + 22 \
3201ae1b5e8Smrg	      - $umask % 100 % 40 + $umask % 20 \
3211ae1b5e8Smrg	      - $umask % 10 % 4 + $umask % 2
3221ae1b5e8Smrg	    `;;
3231ae1b5e8Smrg	  *) mkdir_umask=$umask,go-w;;
3241ae1b5e8Smrg	esac
3251ae1b5e8Smrg
3261ae1b5e8Smrg	# With -d, create the new directory with the user-specified mode.
3271ae1b5e8Smrg	# Otherwise, rely on $mkdir_umask.
3281ae1b5e8Smrg	if test -n "$dir_arg"; then
3291ae1b5e8Smrg	  mkdir_mode=-m$mode
3301ae1b5e8Smrg	else
3311ae1b5e8Smrg	  mkdir_mode=
3321ae1b5e8Smrg	fi
3331ae1b5e8Smrg
3341ae1b5e8Smrg	posix_mkdir=false
3351ae1b5e8Smrg	case $umask in
3361ae1b5e8Smrg	  *[123567][0-7][0-7])
3371ae1b5e8Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3381ae1b5e8Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3391ae1b5e8Smrg	    ;;
3401ae1b5e8Smrg	  *)
3411ae1b5e8Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3421ae1b5e8Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3431ae1b5e8Smrg
3441ae1b5e8Smrg	    if (umask $mkdir_umask &&
3451ae1b5e8Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3461ae1b5e8Smrg	    then
3471ae1b5e8Smrg	      if test -z "$dir_arg" || {
3481ae1b5e8Smrg		   # Check for POSIX incompatibilities with -m.
3491ae1b5e8Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3501ae1b5e8Smrg		   # other-writeable bit of parent directory when it shouldn't.
3511ae1b5e8Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3521ae1b5e8Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3531ae1b5e8Smrg		   case $ls_ld_tmpdir in
3541ae1b5e8Smrg		     d????-?r-*) different_mode=700;;
3551ae1b5e8Smrg		     d????-?--*) different_mode=755;;
3561ae1b5e8Smrg		     *) false;;
3571ae1b5e8Smrg		   esac &&
3581ae1b5e8Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3591ae1b5e8Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3601ae1b5e8Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3611ae1b5e8Smrg		   }
3621ae1b5e8Smrg		 }
3631ae1b5e8Smrg	      then posix_mkdir=:
3641ae1b5e8Smrg	      fi
3651ae1b5e8Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3661ae1b5e8Smrg	    else
3671ae1b5e8Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3681ae1b5e8Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3691ae1b5e8Smrg	    fi
3701ae1b5e8Smrg	    trap '' 0;;
3711ae1b5e8Smrg	esac;;
3721ae1b5e8Smrg    esac
37376888252Smrg
3741ae1b5e8Smrg    if
3751ae1b5e8Smrg      $posix_mkdir && (
3761ae1b5e8Smrg	umask $mkdir_umask &&
3771ae1b5e8Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3781ae1b5e8Smrg      )
3791ae1b5e8Smrg    then :
3801ae1b5e8Smrg    else
38176888252Smrg
3821ae1b5e8Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3831ae1b5e8Smrg      # or it failed possibly due to a race condition.  Create the
3841ae1b5e8Smrg      # directory the slow way, step by step, checking for races as we go.
3851ae1b5e8Smrg
3861ae1b5e8Smrg      case $dstdir in
38786dafe34Smrg	/*) prefix='/';;
38886dafe34Smrg	-*) prefix='./';;
38986dafe34Smrg	*)  prefix='';;
3901ae1b5e8Smrg      esac
3911ae1b5e8Smrg
39286dafe34Smrg      eval "$initialize_posix_glob"
3931ae1b5e8Smrg
3941ae1b5e8Smrg      oIFS=$IFS
3951ae1b5e8Smrg      IFS=/
39686dafe34Smrg      $posix_glob set -f
3971ae1b5e8Smrg      set fnord $dstdir
39876888252Smrg      shift
39986dafe34Smrg      $posix_glob set +f
4001ae1b5e8Smrg      IFS=$oIFS
4011ae1b5e8Smrg
4021ae1b5e8Smrg      prefixes=
4031ae1b5e8Smrg
4041ae1b5e8Smrg      for d
4051ae1b5e8Smrg      do
4061ae1b5e8Smrg	test -z "$d" && continue
4071ae1b5e8Smrg
4081ae1b5e8Smrg	prefix=$prefix$d
4091ae1b5e8Smrg	if test -d "$prefix"; then
4101ae1b5e8Smrg	  prefixes=
4111ae1b5e8Smrg	else
4121ae1b5e8Smrg	  if $posix_mkdir; then
4131ae1b5e8Smrg	    (umask=$mkdir_umask &&
4141ae1b5e8Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4151ae1b5e8Smrg	    # Don't fail if two instances are running concurrently.
4161ae1b5e8Smrg	    test -d "$prefix" || exit 1
4171ae1b5e8Smrg	  else
4181ae1b5e8Smrg	    case $prefix in
4191ae1b5e8Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4201ae1b5e8Smrg	      *) qprefix=$prefix;;
4211ae1b5e8Smrg	    esac
4221ae1b5e8Smrg	    prefixes="$prefixes '$qprefix'"
4231ae1b5e8Smrg	  fi
4241ae1b5e8Smrg	fi
4251ae1b5e8Smrg	prefix=$prefix/
4261ae1b5e8Smrg      done
4271ae1b5e8Smrg
4281ae1b5e8Smrg      if test -n "$prefixes"; then
4291ae1b5e8Smrg	# Don't fail if two instances are running concurrently.
4301ae1b5e8Smrg	(umask $mkdir_umask &&
4311ae1b5e8Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4321ae1b5e8Smrg	  test -d "$dstdir" || exit 1
4331ae1b5e8Smrg	obsolete_mkdir_used=true
43476888252Smrg      fi
4351ae1b5e8Smrg    fi
43676888252Smrg  fi
43776888252Smrg
43876888252Smrg  if test -n "$dir_arg"; then
4391ae1b5e8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4401ae1b5e8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4411ae1b5e8Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4421ae1b5e8Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44376888252Smrg  else
44476888252Smrg
44576888252Smrg    # Make a couple of temp file names in the proper directory.
44676888252Smrg    dsttmp=$dstdir/_inst.$$_
44776888252Smrg    rmtmp=$dstdir/_rm.$$_
44876888252Smrg
44976888252Smrg    # Trap to clean up those temp files at exit.
45076888252Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45176888252Smrg
45276888252Smrg    # Copy the file name to the temp name.
4531ae1b5e8Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45476888252Smrg
45576888252Smrg    # and set any options; do chmod last to preserve setuid bits.
45676888252Smrg    #
45776888252Smrg    # If any of these fail, we abort the whole thing.  If we want to
45876888252Smrg    # ignore errors from any of these, just make sure not to ignore
45976888252Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46076888252Smrg    #
46186dafe34Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46286dafe34Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46386dafe34Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46486dafe34Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46586dafe34Smrg
46686dafe34Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46786dafe34Smrg    if $copy_on_change &&
46886dafe34Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46986dafe34Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47086dafe34Smrg
47186dafe34Smrg       eval "$initialize_posix_glob" &&
47286dafe34Smrg       $posix_glob set -f &&
47386dafe34Smrg       set X $old && old=:$2:$4:$5:$6 &&
47486dafe34Smrg       set X $new && new=:$2:$4:$5:$6 &&
47586dafe34Smrg       $posix_glob set +f &&
47686dafe34Smrg
47786dafe34Smrg       test "$old" = "$new" &&
47886dafe34Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47986dafe34Smrg    then
48086dafe34Smrg      rm -f "$dsttmp"
48186dafe34Smrg    else
48286dafe34Smrg      # Rename the file to the real destination.
48386dafe34Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48486dafe34Smrg
48586dafe34Smrg      # The rename failed, perhaps because mv can't rename something else
48686dafe34Smrg      # to itself, or perhaps because mv is so ancient that it does not
48786dafe34Smrg      # support -f.
48886dafe34Smrg      {
48986dafe34Smrg	# Now remove or move aside any old file at destination location.
49086dafe34Smrg	# We try this two ways since rm can't unlink itself on some
49186dafe34Smrg	# systems and the destination file might be busy for other
49286dafe34Smrg	# reasons.  In this case, the final cleanup might fail but the new
49386dafe34Smrg	# file should still install successfully.
49486dafe34Smrg	{
49586dafe34Smrg	  test ! -f "$dst" ||
49686dafe34Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49786dafe34Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49886dafe34Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49986dafe34Smrg	  } ||
50086dafe34Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50186dafe34Smrg	    (exit 1); exit 1
50286dafe34Smrg	  }
50386dafe34Smrg	} &&
50486dafe34Smrg
50586dafe34Smrg	# Now rename the file to the real destination.
50686dafe34Smrg	$doit $mvcmd "$dsttmp" "$dst"
50786dafe34Smrg      }
50886dafe34Smrg    fi || exit 1
50976888252Smrg
5101ae1b5e8Smrg    trap '' 0
5111ae1b5e8Smrg  fi
5121ae1b5e8Smrgdone
51376888252Smrg
51476888252Smrg# Local variables:
51576888252Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51676888252Smrg# time-stamp-start: "scriptversion="
51776888252Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518d8556812Smrg# time-stamp-time-zone: "UTC"
519d8556812Smrg# time-stamp-end: "; # UTC"
52076888252Smrg# End:
521