17a84e134Smrg#!/bin/sh
27a84e134Smrg# install - install a program, script, or datafile
37a84e134Smrg
45b16253fSmrgscriptversion=2020-11-14.01; # UTC
57a84e134Smrg
67a84e134Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a84e134Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a84e134Smrg# following copyright and license.
97a84e134Smrg#
107a84e134Smrg# Copyright (C) 1994 X Consortium
117a84e134Smrg#
127a84e134Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a84e134Smrg# of this software and associated documentation files (the "Software"), to
147a84e134Smrg# deal in the Software without restriction, including without limitation the
157a84e134Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a84e134Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a84e134Smrg# furnished to do so, subject to the following conditions:
187a84e134Smrg#
197a84e134Smrg# The above copyright notice and this permission notice shall be included in
207a84e134Smrg# all copies or substantial portions of the Software.
217a84e134Smrg#
227a84e134Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a84e134Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a84e134Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a84e134Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a84e134Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a84e134Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a84e134Smrg#
297a84e134Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a84e134Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a84e134Smrg# ings in this Software without prior written authorization from the X Consor-
327a84e134Smrg# tium.
337a84e134Smrg#
347a84e134Smrg#
357a84e134Smrg# FSF changes to this file are in the public domain.
367a84e134Smrg#
377a84e134Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c889a3bfSmrg# 'make' implicit rules from creating a file called install from it
397a84e134Smrg# when there is no Makefile.
407a84e134Smrg#
417a84e134Smrg# This script is compatible with the BSD install script, but was written
427a84e134Smrg# from scratch.
437a84e134Smrg
44c8571806Smrgtab='	'
457a84e134Smrgnl='
467a84e134Smrg'
47c8571806SmrgIFS=" $tab$nl"
487a84e134Smrg
49c8571806Smrg# Set DOITPROG to "echo" to test this script.
507a84e134Smrg
51ab902922Smrgdoit=${DOITPROG-}
52c8571806Smrgdoit_exec=${doit:-exec}
537a84e134Smrg
547a84e134Smrg# Put in absolute file names if you don't have them in your path;
557a84e134Smrg# or use environment vars.
567a84e134Smrg
57ab902922Smrgchgrpprog=${CHGRPPROG-chgrp}
58ab902922Smrgchmodprog=${CHMODPROG-chmod}
59ab902922Smrgchownprog=${CHOWNPROG-chown}
60ab902922Smrgcmpprog=${CMPPROG-cmp}
61ab902922Smrgcpprog=${CPPROG-cp}
62ab902922Smrgmkdirprog=${MKDIRPROG-mkdir}
63ab902922Smrgmvprog=${MVPROG-mv}
64ab902922Smrgrmprog=${RMPROG-rm}
65ab902922Smrgstripprog=${STRIPPROG-strip}
66ab902922Smrg
677a84e134Smrgposix_mkdir=
687a84e134Smrg
697a84e134Smrg# Desired mode of installed file.
707a84e134Smrgmode=0755
717a84e134Smrg
725b16253fSmrg# Create dirs (including intermediate dirs) using mode 755.
735b16253fSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
745b16253fSmrgmkdir_umask=22
755b16253fSmrg
765b16253fSmrgbackupsuffix=
77ab902922Smrgchgrpcmd=
787a84e134Smrgchmodcmd=$chmodprog
797a84e134Smrgchowncmd=
80ab902922Smrgmvcmd=$mvprog
817a84e134Smrgrmcmd="$rmprog -f"
82ab902922Smrgstripcmd=
83ab902922Smrg
847a84e134Smrgsrc=
857a84e134Smrgdst=
867a84e134Smrgdir_arg=
87ab902922Smrgdst_arg=
88ab902922Smrg
89ab902922Smrgcopy_on_change=false
90c8571806Smrgis_target_a_directory=possibly
917a84e134Smrg
92ab902922Smrgusage="\
93ab902922SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
947a84e134Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
957a84e134Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
967a84e134Smrg   or: $0 [OPTION]... -d DIRECTORIES...
977a84e134Smrg
987a84e134SmrgIn the 1st form, copy SRCFILE to DSTFILE.
997a84e134SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1007a84e134SmrgIn the 4th, create DIRECTORIES.
1017a84e134Smrg
1027a84e134SmrgOptions:
103ab902922Smrg     --help     display this help and exit.
104ab902922Smrg     --version  display version info and exit.
105ab902922Smrg
106ab902922Smrg  -c            (ignored)
1075b16253fSmrg  -C            install only if different (preserve data modification time)
108ab902922Smrg  -d            create directories instead of installing files.
109ab902922Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110ab902922Smrg  -m MODE       $chmodprog installed files to MODE.
111ab902922Smrg  -o USER       $chownprog installed files to USER.
1125b16253fSmrg  -p            pass -p to $cpprog.
113ab902922Smrg  -s            $stripprog installed files.
1145b16253fSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115ab902922Smrg  -t DIRECTORY  install into DIRECTORY.
116ab902922Smrg  -T            report an error if DSTFILE is a directory.
1177a84e134Smrg
1187a84e134SmrgEnvironment variables override the default commands:
119ab902922Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120ab902922Smrg  RMPROG STRIPPROG
1215b16253fSmrg
1225b16253fSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1235b16253fSmrgit's up to you to specify -f if you want it.
1245b16253fSmrg
1255b16253fSmrgIf -S is not specified, no backups are attempted.
1265b16253fSmrg
1275b16253fSmrgEmail bug reports to bug-automake@gnu.org.
1285b16253fSmrgAutomake home page: https://www.gnu.org/software/automake/
1297a84e134Smrg"
1307a84e134Smrg
1317a84e134Smrgwhile test $# -ne 0; do
1327a84e134Smrg  case $1 in
133ab902922Smrg    -c) ;;
134ab902922Smrg
135ab902922Smrg    -C) copy_on_change=true;;
1367a84e134Smrg
137ab902922Smrg    -d) dir_arg=true;;
1387a84e134Smrg
1397a84e134Smrg    -g) chgrpcmd="$chgrpprog $2"
140c8571806Smrg        shift;;
1417a84e134Smrg
1427a84e134Smrg    --help) echo "$usage"; exit $?;;
1437a84e134Smrg
1447a84e134Smrg    -m) mode=$2
145c8571806Smrg        case $mode in
146c8571806Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147c8571806Smrg            echo "$0: invalid mode: $mode" >&2
148c8571806Smrg            exit 1;;
149c8571806Smrg        esac
150c8571806Smrg        shift;;
1517a84e134Smrg
1527a84e134Smrg    -o) chowncmd="$chownprog $2"
153c8571806Smrg        shift;;
1547a84e134Smrg
1555b16253fSmrg    -p) cpprog="$cpprog -p";;
1565b16253fSmrg
157ab902922Smrg    -s) stripcmd=$stripprog;;
1587a84e134Smrg
1595b16253fSmrg    -S) backupsuffix="$2"
1605b16253fSmrg        shift;;
1615b16253fSmrg
162c8571806Smrg    -t)
163c8571806Smrg        is_target_a_directory=always
164c8571806Smrg        dst_arg=$2
165c8571806Smrg        # Protect names problematic for 'test' and other utilities.
166c8571806Smrg        case $dst_arg in
167c8571806Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168c8571806Smrg        esac
169c8571806Smrg        shift;;
1707a84e134Smrg
171c8571806Smrg    -T) is_target_a_directory=never;;
1727a84e134Smrg
1737a84e134Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1747a84e134Smrg
175c8571806Smrg    --) shift
176c8571806Smrg        break;;
1777a84e134Smrg
178c8571806Smrg    -*) echo "$0: invalid option: $1" >&2
179c8571806Smrg        exit 1;;
1807a84e134Smrg
1817a84e134Smrg    *)  break;;
1827a84e134Smrg  esac
183ab902922Smrg  shift
1847a84e134Smrgdone
1857a84e134Smrg
186c8571806Smrg# We allow the use of options -d and -T together, by making -d
187c8571806Smrg# take the precedence; this is for compatibility with GNU install.
188c8571806Smrg
189c8571806Smrgif test -n "$dir_arg"; then
190c8571806Smrg  if test -n "$dst_arg"; then
191c8571806Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192c8571806Smrg    exit 1
193c8571806Smrg  fi
194c8571806Smrgfi
195c8571806Smrg
196ab902922Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1977a84e134Smrg  # When -d is used, all remaining arguments are directories to create.
1987a84e134Smrg  # When -t is used, the destination is already specified.
1997a84e134Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2007a84e134Smrg  for arg
2017a84e134Smrg  do
202ab902922Smrg    if test -n "$dst_arg"; then
2037a84e134Smrg      # $@ is not empty: it contains at least $arg.
204ab902922Smrg      set fnord "$@" "$dst_arg"
2057a84e134Smrg      shift # fnord
2067a84e134Smrg    fi
2077a84e134Smrg    shift # arg
208ab902922Smrg    dst_arg=$arg
209c889a3bfSmrg    # Protect names problematic for 'test' and other utilities.
210421c997bSmrg    case $dst_arg in
211421c997bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212421c997bSmrg    esac
2137a84e134Smrg  done
2147a84e134Smrgfi
2157a84e134Smrg
2167a84e134Smrgif test $# -eq 0; then
2177a84e134Smrg  if test -z "$dir_arg"; then
2187a84e134Smrg    echo "$0: no input file specified." >&2
2197a84e134Smrg    exit 1
2207a84e134Smrg  fi
221c889a3bfSmrg  # It's OK to call 'install-sh -d' without argument.
2227a84e134Smrg  # This can happen when creating conditional directories.
2237a84e134Smrg  exit 0
2247a84e134Smrgfi
2257a84e134Smrg
226c8571806Smrgif test -z "$dir_arg"; then
227c8571806Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228c8571806Smrg    if test ! -d "$dst_arg"; then
229c8571806Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230c8571806Smrg      exit 1
231c8571806Smrg    fi
232c8571806Smrg  fi
233c8571806Smrgfi
234c8571806Smrg
2357a84e134Smrgif test -z "$dir_arg"; then
236421c997bSmrg  do_exit='(exit $ret); exit $ret'
237421c997bSmrg  trap "ret=129; $do_exit" 1
238421c997bSmrg  trap "ret=130; $do_exit" 2
239421c997bSmrg  trap "ret=141; $do_exit" 13
240421c997bSmrg  trap "ret=143; $do_exit" 15
2417a84e134Smrg
2427a84e134Smrg  # Set umask so as not to create temps with too-generous modes.
2437a84e134Smrg  # However, 'strip' requires both read and write access to temps.
2447a84e134Smrg  case $mode in
2457a84e134Smrg    # Optimize common cases.
2467a84e134Smrg    *644) cp_umask=133;;
2477a84e134Smrg    *755) cp_umask=22;;
2487a84e134Smrg
2497a84e134Smrg    *[0-7])
2507a84e134Smrg      if test -z "$stripcmd"; then
251c8571806Smrg        u_plus_rw=
2527a84e134Smrg      else
253c8571806Smrg        u_plus_rw='% 200'
2547a84e134Smrg      fi
2557a84e134Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2567a84e134Smrg    *)
2577a84e134Smrg      if test -z "$stripcmd"; then
258c8571806Smrg        u_plus_rw=
2597a84e134Smrg      else
260c8571806Smrg        u_plus_rw=,u+rw
2617a84e134Smrg      fi
2627a84e134Smrg      cp_umask=$mode$u_plus_rw;;
2637a84e134Smrg  esac
2647a84e134Smrgfi
2657a84e134Smrg
2667a84e134Smrgfor src
2677a84e134Smrgdo
268c889a3bfSmrg  # Protect names problematic for 'test' and other utilities.
2697a84e134Smrg  case $src in
270421c997bSmrg    -* | [=\(\)!]) src=./$src;;
2717a84e134Smrg  esac
2727a84e134Smrg
2737a84e134Smrg  if test -n "$dir_arg"; then
2747a84e134Smrg    dst=$src
2757a84e134Smrg    dstdir=$dst
2767a84e134Smrg    test -d "$dstdir"
2777a84e134Smrg    dstdir_status=$?
2785b16253fSmrg    # Don't chown directories that already exist.
2795b16253fSmrg    if test $dstdir_status = 0; then
2805b16253fSmrg      chowncmd=""
2815b16253fSmrg    fi
2827a84e134Smrg  else
2837a84e134Smrg
2847a84e134Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2857a84e134Smrg    # might cause directories to be created, which would be especially bad
2867a84e134Smrg    # if $src (and thus $dsttmp) contains '*'.
2877a84e134Smrg    if test ! -f "$src" && test ! -d "$src"; then
2887a84e134Smrg      echo "$0: $src does not exist." >&2
2897a84e134Smrg      exit 1
2907a84e134Smrg    fi
2917a84e134Smrg
292ab902922Smrg    if test -z "$dst_arg"; then
2937a84e134Smrg      echo "$0: no destination specified." >&2
2947a84e134Smrg      exit 1
2957a84e134Smrg    fi
296ab902922Smrg    dst=$dst_arg
2977a84e134Smrg
2985ec34c4cSmrg    # If destination is a directory, append the input filename.
2997a84e134Smrg    if test -d "$dst"; then
300c8571806Smrg      if test "$is_target_a_directory" = never; then
301c8571806Smrg        echo "$0: $dst_arg: Is a directory" >&2
302c8571806Smrg        exit 1
3037a84e134Smrg      fi
3047a84e134Smrg      dstdir=$dst
3055ec34c4cSmrg      dstbase=`basename "$src"`
3065ec34c4cSmrg      case $dst in
3075ec34c4cSmrg	*/) dst=$dst$dstbase;;
3085ec34c4cSmrg	*)  dst=$dst/$dstbase;;
3095ec34c4cSmrg      esac
3107a84e134Smrg      dstdir_status=0
3117a84e134Smrg    else
312c8571806Smrg      dstdir=`dirname "$dst"`
3137a84e134Smrg      test -d "$dstdir"
3147a84e134Smrg      dstdir_status=$?
3157a84e134Smrg    fi
3167a84e134Smrg  fi
3177a84e134Smrg
3185ec34c4cSmrg  case $dstdir in
3195ec34c4cSmrg    */) dstdirslash=$dstdir;;
3205ec34c4cSmrg    *)  dstdirslash=$dstdir/;;
3215ec34c4cSmrg  esac
3225ec34c4cSmrg
3237a84e134Smrg  obsolete_mkdir_used=false
3247a84e134Smrg
3257a84e134Smrg  if test $dstdir_status != 0; then
3267a84e134Smrg    case $posix_mkdir in
3277a84e134Smrg      '')
328c8571806Smrg        # With -d, create the new directory with the user-specified mode.
329c8571806Smrg        # Otherwise, rely on $mkdir_umask.
330c8571806Smrg        if test -n "$dir_arg"; then
331c8571806Smrg          mkdir_mode=-m$mode
332c8571806Smrg        else
333c8571806Smrg          mkdir_mode=
334c8571806Smrg        fi
335c8571806Smrg
336c8571806Smrg        posix_mkdir=false
3375b16253fSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3385b16253fSmrg	# here however when possible just to lower collision chance.
3395b16253fSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3405b16253fSmrg
3415b16253fSmrg	trap '
3425b16253fSmrg	  ret=$?
3435b16253fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3445b16253fSmrg	  exit $ret
3455b16253fSmrg	' 0
3465b16253fSmrg
3475b16253fSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3485b16253fSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3495b16253fSmrg	# directory is successfully created first before we actually test
3505b16253fSmrg	# 'mkdir -p'.
3515b16253fSmrg	if (umask $mkdir_umask &&
3525b16253fSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3535b16253fSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3545b16253fSmrg	then
3555b16253fSmrg	  if test -z "$dir_arg" || {
3565b16253fSmrg	       # Check for POSIX incompatibilities with -m.
3575b16253fSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3585b16253fSmrg	       # other-writable bit of parent directory when it shouldn't.
3595b16253fSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3605b16253fSmrg	       test_tmpdir="$tmpdir/a"
3615b16253fSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3625b16253fSmrg	       case $ls_ld_tmpdir in
3635b16253fSmrg		 d????-?r-*) different_mode=700;;
3645b16253fSmrg		 d????-?--*) different_mode=755;;
3655b16253fSmrg		 *) false;;
3665b16253fSmrg	       esac &&
3675b16253fSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3685b16253fSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3695b16253fSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3705b16253fSmrg	       }
3715b16253fSmrg	     }
3725b16253fSmrg	  then posix_mkdir=:
3735b16253fSmrg	  fi
3745b16253fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3755b16253fSmrg	else
3765b16253fSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3775b16253fSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3785b16253fSmrg	fi
3795b16253fSmrg	trap '' 0;;
3807a84e134Smrg    esac
3817a84e134Smrg
3827a84e134Smrg    if
3837a84e134Smrg      $posix_mkdir && (
384c8571806Smrg        umask $mkdir_umask &&
385c8571806Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3867a84e134Smrg      )
3877a84e134Smrg    then :
3887a84e134Smrg    else
3897a84e134Smrg
3905b16253fSmrg      # mkdir does not conform to POSIX,
3917a84e134Smrg      # or it failed possibly due to a race condition.  Create the
3927a84e134Smrg      # directory the slow way, step by step, checking for races as we go.
3937a84e134Smrg
3947a84e134Smrg      case $dstdir in
395c8571806Smrg        /*) prefix='/';;
396c8571806Smrg        [-=\(\)!]*) prefix='./';;
397c8571806Smrg        *)  prefix='';;
3987a84e134Smrg      esac
3997a84e134Smrg
4007a84e134Smrg      oIFS=$IFS
4017a84e134Smrg      IFS=/
402c8571806Smrg      set -f
4037a84e134Smrg      set fnord $dstdir
4047a84e134Smrg      shift
405c8571806Smrg      set +f
4067a84e134Smrg      IFS=$oIFS
4077a84e134Smrg
4087a84e134Smrg      prefixes=
4097a84e134Smrg
4107a84e134Smrg      for d
4117a84e134Smrg      do
412c8571806Smrg        test X"$d" = X && continue
413c8571806Smrg
414c8571806Smrg        prefix=$prefix$d
415c8571806Smrg        if test -d "$prefix"; then
416c8571806Smrg          prefixes=
417c8571806Smrg        else
418c8571806Smrg          if $posix_mkdir; then
4195b16253fSmrg            (umask $mkdir_umask &&
420c8571806Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421c8571806Smrg            # Don't fail if two instances are running concurrently.
422c8571806Smrg            test -d "$prefix" || exit 1
423c8571806Smrg          else
424c8571806Smrg            case $prefix in
425c8571806Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426c8571806Smrg              *) qprefix=$prefix;;
427c8571806Smrg            esac
428c8571806Smrg            prefixes="$prefixes '$qprefix'"
429c8571806Smrg          fi
430c8571806Smrg        fi
431c8571806Smrg        prefix=$prefix/
4327a84e134Smrg      done
4337a84e134Smrg
4347a84e134Smrg      if test -n "$prefixes"; then
435c8571806Smrg        # Don't fail if two instances are running concurrently.
436c8571806Smrg        (umask $mkdir_umask &&
437c8571806Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438c8571806Smrg          test -d "$dstdir" || exit 1
439c8571806Smrg        obsolete_mkdir_used=true
4407a84e134Smrg      fi
4417a84e134Smrg    fi
4427a84e134Smrg  fi
4437a84e134Smrg
4447a84e134Smrg  if test -n "$dir_arg"; then
4457a84e134Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4467a84e134Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4477a84e134Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4487a84e134Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4497a84e134Smrg  else
4507a84e134Smrg
4517a84e134Smrg    # Make a couple of temp file names in the proper directory.
4525ec34c4cSmrg    dsttmp=${dstdirslash}_inst.$$_
4535ec34c4cSmrg    rmtmp=${dstdirslash}_rm.$$_
4547a84e134Smrg
4557a84e134Smrg    # Trap to clean up those temp files at exit.
4567a84e134Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4577a84e134Smrg
4587a84e134Smrg    # Copy the file name to the temp name.
4595ec34c4cSmrg    (umask $cp_umask &&
4605ec34c4cSmrg     { test -z "$stripcmd" || {
4615ec34c4cSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4625ec34c4cSmrg	 # which would cause strip to fail.
4635ec34c4cSmrg	 if test -z "$doit"; then
4645ec34c4cSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4655ec34c4cSmrg	 else
4665ec34c4cSmrg	   $doit touch "$dsttmp"
4675ec34c4cSmrg	 fi
4685ec34c4cSmrg       }
4695ec34c4cSmrg     } &&
4705ec34c4cSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4717a84e134Smrg
4727a84e134Smrg    # and set any options; do chmod last to preserve setuid bits.
4737a84e134Smrg    #
4747a84e134Smrg    # If any of these fail, we abort the whole thing.  If we want to
4757a84e134Smrg    # ignore errors from any of these, just make sure not to ignore
4767a84e134Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4777a84e134Smrg    #
478ab902922Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479ab902922Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480ab902922Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481ab902922Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482ab902922Smrg
483ab902922Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484ab902922Smrg    if $copy_on_change &&
485c8571806Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486c8571806Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487c8571806Smrg       set -f &&
488ab902922Smrg       set X $old && old=:$2:$4:$5:$6 &&
489ab902922Smrg       set X $new && new=:$2:$4:$5:$6 &&
490c8571806Smrg       set +f &&
491ab902922Smrg       test "$old" = "$new" &&
492ab902922Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493ab902922Smrg    then
494ab902922Smrg      rm -f "$dsttmp"
495ab902922Smrg    else
4965b16253fSmrg      # If $backupsuffix is set, and the file being installed
4975b16253fSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4985b16253fSmrg      # e.g., if mv doesn't support -f.
4995b16253fSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5005b16253fSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5015b16253fSmrg      fi
5025b16253fSmrg
503ab902922Smrg      # Rename the file to the real destination.
504ab902922Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505ab902922Smrg
506ab902922Smrg      # The rename failed, perhaps because mv can't rename something else
507ab902922Smrg      # to itself, or perhaps because mv is so ancient that it does not
508ab902922Smrg      # support -f.
509ab902922Smrg      {
510c8571806Smrg        # Now remove or move aside any old file at destination location.
511c8571806Smrg        # We try this two ways since rm can't unlink itself on some
512c8571806Smrg        # systems and the destination file might be busy for other
513c8571806Smrg        # reasons.  In this case, the final cleanup might fail but the new
514c8571806Smrg        # file should still install successfully.
515c8571806Smrg        {
516c8571806Smrg          test ! -f "$dst" ||
5175b16253fSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518c8571806Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5195b16253fSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520c8571806Smrg          } ||
521c8571806Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522c8571806Smrg            (exit 1); exit 1
523c8571806Smrg          }
524c8571806Smrg        } &&
525c8571806Smrg
526c8571806Smrg        # Now rename the file to the real destination.
527c8571806Smrg        $doit $mvcmd "$dsttmp" "$dst"
528ab902922Smrg      }
529ab902922Smrg    fi || exit 1
5307a84e134Smrg
5317a84e134Smrg    trap '' 0
5327a84e134Smrg  fi
5337a84e134Smrgdone
5347a84e134Smrg
5357a84e134Smrg# Local variables:
5365ec34c4cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5377a84e134Smrg# time-stamp-start: "scriptversion="
5387a84e134Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5395ec34c4cSmrg# time-stamp-time-zone: "UTC0"
540e1e1713cSmrg# time-stamp-end: "; # UTC"
5417a84e134Smrg# End:
542