1100ae103Smrg#!/bin/sh
2100ae103Smrg# install - install a program, script, or datafile
3100ae103Smrg
40103645bSmrgscriptversion=2020-11-14.01; # UTC
5100ae103Smrg
6100ae103Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7100ae103Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8100ae103Smrg# following copyright and license.
9100ae103Smrg#
10100ae103Smrg# Copyright (C) 1994 X Consortium
11100ae103Smrg#
12100ae103Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13100ae103Smrg# of this software and associated documentation files (the "Software"), to
14100ae103Smrg# deal in the Software without restriction, including without limitation the
15100ae103Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16100ae103Smrg# sell copies of the Software, and to permit persons to whom the Software is
17100ae103Smrg# furnished to do so, subject to the following conditions:
18100ae103Smrg#
19100ae103Smrg# The above copyright notice and this permission notice shall be included in
20100ae103Smrg# all copies or substantial portions of the Software.
21100ae103Smrg#
22100ae103Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23100ae103Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24100ae103Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25100ae103Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26100ae103Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27100ae103Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28100ae103Smrg#
29100ae103Smrg# Except as contained in this notice, the name of the X Consortium shall not
30100ae103Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31100ae103Smrg# ings in this Software without prior written authorization from the X Consor-
32100ae103Smrg# tium.
33100ae103Smrg#
34100ae103Smrg#
35100ae103Smrg# FSF changes to this file are in the public domain.
36100ae103Smrg#
37100ae103Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3895cf0c00Smrg# 'make' implicit rules from creating a file called install from it
39100ae103Smrg# when there is no Makefile.
40100ae103Smrg#
41100ae103Smrg# This script is compatible with the BSD install script, but was written
42165cb819Smrg# from scratch.
43165cb819Smrg
4495cf0c00Smrgtab='	'
45165cb819Smrgnl='
46165cb819Smrg'
4795cf0c00SmrgIFS=" $tab$nl"
48100ae103Smrg
4995cf0c00Smrg# Set DOITPROG to "echo" to test this script.
50100ae103Smrg
51165cb819Smrgdoit=${DOITPROG-}
5295cf0c00Smrgdoit_exec=${doit:-exec}
53100ae103Smrg
54165cb819Smrg# Put in absolute file names if you don't have them in your path;
55165cb819Smrg# or use environment vars.
56165cb819Smrg
57165cb819Smrgchgrpprog=${CHGRPPROG-chgrp}
58165cb819Smrgchmodprog=${CHMODPROG-chmod}
59165cb819Smrgchownprog=${CHOWNPROG-chown}
60165cb819Smrgcmpprog=${CMPPROG-cmp}
61165cb819Smrgcpprog=${CPPROG-cp}
62165cb819Smrgmkdirprog=${MKDIRPROG-mkdir}
63165cb819Smrgmvprog=${MVPROG-mv}
64165cb819Smrgrmprog=${RMPROG-rm}
65165cb819Smrgstripprog=${STRIPPROG-strip}
66165cb819Smrg
67165cb819Smrgposix_mkdir=
68165cb819Smrg
69165cb819Smrg# Desired mode of installed file.
70165cb819Smrgmode=0755
71100ae103Smrg
720103645bSmrg# Create dirs (including intermediate dirs) using mode 755.
730103645bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740103645bSmrgmkdir_umask=22
750103645bSmrg
760103645bSmrgbackupsuffix=
77100ae103Smrgchgrpcmd=
78165cb819Smrgchmodcmd=$chmodprog
79165cb819Smrgchowncmd=
80165cb819Smrgmvcmd=$mvprog
81100ae103Smrgrmcmd="$rmprog -f"
82165cb819Smrgstripcmd=
83165cb819Smrg
84100ae103Smrgsrc=
85100ae103Smrgdst=
86100ae103Smrgdir_arg=
87165cb819Smrgdst_arg=
88165cb819Smrg
89165cb819Smrgcopy_on_change=false
9095cf0c00Smrgis_target_a_directory=possibly
91100ae103Smrg
92165cb819Smrgusage="\
93165cb819SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94100ae103Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95100ae103Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96100ae103Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97100ae103Smrg
98100ae103SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99100ae103SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100100ae103SmrgIn the 4th, create DIRECTORIES.
101100ae103Smrg
102100ae103SmrgOptions:
103165cb819Smrg     --help     display this help and exit.
104165cb819Smrg     --version  display version info and exit.
105165cb819Smrg
106165cb819Smrg  -c            (ignored)
1070103645bSmrg  -C            install only if different (preserve data modification time)
108165cb819Smrg  -d            create directories instead of installing files.
109165cb819Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110165cb819Smrg  -m MODE       $chmodprog installed files to MODE.
111165cb819Smrg  -o USER       $chownprog installed files to USER.
1120103645bSmrg  -p            pass -p to $cpprog.
113165cb819Smrg  -s            $stripprog installed files.
1140103645bSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115165cb819Smrg  -t DIRECTORY  install into DIRECTORY.
116165cb819Smrg  -T            report an error if DSTFILE is a directory.
117100ae103Smrg
118100ae103SmrgEnvironment variables override the default commands:
119165cb819Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120165cb819Smrg  RMPROG STRIPPROG
1210103645bSmrg
1220103645bSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230103645bSmrgit's up to you to specify -f if you want it.
1240103645bSmrg
1250103645bSmrgIf -S is not specified, no backups are attempted.
1260103645bSmrg
1270103645bSmrgEmail bug reports to bug-automake@gnu.org.
1280103645bSmrgAutomake home page: https://www.gnu.org/software/automake/
129100ae103Smrg"
130100ae103Smrg
131165cb819Smrgwhile test $# -ne 0; do
132100ae103Smrg  case $1 in
133165cb819Smrg    -c) ;;
134165cb819Smrg
135165cb819Smrg    -C) copy_on_change=true;;
136100ae103Smrg
137165cb819Smrg    -d) dir_arg=true;;
138100ae103Smrg
139100ae103Smrg    -g) chgrpcmd="$chgrpprog $2"
14095cf0c00Smrg        shift;;
141100ae103Smrg
142100ae103Smrg    --help) echo "$usage"; exit $?;;
143100ae103Smrg
144165cb819Smrg    -m) mode=$2
14595cf0c00Smrg        case $mode in
14695cf0c00Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14795cf0c00Smrg            echo "$0: invalid mode: $mode" >&2
14895cf0c00Smrg            exit 1;;
14995cf0c00Smrg        esac
15095cf0c00Smrg        shift;;
151100ae103Smrg
152100ae103Smrg    -o) chowncmd="$chownprog $2"
15395cf0c00Smrg        shift;;
154100ae103Smrg
1550103645bSmrg    -p) cpprog="$cpprog -p";;
1560103645bSmrg
157165cb819Smrg    -s) stripcmd=$stripprog;;
158100ae103Smrg
1590103645bSmrg    -S) backupsuffix="$2"
1600103645bSmrg        shift;;
1610103645bSmrg
16295cf0c00Smrg    -t)
16395cf0c00Smrg        is_target_a_directory=always
16495cf0c00Smrg        dst_arg=$2
16595cf0c00Smrg        # Protect names problematic for 'test' and other utilities.
16695cf0c00Smrg        case $dst_arg in
16795cf0c00Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16895cf0c00Smrg        esac
16995cf0c00Smrg        shift;;
170100ae103Smrg
17195cf0c00Smrg    -T) is_target_a_directory=never;;
172100ae103Smrg
173100ae103Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174100ae103Smrg
17595cf0c00Smrg    --) shift
17695cf0c00Smrg        break;;
177165cb819Smrg
17895cf0c00Smrg    -*) echo "$0: invalid option: $1" >&2
17995cf0c00Smrg        exit 1;;
180165cb819Smrg
181165cb819Smrg    *)  break;;
182100ae103Smrg  esac
183165cb819Smrg  shift
184100ae103Smrgdone
185100ae103Smrg
18695cf0c00Smrg# We allow the use of options -d and -T together, by making -d
18795cf0c00Smrg# take the precedence; this is for compatibility with GNU install.
18895cf0c00Smrg
18995cf0c00Smrgif test -n "$dir_arg"; then
19095cf0c00Smrg  if test -n "$dst_arg"; then
19195cf0c00Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19295cf0c00Smrg    exit 1
19395cf0c00Smrg  fi
19495cf0c00Smrgfi
19595cf0c00Smrg
196165cb819Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197165cb819Smrg  # When -d is used, all remaining arguments are directories to create.
198165cb819Smrg  # When -t is used, the destination is already specified.
199165cb819Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200165cb819Smrg  for arg
201165cb819Smrg  do
202165cb819Smrg    if test -n "$dst_arg"; then
203165cb819Smrg      # $@ is not empty: it contains at least $arg.
204165cb819Smrg      set fnord "$@" "$dst_arg"
205165cb819Smrg      shift # fnord
206165cb819Smrg    fi
207165cb819Smrg    shift # arg
208165cb819Smrg    dst_arg=$arg
20995cf0c00Smrg    # Protect names problematic for 'test' and other utilities.
2108831d3fbSmrg    case $dst_arg in
2118831d3fbSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2128831d3fbSmrg    esac
213165cb819Smrg  done
214165cb819Smrgfi
215165cb819Smrg
216165cb819Smrgif test $# -eq 0; then
217100ae103Smrg  if test -z "$dir_arg"; then
218100ae103Smrg    echo "$0: no input file specified." >&2
219100ae103Smrg    exit 1
220100ae103Smrg  fi
22195cf0c00Smrg  # It's OK to call 'install-sh -d' without argument.
222100ae103Smrg  # This can happen when creating conditional directories.
223100ae103Smrg  exit 0
224100ae103Smrgfi
225100ae103Smrg
22695cf0c00Smrgif test -z "$dir_arg"; then
22795cf0c00Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22895cf0c00Smrg    if test ! -d "$dst_arg"; then
22995cf0c00Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23095cf0c00Smrg      exit 1
23195cf0c00Smrg    fi
23295cf0c00Smrg  fi
23395cf0c00Smrgfi
23495cf0c00Smrg
235165cb819Smrgif test -z "$dir_arg"; then
2368831d3fbSmrg  do_exit='(exit $ret); exit $ret'
2378831d3fbSmrg  trap "ret=129; $do_exit" 1
2388831d3fbSmrg  trap "ret=130; $do_exit" 2
2398831d3fbSmrg  trap "ret=141; $do_exit" 13
2408831d3fbSmrg  trap "ret=143; $do_exit" 15
241165cb819Smrg
242165cb819Smrg  # Set umask so as not to create temps with too-generous modes.
243165cb819Smrg  # However, 'strip' requires both read and write access to temps.
244165cb819Smrg  case $mode in
245165cb819Smrg    # Optimize common cases.
246165cb819Smrg    *644) cp_umask=133;;
247165cb819Smrg    *755) cp_umask=22;;
248165cb819Smrg
249165cb819Smrg    *[0-7])
250165cb819Smrg      if test -z "$stripcmd"; then
25195cf0c00Smrg        u_plus_rw=
252165cb819Smrg      else
25395cf0c00Smrg        u_plus_rw='% 200'
254165cb819Smrg      fi
255165cb819Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256165cb819Smrg    *)
257165cb819Smrg      if test -z "$stripcmd"; then
25895cf0c00Smrg        u_plus_rw=
259165cb819Smrg      else
26095cf0c00Smrg        u_plus_rw=,u+rw
261165cb819Smrg      fi
262165cb819Smrg      cp_umask=$mode$u_plus_rw;;
263165cb819Smrg  esac
264165cb819Smrgfi
265165cb819Smrg
266100ae103Smrgfor src
267100ae103Smrgdo
26895cf0c00Smrg  # Protect names problematic for 'test' and other utilities.
269100ae103Smrg  case $src in
2708831d3fbSmrg    -* | [=\(\)!]) src=./$src;;
271100ae103Smrg  esac
272100ae103Smrg
273100ae103Smrg  if test -n "$dir_arg"; then
274100ae103Smrg    dst=$src
275165cb819Smrg    dstdir=$dst
276165cb819Smrg    test -d "$dstdir"
277165cb819Smrg    dstdir_status=$?
2780103645bSmrg    # Don't chown directories that already exist.
2790103645bSmrg    if test $dstdir_status = 0; then
2800103645bSmrg      chowncmd=""
2810103645bSmrg    fi
282100ae103Smrg  else
283165cb819Smrg
284100ae103Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285100ae103Smrg    # might cause directories to be created, which would be especially bad
286100ae103Smrg    # if $src (and thus $dsttmp) contains '*'.
287100ae103Smrg    if test ! -f "$src" && test ! -d "$src"; then
288100ae103Smrg      echo "$0: $src does not exist." >&2
289100ae103Smrg      exit 1
290100ae103Smrg    fi
291100ae103Smrg
292165cb819Smrg    if test -z "$dst_arg"; then
293100ae103Smrg      echo "$0: no destination specified." >&2
294100ae103Smrg      exit 1
295100ae103Smrg    fi
296165cb819Smrg    dst=$dst_arg
297100ae103Smrg
2980103645bSmrg    # If destination is a directory, append the input filename.
299100ae103Smrg    if test -d "$dst"; then
30095cf0c00Smrg      if test "$is_target_a_directory" = never; then
30195cf0c00Smrg        echo "$0: $dst_arg: Is a directory" >&2
30295cf0c00Smrg        exit 1
303100ae103Smrg      fi
304165cb819Smrg      dstdir=$dst
3050103645bSmrg      dstbase=`basename "$src"`
3060103645bSmrg      case $dst in
3070103645bSmrg	*/) dst=$dst$dstbase;;
3080103645bSmrg	*)  dst=$dst/$dstbase;;
3090103645bSmrg      esac
310165cb819Smrg      dstdir_status=0
311165cb819Smrg    else
31295cf0c00Smrg      dstdir=`dirname "$dst"`
313165cb819Smrg      test -d "$dstdir"
314165cb819Smrg      dstdir_status=$?
315100ae103Smrg    fi
316100ae103Smrg  fi
317100ae103Smrg
3180103645bSmrg  case $dstdir in
3190103645bSmrg    */) dstdirslash=$dstdir;;
3200103645bSmrg    *)  dstdirslash=$dstdir/;;
3210103645bSmrg  esac
3220103645bSmrg
323165cb819Smrg  obsolete_mkdir_used=false
324165cb819Smrg
325165cb819Smrg  if test $dstdir_status != 0; then
326165cb819Smrg    case $posix_mkdir in
327165cb819Smrg      '')
32895cf0c00Smrg        # With -d, create the new directory with the user-specified mode.
32995cf0c00Smrg        # Otherwise, rely on $mkdir_umask.
33095cf0c00Smrg        if test -n "$dir_arg"; then
33195cf0c00Smrg          mkdir_mode=-m$mode
33295cf0c00Smrg        else
33395cf0c00Smrg          mkdir_mode=
33495cf0c00Smrg        fi
33595cf0c00Smrg
33695cf0c00Smrg        posix_mkdir=false
3370103645bSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380103645bSmrg	# here however when possible just to lower collision chance.
3390103645bSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400103645bSmrg
3410103645bSmrg	trap '
3420103645bSmrg	  ret=$?
3430103645bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440103645bSmrg	  exit $ret
3450103645bSmrg	' 0
3460103645bSmrg
3470103645bSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3480103645bSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3490103645bSmrg	# directory is successfully created first before we actually test
3500103645bSmrg	# 'mkdir -p'.
3510103645bSmrg	if (umask $mkdir_umask &&
3520103645bSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530103645bSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540103645bSmrg	then
3550103645bSmrg	  if test -z "$dir_arg" || {
3560103645bSmrg	       # Check for POSIX incompatibilities with -m.
3570103645bSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580103645bSmrg	       # other-writable bit of parent directory when it shouldn't.
3590103645bSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600103645bSmrg	       test_tmpdir="$tmpdir/a"
3610103645bSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620103645bSmrg	       case $ls_ld_tmpdir in
3630103645bSmrg		 d????-?r-*) different_mode=700;;
3640103645bSmrg		 d????-?--*) different_mode=755;;
3650103645bSmrg		 *) false;;
3660103645bSmrg	       esac &&
3670103645bSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680103645bSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690103645bSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700103645bSmrg	       }
3710103645bSmrg	     }
3720103645bSmrg	  then posix_mkdir=:
3730103645bSmrg	  fi
3740103645bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3750103645bSmrg	else
3760103645bSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770103645bSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3780103645bSmrg	fi
3790103645bSmrg	trap '' 0;;
380165cb819Smrg    esac
381100ae103Smrg
382165cb819Smrg    if
383165cb819Smrg      $posix_mkdir && (
38495cf0c00Smrg        umask $mkdir_umask &&
38595cf0c00Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386165cb819Smrg      )
387165cb819Smrg    then :
388165cb819Smrg    else
389100ae103Smrg
3900103645bSmrg      # mkdir does not conform to POSIX,
391165cb819Smrg      # or it failed possibly due to a race condition.  Create the
392165cb819Smrg      # directory the slow way, step by step, checking for races as we go.
393100ae103Smrg
394165cb819Smrg      case $dstdir in
39595cf0c00Smrg        /*) prefix='/';;
39695cf0c00Smrg        [-=\(\)!]*) prefix='./';;
39795cf0c00Smrg        *)  prefix='';;
398165cb819Smrg      esac
399100ae103Smrg
400165cb819Smrg      oIFS=$IFS
401165cb819Smrg      IFS=/
40295cf0c00Smrg      set -f
403165cb819Smrg      set fnord $dstdir
404100ae103Smrg      shift
40595cf0c00Smrg      set +f
406165cb819Smrg      IFS=$oIFS
407165cb819Smrg
408165cb819Smrg      prefixes=
409165cb819Smrg
410165cb819Smrg      for d
411165cb819Smrg      do
41295cf0c00Smrg        test X"$d" = X && continue
41395cf0c00Smrg
41495cf0c00Smrg        prefix=$prefix$d
41595cf0c00Smrg        if test -d "$prefix"; then
41695cf0c00Smrg          prefixes=
41795cf0c00Smrg        else
41895cf0c00Smrg          if $posix_mkdir; then
4190103645bSmrg            (umask $mkdir_umask &&
42095cf0c00Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42195cf0c00Smrg            # Don't fail if two instances are running concurrently.
42295cf0c00Smrg            test -d "$prefix" || exit 1
42395cf0c00Smrg          else
42495cf0c00Smrg            case $prefix in
42595cf0c00Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42695cf0c00Smrg              *) qprefix=$prefix;;
42795cf0c00Smrg            esac
42895cf0c00Smrg            prefixes="$prefixes '$qprefix'"
42995cf0c00Smrg          fi
43095cf0c00Smrg        fi
43195cf0c00Smrg        prefix=$prefix/
432165cb819Smrg      done
433165cb819Smrg
434165cb819Smrg      if test -n "$prefixes"; then
43595cf0c00Smrg        # Don't fail if two instances are running concurrently.
43695cf0c00Smrg        (umask $mkdir_umask &&
43795cf0c00Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43895cf0c00Smrg          test -d "$dstdir" || exit 1
43995cf0c00Smrg        obsolete_mkdir_used=true
440100ae103Smrg      fi
441165cb819Smrg    fi
442100ae103Smrg  fi
443100ae103Smrg
444100ae103Smrg  if test -n "$dir_arg"; then
445165cb819Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446165cb819Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447165cb819Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448165cb819Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449100ae103Smrg  else
450100ae103Smrg
451100ae103Smrg    # Make a couple of temp file names in the proper directory.
4520103645bSmrg    dsttmp=${dstdirslash}_inst.$$_
4530103645bSmrg    rmtmp=${dstdirslash}_rm.$$_
454100ae103Smrg
455100ae103Smrg    # Trap to clean up those temp files at exit.
456100ae103Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457100ae103Smrg
458100ae103Smrg    # Copy the file name to the temp name.
4590103645bSmrg    (umask $cp_umask &&
4600103645bSmrg     { test -z "$stripcmd" || {
4610103645bSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620103645bSmrg	 # which would cause strip to fail.
4630103645bSmrg	 if test -z "$doit"; then
4640103645bSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650103645bSmrg	 else
4660103645bSmrg	   $doit touch "$dsttmp"
4670103645bSmrg	 fi
4680103645bSmrg       }
4690103645bSmrg     } &&
4700103645bSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471100ae103Smrg
472100ae103Smrg    # and set any options; do chmod last to preserve setuid bits.
473100ae103Smrg    #
474100ae103Smrg    # If any of these fail, we abort the whole thing.  If we want to
475100ae103Smrg    # ignore errors from any of these, just make sure not to ignore
476100ae103Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477100ae103Smrg    #
478165cb819Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479165cb819Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480165cb819Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481165cb819Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482165cb819Smrg
483165cb819Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484165cb819Smrg    if $copy_on_change &&
48595cf0c00Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48695cf0c00Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48795cf0c00Smrg       set -f &&
488165cb819Smrg       set X $old && old=:$2:$4:$5:$6 &&
489165cb819Smrg       set X $new && new=:$2:$4:$5:$6 &&
49095cf0c00Smrg       set +f &&
491165cb819Smrg       test "$old" = "$new" &&
492165cb819Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493165cb819Smrg    then
494165cb819Smrg      rm -f "$dsttmp"
495165cb819Smrg    else
4960103645bSmrg      # If $backupsuffix is set, and the file being installed
4970103645bSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4980103645bSmrg      # e.g., if mv doesn't support -f.
4990103645bSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000103645bSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010103645bSmrg      fi
5020103645bSmrg
503165cb819Smrg      # Rename the file to the real destination.
504165cb819Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505165cb819Smrg
506165cb819Smrg      # The rename failed, perhaps because mv can't rename something else
507165cb819Smrg      # to itself, or perhaps because mv is so ancient that it does not
508165cb819Smrg      # support -f.
509165cb819Smrg      {
51095cf0c00Smrg        # Now remove or move aside any old file at destination location.
51195cf0c00Smrg        # We try this two ways since rm can't unlink itself on some
51295cf0c00Smrg        # systems and the destination file might be busy for other
51395cf0c00Smrg        # reasons.  In this case, the final cleanup might fail but the new
51495cf0c00Smrg        # file should still install successfully.
51595cf0c00Smrg        {
51695cf0c00Smrg          test ! -f "$dst" ||
5170103645bSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51895cf0c00Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190103645bSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52095cf0c00Smrg          } ||
52195cf0c00Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52295cf0c00Smrg            (exit 1); exit 1
52395cf0c00Smrg          }
52495cf0c00Smrg        } &&
52595cf0c00Smrg
52695cf0c00Smrg        # Now rename the file to the real destination.
52795cf0c00Smrg        $doit $mvcmd "$dsttmp" "$dst"
528165cb819Smrg      }
529165cb819Smrg    fi || exit 1
530165cb819Smrg
531165cb819Smrg    trap '' 0
532165cb819Smrg  fi
533100ae103Smrgdone
534100ae103Smrg
535100ae103Smrg# Local variables:
5360103645bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537100ae103Smrg# time-stamp-start: "scriptversion="
538100ae103Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390103645bSmrg# time-stamp-time-zone: "UTC0"
540165cb819Smrg# time-stamp-end: "; # UTC"
541100ae103Smrg# End:
542