install-sh revision 0da4cdcc
19bd41f2cSmrg#!/bin/sh
29bd41f2cSmrg# install - install a program, script, or datafile
39bd41f2cSmrg
40da4cdccSmrgscriptversion=2011-01-19.21; # UTC
59bd41f2cSmrg
69bd41f2cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79bd41f2cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
89bd41f2cSmrg# following copyright and license.
99bd41f2cSmrg#
109bd41f2cSmrg# Copyright (C) 1994 X Consortium
119bd41f2cSmrg#
129bd41f2cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139bd41f2cSmrg# of this software and associated documentation files (the "Software"), to
149bd41f2cSmrg# deal in the Software without restriction, including without limitation the
159bd41f2cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169bd41f2cSmrg# sell copies of the Software, and to permit persons to whom the Software is
179bd41f2cSmrg# furnished to do so, subject to the following conditions:
189bd41f2cSmrg#
199bd41f2cSmrg# The above copyright notice and this permission notice shall be included in
209bd41f2cSmrg# all copies or substantial portions of the Software.
219bd41f2cSmrg#
229bd41f2cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239bd41f2cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249bd41f2cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259bd41f2cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269bd41f2cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279bd41f2cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289bd41f2cSmrg#
299bd41f2cSmrg# Except as contained in this notice, the name of the X Consortium shall not
309bd41f2cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
319bd41f2cSmrg# ings in this Software without prior written authorization from the X Consor-
329bd41f2cSmrg# tium.
339bd41f2cSmrg#
349bd41f2cSmrg#
359bd41f2cSmrg# FSF changes to this file are in the public domain.
369bd41f2cSmrg#
379bd41f2cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389bd41f2cSmrg# `make' implicit rules from creating a file called install from it
399bd41f2cSmrg# when there is no Makefile.
409bd41f2cSmrg#
419bd41f2cSmrg# This script is compatible with the BSD install script, but was written
429418810dSmrg# from scratch.
439418810dSmrg
449418810dSmrgnl='
459418810dSmrg'
469418810dSmrgIFS=" ""	$nl"
479bd41f2cSmrg
489bd41f2cSmrg# set DOITPROG to echo to test this script
499bd41f2cSmrg
509bd41f2cSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
519418810dSmrgdoit=${DOITPROG-}
529418810dSmrgif test -z "$doit"; then
539418810dSmrg  doit_exec=exec
549418810dSmrgelse
559418810dSmrg  doit_exec=$doit
569418810dSmrgfi
579bd41f2cSmrg
589418810dSmrg# Put in absolute file names if you don't have them in your path;
599418810dSmrg# or use environment vars.
609418810dSmrg
619418810dSmrgchgrpprog=${CHGRPPROG-chgrp}
629418810dSmrgchmodprog=${CHMODPROG-chmod}
639418810dSmrgchownprog=${CHOWNPROG-chown}
649418810dSmrgcmpprog=${CMPPROG-cmp}
659418810dSmrgcpprog=${CPPROG-cp}
669418810dSmrgmkdirprog=${MKDIRPROG-mkdir}
679418810dSmrgmvprog=${MVPROG-mv}
689418810dSmrgrmprog=${RMPROG-rm}
699418810dSmrgstripprog=${STRIPPROG-strip}
709418810dSmrg
719418810dSmrgposix_glob='?'
729418810dSmrginitialize_posix_glob='
739418810dSmrg  test "$posix_glob" != "?" || {
749418810dSmrg    if (set -f) 2>/dev/null; then
759418810dSmrg      posix_glob=
769418810dSmrg    else
779418810dSmrg      posix_glob=:
789418810dSmrg    fi
799418810dSmrg  }
809418810dSmrg'
819bd41f2cSmrg
829418810dSmrgposix_mkdir=
839418810dSmrg
849418810dSmrg# Desired mode of installed file.
859418810dSmrgmode=0755
869bd41f2cSmrg
879bd41f2cSmrgchgrpcmd=
889418810dSmrgchmodcmd=$chmodprog
899418810dSmrgchowncmd=
909418810dSmrgmvcmd=$mvprog
919bd41f2cSmrgrmcmd="$rmprog -f"
929418810dSmrgstripcmd=
939418810dSmrg
949bd41f2cSmrgsrc=
959bd41f2cSmrgdst=
969bd41f2cSmrgdir_arg=
979418810dSmrgdst_arg=
989418810dSmrg
999418810dSmrgcopy_on_change=false
1009bd41f2cSmrgno_target_directory=
1019bd41f2cSmrg
1029418810dSmrgusage="\
1039418810dSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1049bd41f2cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1059bd41f2cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1069bd41f2cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
1079bd41f2cSmrg
1089bd41f2cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
1099bd41f2cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1109bd41f2cSmrgIn the 4th, create DIRECTORIES.
1119bd41f2cSmrg
1129bd41f2cSmrgOptions:
1139418810dSmrg     --help     display this help and exit.
1149418810dSmrg     --version  display version info and exit.
1159418810dSmrg
1169418810dSmrg  -c            (ignored)
1179418810dSmrg  -C            install only if different (preserve the last data modification time)
1189418810dSmrg  -d            create directories instead of installing files.
1199418810dSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1209418810dSmrg  -m MODE       $chmodprog installed files to MODE.
1219418810dSmrg  -o USER       $chownprog installed files to USER.
1229418810dSmrg  -s            $stripprog installed files.
1239418810dSmrg  -t DIRECTORY  install into DIRECTORY.
1249418810dSmrg  -T            report an error if DSTFILE is a directory.
1259bd41f2cSmrg
1269bd41f2cSmrgEnvironment variables override the default commands:
1279418810dSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1289418810dSmrg  RMPROG STRIPPROG
1299bd41f2cSmrg"
1309bd41f2cSmrg
1319418810dSmrgwhile test $# -ne 0; do
1329bd41f2cSmrg  case $1 in
1339418810dSmrg    -c) ;;
1349418810dSmrg
1359418810dSmrg    -C) copy_on_change=true;;
1369bd41f2cSmrg
1379418810dSmrg    -d) dir_arg=true;;
1389bd41f2cSmrg
1399bd41f2cSmrg    -g) chgrpcmd="$chgrpprog $2"
1409418810dSmrg	shift;;
1419bd41f2cSmrg
1429bd41f2cSmrg    --help) echo "$usage"; exit $?;;
1439bd41f2cSmrg
1449418810dSmrg    -m) mode=$2
1459418810dSmrg	case $mode in
1469418810dSmrg	  *' '* | *'	'* | *'
1479418810dSmrg'*	  | *'*'* | *'?'* | *'['*)
1489418810dSmrg	    echo "$0: invalid mode: $mode" >&2
1499418810dSmrg	    exit 1;;
1509418810dSmrg	esac
1519418810dSmrg	shift;;
1529bd41f2cSmrg
1539bd41f2cSmrg    -o) chowncmd="$chownprog $2"
1549418810dSmrg	shift;;
1559bd41f2cSmrg
1569418810dSmrg    -s) stripcmd=$stripprog;;
1579bd41f2cSmrg
1589418810dSmrg    -t) dst_arg=$2
1590da4cdccSmrg	# Protect names problematic for `test' and other utilities.
1600da4cdccSmrg	case $dst_arg in
1610da4cdccSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1620da4cdccSmrg	esac
1639418810dSmrg	shift;;
1649bd41f2cSmrg
1659418810dSmrg    -T) no_target_directory=true;;
1669bd41f2cSmrg
1679bd41f2cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1689bd41f2cSmrg
1699418810dSmrg    --)	shift
1709bd41f2cSmrg	break;;
1719418810dSmrg
1729418810dSmrg    -*)	echo "$0: invalid option: $1" >&2
1739418810dSmrg	exit 1;;
1749418810dSmrg
1759418810dSmrg    *)  break;;
1769bd41f2cSmrg  esac
1779418810dSmrg  shift
1789bd41f2cSmrgdone
1799bd41f2cSmrg
1809418810dSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1819418810dSmrg  # When -d is used, all remaining arguments are directories to create.
1829418810dSmrg  # When -t is used, the destination is already specified.
1839418810dSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1849418810dSmrg  for arg
1859418810dSmrg  do
1869418810dSmrg    if test -n "$dst_arg"; then
1879418810dSmrg      # $@ is not empty: it contains at least $arg.
1889418810dSmrg      set fnord "$@" "$dst_arg"
1899418810dSmrg      shift # fnord
1909418810dSmrg    fi
1919418810dSmrg    shift # arg
1929418810dSmrg    dst_arg=$arg
1930da4cdccSmrg    # Protect names problematic for `test' and other utilities.
1940da4cdccSmrg    case $dst_arg in
1950da4cdccSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1960da4cdccSmrg    esac
1979418810dSmrg  done
1989418810dSmrgfi
1999418810dSmrg
2009418810dSmrgif test $# -eq 0; then
2019bd41f2cSmrg  if test -z "$dir_arg"; then
2029bd41f2cSmrg    echo "$0: no input file specified." >&2
2039bd41f2cSmrg    exit 1
2049bd41f2cSmrg  fi
2059bd41f2cSmrg  # It's OK to call `install-sh -d' without argument.
2069bd41f2cSmrg  # This can happen when creating conditional directories.
2079bd41f2cSmrg  exit 0
2089bd41f2cSmrgfi
2099bd41f2cSmrg
2109418810dSmrgif test -z "$dir_arg"; then
2110da4cdccSmrg  do_exit='(exit $ret); exit $ret'
2120da4cdccSmrg  trap "ret=129; $do_exit" 1
2130da4cdccSmrg  trap "ret=130; $do_exit" 2
2140da4cdccSmrg  trap "ret=141; $do_exit" 13
2150da4cdccSmrg  trap "ret=143; $do_exit" 15
2169418810dSmrg
2179418810dSmrg  # Set umask so as not to create temps with too-generous modes.
2189418810dSmrg  # However, 'strip' requires both read and write access to temps.
2199418810dSmrg  case $mode in
2209418810dSmrg    # Optimize common cases.
2219418810dSmrg    *644) cp_umask=133;;
2229418810dSmrg    *755) cp_umask=22;;
2239418810dSmrg
2249418810dSmrg    *[0-7])
2259418810dSmrg      if test -z "$stripcmd"; then
2269418810dSmrg	u_plus_rw=
2279418810dSmrg      else
2289418810dSmrg	u_plus_rw='% 200'
2299418810dSmrg      fi
2309418810dSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2319418810dSmrg    *)
2329418810dSmrg      if test -z "$stripcmd"; then
2339418810dSmrg	u_plus_rw=
2349418810dSmrg      else
2359418810dSmrg	u_plus_rw=,u+rw
2369418810dSmrg      fi
2379418810dSmrg      cp_umask=$mode$u_plus_rw;;
2389418810dSmrg  esac
2399418810dSmrgfi
2409418810dSmrg
2419bd41f2cSmrgfor src
2429bd41f2cSmrgdo
2430da4cdccSmrg  # Protect names problematic for `test' and other utilities.
2449bd41f2cSmrg  case $src in
2450da4cdccSmrg    -* | [=\(\)!]) src=./$src;;
2469bd41f2cSmrg  esac
2479bd41f2cSmrg
2489bd41f2cSmrg  if test -n "$dir_arg"; then
2499bd41f2cSmrg    dst=$src
2509418810dSmrg    dstdir=$dst
2519418810dSmrg    test -d "$dstdir"
2529418810dSmrg    dstdir_status=$?
253d075918cSmrg  else
2549418810dSmrg
2559bd41f2cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2569bd41f2cSmrg    # might cause directories to be created, which would be especially bad
2579bd41f2cSmrg    # if $src (and thus $dsttmp) contains '*'.
2589bd41f2cSmrg    if test ! -f "$src" && test ! -d "$src"; then
2599bd41f2cSmrg      echo "$0: $src does not exist." >&2
2609bd41f2cSmrg      exit 1
2619bd41f2cSmrg    fi
2629bd41f2cSmrg
2639418810dSmrg    if test -z "$dst_arg"; then
2649bd41f2cSmrg      echo "$0: no destination specified." >&2
2659bd41f2cSmrg      exit 1
2669bd41f2cSmrg    fi
2679418810dSmrg    dst=$dst_arg
2689bd41f2cSmrg
2699bd41f2cSmrg    # If destination is a directory, append the input filename; won't work
2709bd41f2cSmrg    # if double slashes aren't ignored.
2719bd41f2cSmrg    if test -d "$dst"; then
2729bd41f2cSmrg      if test -n "$no_target_directory"; then
2739418810dSmrg	echo "$0: $dst_arg: Is a directory" >&2
2749bd41f2cSmrg	exit 1
2759bd41f2cSmrg      fi
2769418810dSmrg      dstdir=$dst
2779418810dSmrg      dst=$dstdir/`basename "$src"`
2789418810dSmrg      dstdir_status=0
2799418810dSmrg    else
2809418810dSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2819418810dSmrg      dstdir=`
2829418810dSmrg	(dirname "$dst") 2>/dev/null ||
2839418810dSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2849418810dSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2859418810dSmrg	     X"$dst" : 'X\(//\)$' \| \
2869418810dSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2879418810dSmrg	echo X"$dst" |
2889418810dSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2899418810dSmrg		   s//\1/
2909418810dSmrg		   q
2919418810dSmrg		 }
2929418810dSmrg		 /^X\(\/\/\)[^/].*/{
2939418810dSmrg		   s//\1/
2949418810dSmrg		   q
2959418810dSmrg		 }
2969418810dSmrg		 /^X\(\/\/\)$/{
2979418810dSmrg		   s//\1/
2989418810dSmrg		   q
2999418810dSmrg		 }
3009418810dSmrg		 /^X\(\/\).*/{
3019418810dSmrg		   s//\1/
3029418810dSmrg		   q
3039418810dSmrg		 }
3049418810dSmrg		 s/.*/./; q'
3059418810dSmrg      `
3069418810dSmrg
3079418810dSmrg      test -d "$dstdir"
3089418810dSmrg      dstdir_status=$?
3099bd41f2cSmrg    fi
3109bd41f2cSmrg  fi
3119bd41f2cSmrg
3129418810dSmrg  obsolete_mkdir_used=false
3139418810dSmrg
3149418810dSmrg  if test $dstdir_status != 0; then
3159418810dSmrg    case $posix_mkdir in
3169418810dSmrg      '')
3179418810dSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3189418810dSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3199418810dSmrg	umask=`umask`
3209418810dSmrg	case $stripcmd.$umask in
3219418810dSmrg	  # Optimize common cases.
3229418810dSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3239418810dSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3249418810dSmrg
3259418810dSmrg	  *[0-7])
3269418810dSmrg	    mkdir_umask=`expr $umask + 22 \
3279418810dSmrg	      - $umask % 100 % 40 + $umask % 20 \
3289418810dSmrg	      - $umask % 10 % 4 + $umask % 2
3299418810dSmrg	    `;;
3309418810dSmrg	  *) mkdir_umask=$umask,go-w;;
3319418810dSmrg	esac
3329418810dSmrg
3339418810dSmrg	# With -d, create the new directory with the user-specified mode.
3349418810dSmrg	# Otherwise, rely on $mkdir_umask.
3359418810dSmrg	if test -n "$dir_arg"; then
3369418810dSmrg	  mkdir_mode=-m$mode
3379418810dSmrg	else
3389418810dSmrg	  mkdir_mode=
3399418810dSmrg	fi
3409418810dSmrg
3419418810dSmrg	posix_mkdir=false
3429418810dSmrg	case $umask in
3439418810dSmrg	  *[123567][0-7][0-7])
3449418810dSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3459418810dSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3469418810dSmrg	    ;;
3479418810dSmrg	  *)
3489418810dSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3499418810dSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3509418810dSmrg
3519418810dSmrg	    if (umask $mkdir_umask &&
3529418810dSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3539418810dSmrg	    then
3549418810dSmrg	      if test -z "$dir_arg" || {
3559418810dSmrg		   # Check for POSIX incompatibilities with -m.
3569418810dSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3579418810dSmrg		   # other-writeable bit of parent directory when it shouldn't.
3589418810dSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3599418810dSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3609418810dSmrg		   case $ls_ld_tmpdir in
3619418810dSmrg		     d????-?r-*) different_mode=700;;
3629418810dSmrg		     d????-?--*) different_mode=755;;
3639418810dSmrg		     *) false;;
3649418810dSmrg		   esac &&
3659418810dSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3669418810dSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3679418810dSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3689418810dSmrg		   }
3699418810dSmrg		 }
3709418810dSmrg	      then posix_mkdir=:
3719418810dSmrg	      fi
3729418810dSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3739418810dSmrg	    else
3749418810dSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3759418810dSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3769418810dSmrg	    fi
3779418810dSmrg	    trap '' 0;;
3789418810dSmrg	esac;;
3799418810dSmrg    esac
3809bd41f2cSmrg
3819418810dSmrg    if
3829418810dSmrg      $posix_mkdir && (
3839418810dSmrg	umask $mkdir_umask &&
3849418810dSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3859418810dSmrg      )
3869418810dSmrg    then :
3879418810dSmrg    else
388d075918cSmrg
3899418810dSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3909418810dSmrg      # or it failed possibly due to a race condition.  Create the
3919418810dSmrg      # directory the slow way, step by step, checking for races as we go.
3929bd41f2cSmrg
3939418810dSmrg      case $dstdir in
3949418810dSmrg	/*) prefix='/';;
3950da4cdccSmrg	[-=\(\)!]*) prefix='./';;
3969418810dSmrg	*)  prefix='';;
3979418810dSmrg      esac
398d075918cSmrg
3999418810dSmrg      eval "$initialize_posix_glob"
400d075918cSmrg
4019418810dSmrg      oIFS=$IFS
4029418810dSmrg      IFS=/
4039418810dSmrg      $posix_glob set -f
4049418810dSmrg      set fnord $dstdir
4059bd41f2cSmrg      shift
4069418810dSmrg      $posix_glob set +f
4079418810dSmrg      IFS=$oIFS
4089418810dSmrg
4099418810dSmrg      prefixes=
4109418810dSmrg
4119418810dSmrg      for d
4129418810dSmrg      do
4130da4cdccSmrg	test X"$d" = X && continue
4149418810dSmrg
4159418810dSmrg	prefix=$prefix$d
4169418810dSmrg	if test -d "$prefix"; then
4179418810dSmrg	  prefixes=
4189418810dSmrg	else
4199418810dSmrg	  if $posix_mkdir; then
4209418810dSmrg	    (umask=$mkdir_umask &&
4219418810dSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4229418810dSmrg	    # Don't fail if two instances are running concurrently.
4239418810dSmrg	    test -d "$prefix" || exit 1
4249418810dSmrg	  else
4259418810dSmrg	    case $prefix in
4269418810dSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4279418810dSmrg	      *) qprefix=$prefix;;
4289418810dSmrg	    esac
4299418810dSmrg	    prefixes="$prefixes '$qprefix'"
4309418810dSmrg	  fi
4319418810dSmrg	fi
4329418810dSmrg	prefix=$prefix/
4339418810dSmrg      done
4349418810dSmrg
4359418810dSmrg      if test -n "$prefixes"; then
4369418810dSmrg	# Don't fail if two instances are running concurrently.
4379418810dSmrg	(umask $mkdir_umask &&
4389418810dSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4399418810dSmrg	  test -d "$dstdir" || exit 1
4409418810dSmrg	obsolete_mkdir_used=true
4419bd41f2cSmrg      fi
4429418810dSmrg    fi
4439bd41f2cSmrg  fi
4449bd41f2cSmrg
4459bd41f2cSmrg  if test -n "$dir_arg"; then
4469418810dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4479418810dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4489418810dSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4499418810dSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4509bd41f2cSmrg  else
4519bd41f2cSmrg
4529bd41f2cSmrg    # Make a couple of temp file names in the proper directory.
4539bd41f2cSmrg    dsttmp=$dstdir/_inst.$$_
4549bd41f2cSmrg    rmtmp=$dstdir/_rm.$$_
4559bd41f2cSmrg
4569bd41f2cSmrg    # Trap to clean up those temp files at exit.
4579bd41f2cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4589bd41f2cSmrg
4599bd41f2cSmrg    # Copy the file name to the temp name.
4609418810dSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4619bd41f2cSmrg
4629bd41f2cSmrg    # and set any options; do chmod last to preserve setuid bits.
4639bd41f2cSmrg    #
4649bd41f2cSmrg    # If any of these fail, we abort the whole thing.  If we want to
4659bd41f2cSmrg    # ignore errors from any of these, just make sure not to ignore
4669bd41f2cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4679bd41f2cSmrg    #
4689418810dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4699418810dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4709418810dSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4719418810dSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4729418810dSmrg
4739418810dSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4749418810dSmrg    if $copy_on_change &&
4759418810dSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4769418810dSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4779418810dSmrg
4789418810dSmrg       eval "$initialize_posix_glob" &&
4799418810dSmrg       $posix_glob set -f &&
4809418810dSmrg       set X $old && old=:$2:$4:$5:$6 &&
4819418810dSmrg       set X $new && new=:$2:$4:$5:$6 &&
4829418810dSmrg       $posix_glob set +f &&
4839418810dSmrg
4849418810dSmrg       test "$old" = "$new" &&
4859418810dSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4869418810dSmrg    then
4879418810dSmrg      rm -f "$dsttmp"
4889418810dSmrg    else
4899418810dSmrg      # Rename the file to the real destination.
4909418810dSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4919418810dSmrg
4929418810dSmrg      # The rename failed, perhaps because mv can't rename something else
4939418810dSmrg      # to itself, or perhaps because mv is so ancient that it does not
4949418810dSmrg      # support -f.
4959418810dSmrg      {
4969418810dSmrg	# Now remove or move aside any old file at destination location.
4979418810dSmrg	# We try this two ways since rm can't unlink itself on some
4989418810dSmrg	# systems and the destination file might be busy for other
4999418810dSmrg	# reasons.  In this case, the final cleanup might fail but the new
5009418810dSmrg	# file should still install successfully.
5019418810dSmrg	{
5029418810dSmrg	  test ! -f "$dst" ||
5039418810dSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5049418810dSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5059418810dSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5069418810dSmrg	  } ||
5079418810dSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
5089418810dSmrg	    (exit 1); exit 1
5099418810dSmrg	  }
5109418810dSmrg	} &&
5119418810dSmrg
5129418810dSmrg	# Now rename the file to the real destination.
5139418810dSmrg	$doit $mvcmd "$dsttmp" "$dst"
5149418810dSmrg      }
5159418810dSmrg    fi || exit 1
5169418810dSmrg
5179418810dSmrg    trap '' 0
5189418810dSmrg  fi
5199bd41f2cSmrgdone
5209bd41f2cSmrg
5219bd41f2cSmrg# Local variables:
5229bd41f2cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5239bd41f2cSmrg# time-stamp-start: "scriptversion="
5249bd41f2cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5250da4cdccSmrg# time-stamp-time-zone: "UTC"
5260da4cdccSmrg# time-stamp-end: "; # UTC"
5279bd41f2cSmrg# End:
528