install-sh revision 6df26cac
16df26cacSmrg#!/bin/sh
26df26cacSmrg# install - install a program, script, or datafile
36df26cacSmrg
46df26cacSmrgscriptversion=2006-10-14.15
56df26cacSmrg
66df26cacSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76df26cacSmrg# later released in X11R6 (xc/config/util/install.sh) with the
86df26cacSmrg# following copyright and license.
96df26cacSmrg#
106df26cacSmrg# Copyright (C) 1994 X Consortium
116df26cacSmrg#
126df26cacSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136df26cacSmrg# of this software and associated documentation files (the "Software"), to
146df26cacSmrg# deal in the Software without restriction, including without limitation the
156df26cacSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166df26cacSmrg# sell copies of the Software, and to permit persons to whom the Software is
176df26cacSmrg# furnished to do so, subject to the following conditions:
186df26cacSmrg#
196df26cacSmrg# The above copyright notice and this permission notice shall be included in
206df26cacSmrg# all copies or substantial portions of the Software.
216df26cacSmrg#
226df26cacSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236df26cacSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246df26cacSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256df26cacSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266df26cacSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276df26cacSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286df26cacSmrg#
296df26cacSmrg# Except as contained in this notice, the name of the X Consortium shall not
306df26cacSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
316df26cacSmrg# ings in this Software without prior written authorization from the X Consor-
326df26cacSmrg# tium.
336df26cacSmrg#
346df26cacSmrg#
356df26cacSmrg# FSF changes to this file are in the public domain.
366df26cacSmrg#
376df26cacSmrg# Calling this script install-sh is preferred over install.sh, to prevent
386df26cacSmrg# `make' implicit rules from creating a file called install from it
396df26cacSmrg# when there is no Makefile.
406df26cacSmrg#
416df26cacSmrg# This script is compatible with the BSD install script, but was written
426df26cacSmrg# from scratch.
436df26cacSmrg
446df26cacSmrgnl='
456df26cacSmrg'
466df26cacSmrgIFS=" ""	$nl"
476df26cacSmrg
486df26cacSmrg# set DOITPROG to echo to test this script
496df26cacSmrg
506df26cacSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
516df26cacSmrgdoit="${DOITPROG-}"
526df26cacSmrgif test -z "$doit"; then
536df26cacSmrg  doit_exec=exec
546df26cacSmrgelse
556df26cacSmrg  doit_exec=$doit
566df26cacSmrgfi
576df26cacSmrg
586df26cacSmrg# Put in absolute file names if you don't have them in your path;
596df26cacSmrg# or use environment vars.
606df26cacSmrg
616df26cacSmrgmvprog="${MVPROG-mv}"
626df26cacSmrgcpprog="${CPPROG-cp}"
636df26cacSmrgchmodprog="${CHMODPROG-chmod}"
646df26cacSmrgchownprog="${CHOWNPROG-chown}"
656df26cacSmrgchgrpprog="${CHGRPPROG-chgrp}"
666df26cacSmrgstripprog="${STRIPPROG-strip}"
676df26cacSmrgrmprog="${RMPROG-rm}"
686df26cacSmrgmkdirprog="${MKDIRPROG-mkdir}"
696df26cacSmrg
706df26cacSmrgposix_glob=
716df26cacSmrgposix_mkdir=
726df26cacSmrg
736df26cacSmrg# Desired mode of installed file.
746df26cacSmrgmode=0755
756df26cacSmrg
766df26cacSmrgchmodcmd=$chmodprog
776df26cacSmrgchowncmd=
786df26cacSmrgchgrpcmd=
796df26cacSmrgstripcmd=
806df26cacSmrgrmcmd="$rmprog -f"
816df26cacSmrgmvcmd="$mvprog"
826df26cacSmrgsrc=
836df26cacSmrgdst=
846df26cacSmrgdir_arg=
856df26cacSmrgdstarg=
866df26cacSmrgno_target_directory=
876df26cacSmrg
886df26cacSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
896df26cacSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
906df26cacSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
916df26cacSmrg   or: $0 [OPTION]... -d DIRECTORIES...
926df26cacSmrg
936df26cacSmrgIn the 1st form, copy SRCFILE to DSTFILE.
946df26cacSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
956df26cacSmrgIn the 4th, create DIRECTORIES.
966df26cacSmrg
976df26cacSmrgOptions:
986df26cacSmrg-c         (ignored)
996df26cacSmrg-d         create directories instead of installing files.
1006df26cacSmrg-g GROUP   $chgrpprog installed files to GROUP.
1016df26cacSmrg-m MODE    $chmodprog installed files to MODE.
1026df26cacSmrg-o USER    $chownprog installed files to USER.
1036df26cacSmrg-s         $stripprog installed files.
1046df26cacSmrg-t DIRECTORY  install into DIRECTORY.
1056df26cacSmrg-T         report an error if DSTFILE is a directory.
1066df26cacSmrg--help     display this help and exit.
1076df26cacSmrg--version  display version info and exit.
1086df26cacSmrg
1096df26cacSmrgEnvironment variables override the default commands:
1106df26cacSmrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
1116df26cacSmrg"
1126df26cacSmrg
1136df26cacSmrgwhile test $# -ne 0; do
1146df26cacSmrg  case $1 in
1156df26cacSmrg    -c) shift
1166df26cacSmrg        continue;;
1176df26cacSmrg
1186df26cacSmrg    -d) dir_arg=true
1196df26cacSmrg        shift
1206df26cacSmrg        continue;;
1216df26cacSmrg
1226df26cacSmrg    -g) chgrpcmd="$chgrpprog $2"
1236df26cacSmrg        shift
1246df26cacSmrg        shift
1256df26cacSmrg        continue;;
1266df26cacSmrg
1276df26cacSmrg    --help) echo "$usage"; exit $?;;
1286df26cacSmrg
1296df26cacSmrg    -m) mode=$2
1306df26cacSmrg        shift
1316df26cacSmrg        shift
1326df26cacSmrg	case $mode in
1336df26cacSmrg	  *' '* | *'	'* | *'
1346df26cacSmrg'*	  | *'*'* | *'?'* | *'['*)
1356df26cacSmrg	    echo "$0: invalid mode: $mode" >&2
1366df26cacSmrg	    exit 1;;
1376df26cacSmrg	esac
1386df26cacSmrg        continue;;
1396df26cacSmrg
1406df26cacSmrg    -o) chowncmd="$chownprog $2"
1416df26cacSmrg        shift
1426df26cacSmrg        shift
1436df26cacSmrg        continue;;
1446df26cacSmrg
1456df26cacSmrg    -s) stripcmd=$stripprog
1466df26cacSmrg        shift
1476df26cacSmrg        continue;;
1486df26cacSmrg
1496df26cacSmrg    -t) dstarg=$2
1506df26cacSmrg	shift
1516df26cacSmrg	shift
1526df26cacSmrg	continue;;
1536df26cacSmrg
1546df26cacSmrg    -T) no_target_directory=true
1556df26cacSmrg	shift
1566df26cacSmrg	continue;;
1576df26cacSmrg
1586df26cacSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1596df26cacSmrg
1606df26cacSmrg    --)	shift
1616df26cacSmrg	break;;
1626df26cacSmrg
1636df26cacSmrg    -*)	echo "$0: invalid option: $1" >&2
1646df26cacSmrg	exit 1;;
1656df26cacSmrg
1666df26cacSmrg    *)  break;;
1676df26cacSmrg  esac
1686df26cacSmrgdone
1696df26cacSmrg
1706df26cacSmrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
1716df26cacSmrg  # When -d is used, all remaining arguments are directories to create.
1726df26cacSmrg  # When -t is used, the destination is already specified.
1736df26cacSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1746df26cacSmrg  for arg
1756df26cacSmrg  do
1766df26cacSmrg    if test -n "$dstarg"; then
1776df26cacSmrg      # $@ is not empty: it contains at least $arg.
1786df26cacSmrg      set fnord "$@" "$dstarg"
1796df26cacSmrg      shift # fnord
1806df26cacSmrg    fi
1816df26cacSmrg    shift # arg
1826df26cacSmrg    dstarg=$arg
1836df26cacSmrg  done
1846df26cacSmrgfi
1856df26cacSmrg
1866df26cacSmrgif test $# -eq 0; then
1876df26cacSmrg  if test -z "$dir_arg"; then
1886df26cacSmrg    echo "$0: no input file specified." >&2
1896df26cacSmrg    exit 1
1906df26cacSmrg  fi
1916df26cacSmrg  # It's OK to call `install-sh -d' without argument.
1926df26cacSmrg  # This can happen when creating conditional directories.
1936df26cacSmrg  exit 0
1946df26cacSmrgfi
1956df26cacSmrg
1966df26cacSmrgif test -z "$dir_arg"; then
1976df26cacSmrg  trap '(exit $?); exit' 1 2 13 15
1986df26cacSmrg
1996df26cacSmrg  # Set umask so as not to create temps with too-generous modes.
2006df26cacSmrg  # However, 'strip' requires both read and write access to temps.
2016df26cacSmrg  case $mode in
2026df26cacSmrg    # Optimize common cases.
2036df26cacSmrg    *644) cp_umask=133;;
2046df26cacSmrg    *755) cp_umask=22;;
2056df26cacSmrg
2066df26cacSmrg    *[0-7])
2076df26cacSmrg      if test -z "$stripcmd"; then
2086df26cacSmrg	u_plus_rw=
2096df26cacSmrg      else
2106df26cacSmrg	u_plus_rw='% 200'
2116df26cacSmrg      fi
2126df26cacSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2136df26cacSmrg    *)
2146df26cacSmrg      if test -z "$stripcmd"; then
2156df26cacSmrg	u_plus_rw=
2166df26cacSmrg      else
2176df26cacSmrg	u_plus_rw=,u+rw
2186df26cacSmrg      fi
2196df26cacSmrg      cp_umask=$mode$u_plus_rw;;
2206df26cacSmrg  esac
2216df26cacSmrgfi
2226df26cacSmrg
2236df26cacSmrgfor src
2246df26cacSmrgdo
2256df26cacSmrg  # Protect names starting with `-'.
2266df26cacSmrg  case $src in
2276df26cacSmrg    -*) src=./$src ;;
2286df26cacSmrg  esac
2296df26cacSmrg
2306df26cacSmrg  if test -n "$dir_arg"; then
2316df26cacSmrg    dst=$src
2326df26cacSmrg    dstdir=$dst
2336df26cacSmrg    test -d "$dstdir"
2346df26cacSmrg    dstdir_status=$?
2356df26cacSmrg  else
2366df26cacSmrg
2376df26cacSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2386df26cacSmrg    # might cause directories to be created, which would be especially bad
2396df26cacSmrg    # if $src (and thus $dsttmp) contains '*'.
2406df26cacSmrg    if test ! -f "$src" && test ! -d "$src"; then
2416df26cacSmrg      echo "$0: $src does not exist." >&2
2426df26cacSmrg      exit 1
2436df26cacSmrg    fi
2446df26cacSmrg
2456df26cacSmrg    if test -z "$dstarg"; then
2466df26cacSmrg      echo "$0: no destination specified." >&2
2476df26cacSmrg      exit 1
2486df26cacSmrg    fi
2496df26cacSmrg
2506df26cacSmrg    dst=$dstarg
2516df26cacSmrg    # Protect names starting with `-'.
2526df26cacSmrg    case $dst in
2536df26cacSmrg      -*) dst=./$dst ;;
2546df26cacSmrg    esac
2556df26cacSmrg
2566df26cacSmrg    # If destination is a directory, append the input filename; won't work
2576df26cacSmrg    # if double slashes aren't ignored.
2586df26cacSmrg    if test -d "$dst"; then
2596df26cacSmrg      if test -n "$no_target_directory"; then
2606df26cacSmrg	echo "$0: $dstarg: Is a directory" >&2
2616df26cacSmrg	exit 1
2626df26cacSmrg      fi
2636df26cacSmrg      dstdir=$dst
2646df26cacSmrg      dst=$dstdir/`basename "$src"`
2656df26cacSmrg      dstdir_status=0
2666df26cacSmrg    else
2676df26cacSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2686df26cacSmrg      dstdir=`
2696df26cacSmrg	(dirname "$dst") 2>/dev/null ||
2706df26cacSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2716df26cacSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2726df26cacSmrg	     X"$dst" : 'X\(//\)$' \| \
2736df26cacSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2746df26cacSmrg	echo X"$dst" |
2756df26cacSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2766df26cacSmrg		   s//\1/
2776df26cacSmrg		   q
2786df26cacSmrg		 }
2796df26cacSmrg		 /^X\(\/\/\)[^/].*/{
2806df26cacSmrg		   s//\1/
2816df26cacSmrg		   q
2826df26cacSmrg		 }
2836df26cacSmrg		 /^X\(\/\/\)$/{
2846df26cacSmrg		   s//\1/
2856df26cacSmrg		   q
2866df26cacSmrg		 }
2876df26cacSmrg		 /^X\(\/\).*/{
2886df26cacSmrg		   s//\1/
2896df26cacSmrg		   q
2906df26cacSmrg		 }
2916df26cacSmrg		 s/.*/./; q'
2926df26cacSmrg      `
2936df26cacSmrg
2946df26cacSmrg      test -d "$dstdir"
2956df26cacSmrg      dstdir_status=$?
2966df26cacSmrg    fi
2976df26cacSmrg  fi
2986df26cacSmrg
2996df26cacSmrg  obsolete_mkdir_used=false
3006df26cacSmrg
3016df26cacSmrg  if test $dstdir_status != 0; then
3026df26cacSmrg    case $posix_mkdir in
3036df26cacSmrg      '')
3046df26cacSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3056df26cacSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3066df26cacSmrg	umask=`umask`
3076df26cacSmrg	case $stripcmd.$umask in
3086df26cacSmrg	  # Optimize common cases.
3096df26cacSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3106df26cacSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3116df26cacSmrg
3126df26cacSmrg	  *[0-7])
3136df26cacSmrg	    mkdir_umask=`expr $umask + 22 \
3146df26cacSmrg	      - $umask % 100 % 40 + $umask % 20 \
3156df26cacSmrg	      - $umask % 10 % 4 + $umask % 2
3166df26cacSmrg	    `;;
3176df26cacSmrg	  *) mkdir_umask=$umask,go-w;;
3186df26cacSmrg	esac
3196df26cacSmrg
3206df26cacSmrg	# With -d, create the new directory with the user-specified mode.
3216df26cacSmrg	# Otherwise, rely on $mkdir_umask.
3226df26cacSmrg	if test -n "$dir_arg"; then
3236df26cacSmrg	  mkdir_mode=-m$mode
3246df26cacSmrg	else
3256df26cacSmrg	  mkdir_mode=
3266df26cacSmrg	fi
3276df26cacSmrg
3286df26cacSmrg	posix_mkdir=false
3296df26cacSmrg	case $umask in
3306df26cacSmrg	  *[123567][0-7][0-7])
3316df26cacSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3326df26cacSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3336df26cacSmrg	    ;;
3346df26cacSmrg	  *)
3356df26cacSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3366df26cacSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3376df26cacSmrg
3386df26cacSmrg	    if (umask $mkdir_umask &&
3396df26cacSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3406df26cacSmrg	    then
3416df26cacSmrg	      if test -z "$dir_arg" || {
3426df26cacSmrg		   # Check for POSIX incompatibilities with -m.
3436df26cacSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3446df26cacSmrg		   # other-writeable bit of parent directory when it shouldn't.
3456df26cacSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3466df26cacSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3476df26cacSmrg		   case $ls_ld_tmpdir in
3486df26cacSmrg		     d????-?r-*) different_mode=700;;
3496df26cacSmrg		     d????-?--*) different_mode=755;;
3506df26cacSmrg		     *) false;;
3516df26cacSmrg		   esac &&
3526df26cacSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3536df26cacSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3546df26cacSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3556df26cacSmrg		   }
3566df26cacSmrg		 }
3576df26cacSmrg	      then posix_mkdir=:
3586df26cacSmrg	      fi
3596df26cacSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3606df26cacSmrg	    else
3616df26cacSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3626df26cacSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3636df26cacSmrg	    fi
3646df26cacSmrg	    trap '' 0;;
3656df26cacSmrg	esac;;
3666df26cacSmrg    esac
3676df26cacSmrg
3686df26cacSmrg    if
3696df26cacSmrg      $posix_mkdir && (
3706df26cacSmrg	umask $mkdir_umask &&
3716df26cacSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3726df26cacSmrg      )
3736df26cacSmrg    then :
3746df26cacSmrg    else
3756df26cacSmrg
3766df26cacSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3776df26cacSmrg      # or it failed possibly due to a race condition.  Create the
3786df26cacSmrg      # directory the slow way, step by step, checking for races as we go.
3796df26cacSmrg
3806df26cacSmrg      case $dstdir in
3816df26cacSmrg	/*) prefix=/ ;;
3826df26cacSmrg	-*) prefix=./ ;;
3836df26cacSmrg	*)  prefix= ;;
3846df26cacSmrg      esac
3856df26cacSmrg
3866df26cacSmrg      case $posix_glob in
3876df26cacSmrg        '')
3886df26cacSmrg	  if (set -f) 2>/dev/null; then
3896df26cacSmrg	    posix_glob=true
3906df26cacSmrg	  else
3916df26cacSmrg	    posix_glob=false
3926df26cacSmrg	  fi ;;
3936df26cacSmrg      esac
3946df26cacSmrg
3956df26cacSmrg      oIFS=$IFS
3966df26cacSmrg      IFS=/
3976df26cacSmrg      $posix_glob && set -f
3986df26cacSmrg      set fnord $dstdir
3996df26cacSmrg      shift
4006df26cacSmrg      $posix_glob && set +f
4016df26cacSmrg      IFS=$oIFS
4026df26cacSmrg
4036df26cacSmrg      prefixes=
4046df26cacSmrg
4056df26cacSmrg      for d
4066df26cacSmrg      do
4076df26cacSmrg	test -z "$d" && continue
4086df26cacSmrg
4096df26cacSmrg	prefix=$prefix$d
4106df26cacSmrg	if test -d "$prefix"; then
4116df26cacSmrg	  prefixes=
4126df26cacSmrg	else
4136df26cacSmrg	  if $posix_mkdir; then
4146df26cacSmrg	    (umask=$mkdir_umask &&
4156df26cacSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4166df26cacSmrg	    # Don't fail if two instances are running concurrently.
4176df26cacSmrg	    test -d "$prefix" || exit 1
4186df26cacSmrg	  else
4196df26cacSmrg	    case $prefix in
4206df26cacSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4216df26cacSmrg	      *) qprefix=$prefix;;
4226df26cacSmrg	    esac
4236df26cacSmrg	    prefixes="$prefixes '$qprefix'"
4246df26cacSmrg	  fi
4256df26cacSmrg	fi
4266df26cacSmrg	prefix=$prefix/
4276df26cacSmrg      done
4286df26cacSmrg
4296df26cacSmrg      if test -n "$prefixes"; then
4306df26cacSmrg	# Don't fail if two instances are running concurrently.
4316df26cacSmrg	(umask $mkdir_umask &&
4326df26cacSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4336df26cacSmrg	  test -d "$dstdir" || exit 1
4346df26cacSmrg	obsolete_mkdir_used=true
4356df26cacSmrg      fi
4366df26cacSmrg    fi
4376df26cacSmrg  fi
4386df26cacSmrg
4396df26cacSmrg  if test -n "$dir_arg"; then
4406df26cacSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4416df26cacSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4426df26cacSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4436df26cacSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4446df26cacSmrg  else
4456df26cacSmrg
4466df26cacSmrg    # Make a couple of temp file names in the proper directory.
4476df26cacSmrg    dsttmp=$dstdir/_inst.$$_
4486df26cacSmrg    rmtmp=$dstdir/_rm.$$_
4496df26cacSmrg
4506df26cacSmrg    # Trap to clean up those temp files at exit.
4516df26cacSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4526df26cacSmrg
4536df26cacSmrg    # Copy the file name to the temp name.
4546df26cacSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4556df26cacSmrg
4566df26cacSmrg    # and set any options; do chmod last to preserve setuid bits.
4576df26cacSmrg    #
4586df26cacSmrg    # If any of these fail, we abort the whole thing.  If we want to
4596df26cacSmrg    # ignore errors from any of these, just make sure not to ignore
4606df26cacSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4616df26cacSmrg    #
4626df26cacSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
4636df26cacSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
4646df26cacSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
4656df26cacSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4666df26cacSmrg
4676df26cacSmrg    # Now rename the file to the real destination.
4686df26cacSmrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
4696df26cacSmrg      || {
4706df26cacSmrg	   # The rename failed, perhaps because mv can't rename something else
4716df26cacSmrg	   # to itself, or perhaps because mv is so ancient that it does not
4726df26cacSmrg	   # support -f.
4736df26cacSmrg
4746df26cacSmrg	   # Now remove or move aside any old file at destination location.
4756df26cacSmrg	   # We try this two ways since rm can't unlink itself on some
4766df26cacSmrg	   # systems and the destination file might be busy for other
4776df26cacSmrg	   # reasons.  In this case, the final cleanup might fail but the new
4786df26cacSmrg	   # file should still install successfully.
4796df26cacSmrg	   {
4806df26cacSmrg	     if test -f "$dst"; then
4816df26cacSmrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
4826df26cacSmrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
4836df26cacSmrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
4846df26cacSmrg	       || {
4856df26cacSmrg		 echo "$0: cannot unlink or rename $dst" >&2
4866df26cacSmrg		 (exit 1); exit 1
4876df26cacSmrg	       }
4886df26cacSmrg	     else
4896df26cacSmrg	       :
4906df26cacSmrg	     fi
4916df26cacSmrg	   } &&
4926df26cacSmrg
4936df26cacSmrg	   # Now rename the file to the real destination.
4946df26cacSmrg	   $doit $mvcmd "$dsttmp" "$dst"
4956df26cacSmrg	 }
4966df26cacSmrg    } || exit 1
4976df26cacSmrg
4986df26cacSmrg    trap '' 0
4996df26cacSmrg  fi
5006df26cacSmrgdone
5016df26cacSmrg
5026df26cacSmrg# Local variables:
5036df26cacSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5046df26cacSmrg# time-stamp-start: "scriptversion="
5056df26cacSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5066df26cacSmrg# time-stamp-end: "$"
5076df26cacSmrg# End:
508