install-sh revision a570218a
1b1297603Smrg#!/bin/sh
2b1297603Smrg# install - install a program, script, or datafile
3b1297603Smrg
4a570218aSmrgscriptversion=2018-03-11.20; # UTC
5b1297603Smrg
6b1297603Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7b1297603Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8b1297603Smrg# following copyright and license.
9b1297603Smrg#
10b1297603Smrg# Copyright (C) 1994 X Consortium
11b1297603Smrg#
12b1297603Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13b1297603Smrg# of this software and associated documentation files (the "Software"), to
14b1297603Smrg# deal in the Software without restriction, including without limitation the
15b1297603Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16b1297603Smrg# sell copies of the Software, and to permit persons to whom the Software is
17b1297603Smrg# furnished to do so, subject to the following conditions:
18b1297603Smrg#
19b1297603Smrg# The above copyright notice and this permission notice shall be included in
20b1297603Smrg# all copies or substantial portions of the Software.
21b1297603Smrg#
22b1297603Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23b1297603Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24b1297603Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25b1297603Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26b1297603Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27b1297603Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28b1297603Smrg#
29b1297603Smrg# Except as contained in this notice, the name of the X Consortium shall not
30b1297603Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31b1297603Smrg# ings in this Software without prior written authorization from the X Consor-
32b1297603Smrg# tium.
33b1297603Smrg#
34b1297603Smrg#
35b1297603Smrg# FSF changes to this file are in the public domain.
36b1297603Smrg#
37b1297603Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3833734831Smrg# 'make' implicit rules from creating a file called install from it
39b1297603Smrg# when there is no Makefile.
40b1297603Smrg#
41b1297603Smrg# This script is compatible with the BSD install script, but was written
425b944e2aSmrg# from scratch.
435b944e2aSmrg
44a570218aSmrgtab='	'
455b944e2aSmrgnl='
465b944e2aSmrg'
47a570218aSmrgIFS=" $tab$nl"
48b1297603Smrg
49a570218aSmrg# Set DOITPROG to "echo" to test this script.
50b1297603Smrg
5133734831Smrgdoit=${DOITPROG-}
52a570218aSmrgdoit_exec=${doit:-exec}
53b1297603Smrg
545b944e2aSmrg# Put in absolute file names if you don't have them in your path;
555b944e2aSmrg# or use environment vars.
56b1297603Smrg
5733734831Smrgchgrpprog=${CHGRPPROG-chgrp}
5833734831Smrgchmodprog=${CHMODPROG-chmod}
5933734831Smrgchownprog=${CHOWNPROG-chown}
6033734831Smrgcmpprog=${CMPPROG-cmp}
6133734831Smrgcpprog=${CPPROG-cp}
6233734831Smrgmkdirprog=${MKDIRPROG-mkdir}
6333734831Smrgmvprog=${MVPROG-mv}
6433734831Smrgrmprog=${RMPROG-rm}
6533734831Smrgstripprog=${STRIPPROG-strip}
6633734831Smrg
675b944e2aSmrgposix_mkdir=
685b944e2aSmrg
695b944e2aSmrg# Desired mode of installed file.
705b944e2aSmrgmode=0755
715b944e2aSmrg
7233734831Smrgchgrpcmd=
735b944e2aSmrgchmodcmd=$chmodprog
74b1297603Smrgchowncmd=
7533734831Smrgmvcmd=$mvprog
76b1297603Smrgrmcmd="$rmprog -f"
7733734831Smrgstripcmd=
7833734831Smrg
79b1297603Smrgsrc=
80b1297603Smrgdst=
81b1297603Smrgdir_arg=
8233734831Smrgdst_arg=
8333734831Smrg
8433734831Smrgcopy_on_change=false
85a570218aSmrgis_target_a_directory=possibly
86b1297603Smrg
8733734831Smrgusage="\
8833734831SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89b1297603Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90b1297603Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91b1297603Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92b1297603Smrg
93b1297603SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94b1297603SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95b1297603SmrgIn the 4th, create DIRECTORIES.
96b1297603Smrg
97b1297603SmrgOptions:
9833734831Smrg     --help     display this help and exit.
9933734831Smrg     --version  display version info and exit.
10033734831Smrg
10133734831Smrg  -c            (ignored)
10233734831Smrg  -C            install only if different (preserve the last data modification time)
10333734831Smrg  -d            create directories instead of installing files.
10433734831Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10533734831Smrg  -m MODE       $chmodprog installed files to MODE.
10633734831Smrg  -o USER       $chownprog installed files to USER.
10733734831Smrg  -s            $stripprog installed files.
10833734831Smrg  -t DIRECTORY  install into DIRECTORY.
10933734831Smrg  -T            report an error if DSTFILE is a directory.
110b1297603Smrg
111b1297603SmrgEnvironment variables override the default commands:
11233734831Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11333734831Smrg  RMPROG STRIPPROG
114b1297603Smrg"
115b1297603Smrg
1165b944e2aSmrgwhile test $# -ne 0; do
117b1297603Smrg  case $1 in
11833734831Smrg    -c) ;;
119b1297603Smrg
12033734831Smrg    -C) copy_on_change=true;;
12133734831Smrg
12233734831Smrg    -d) dir_arg=true;;
123b1297603Smrg
124b1297603Smrg    -g) chgrpcmd="$chgrpprog $2"
125a570218aSmrg        shift;;
126b1297603Smrg
127b1297603Smrg    --help) echo "$usage"; exit $?;;
128b1297603Smrg
1295b944e2aSmrg    -m) mode=$2
130a570218aSmrg        case $mode in
131a570218aSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132a570218aSmrg            echo "$0: invalid mode: $mode" >&2
133a570218aSmrg            exit 1;;
134a570218aSmrg        esac
135a570218aSmrg        shift;;
136b1297603Smrg
137b1297603Smrg    -o) chowncmd="$chownprog $2"
138a570218aSmrg        shift;;
139b1297603Smrg
14033734831Smrg    -s) stripcmd=$stripprog;;
141b1297603Smrg
142a570218aSmrg    -t)
143a570218aSmrg        is_target_a_directory=always
144a570218aSmrg        dst_arg=$2
145a570218aSmrg        # Protect names problematic for 'test' and other utilities.
146a570218aSmrg        case $dst_arg in
147a570218aSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148a570218aSmrg        esac
149a570218aSmrg        shift;;
150b1297603Smrg
151a570218aSmrg    -T) is_target_a_directory=never;;
152b1297603Smrg
153b1297603Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154b1297603Smrg
155a570218aSmrg    --) shift
156a570218aSmrg        break;;
1575b944e2aSmrg
158a570218aSmrg    -*) echo "$0: invalid option: $1" >&2
159a570218aSmrg        exit 1;;
1605b944e2aSmrg
1615b944e2aSmrg    *)  break;;
162b1297603Smrg  esac
16333734831Smrg  shift
164b1297603Smrgdone
165b1297603Smrg
166a570218aSmrg# We allow the use of options -d and -T together, by making -d
167a570218aSmrg# take the precedence; this is for compatibility with GNU install.
168a570218aSmrg
169a570218aSmrgif test -n "$dir_arg"; then
170a570218aSmrg  if test -n "$dst_arg"; then
171a570218aSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172a570218aSmrg    exit 1
173a570218aSmrg  fi
174a570218aSmrgfi
175a570218aSmrg
17633734831Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1775b944e2aSmrg  # When -d is used, all remaining arguments are directories to create.
1785b944e2aSmrg  # When -t is used, the destination is already specified.
1795b944e2aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1805b944e2aSmrg  for arg
1815b944e2aSmrg  do
18233734831Smrg    if test -n "$dst_arg"; then
1835b944e2aSmrg      # $@ is not empty: it contains at least $arg.
18433734831Smrg      set fnord "$@" "$dst_arg"
1855b944e2aSmrg      shift # fnord
1865b944e2aSmrg    fi
1875b944e2aSmrg    shift # arg
18833734831Smrg    dst_arg=$arg
18933734831Smrg    # Protect names problematic for 'test' and other utilities.
19033734831Smrg    case $dst_arg in
19133734831Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19233734831Smrg    esac
1935b944e2aSmrg  done
1945b944e2aSmrgfi
1955b944e2aSmrg
1965b944e2aSmrgif test $# -eq 0; then
197b1297603Smrg  if test -z "$dir_arg"; then
198b1297603Smrg    echo "$0: no input file specified." >&2
199b1297603Smrg    exit 1
200b1297603Smrg  fi
20133734831Smrg  # It's OK to call 'install-sh -d' without argument.
202b1297603Smrg  # This can happen when creating conditional directories.
203b1297603Smrg  exit 0
204b1297603Smrgfi
205b1297603Smrg
206a570218aSmrgif test -z "$dir_arg"; then
207a570218aSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208a570218aSmrg    if test ! -d "$dst_arg"; then
209a570218aSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210a570218aSmrg      exit 1
211a570218aSmrg    fi
212a570218aSmrg  fi
213a570218aSmrgfi
214a570218aSmrg
2155b944e2aSmrgif test -z "$dir_arg"; then
21633734831Smrg  do_exit='(exit $ret); exit $ret'
21733734831Smrg  trap "ret=129; $do_exit" 1
21833734831Smrg  trap "ret=130; $do_exit" 2
21933734831Smrg  trap "ret=141; $do_exit" 13
22033734831Smrg  trap "ret=143; $do_exit" 15
2215b944e2aSmrg
2225b944e2aSmrg  # Set umask so as not to create temps with too-generous modes.
2235b944e2aSmrg  # However, 'strip' requires both read and write access to temps.
2245b944e2aSmrg  case $mode in
2255b944e2aSmrg    # Optimize common cases.
2265b944e2aSmrg    *644) cp_umask=133;;
2275b944e2aSmrg    *755) cp_umask=22;;
2285b944e2aSmrg
2295b944e2aSmrg    *[0-7])
2305b944e2aSmrg      if test -z "$stripcmd"; then
231a570218aSmrg        u_plus_rw=
2325b944e2aSmrg      else
233a570218aSmrg        u_plus_rw='% 200'
2345b944e2aSmrg      fi
2355b944e2aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2365b944e2aSmrg    *)
2375b944e2aSmrg      if test -z "$stripcmd"; then
238a570218aSmrg        u_plus_rw=
2395b944e2aSmrg      else
240a570218aSmrg        u_plus_rw=,u+rw
2415b944e2aSmrg      fi
2425b944e2aSmrg      cp_umask=$mode$u_plus_rw;;
2435b944e2aSmrg  esac
2445b944e2aSmrgfi
2455b944e2aSmrg
246b1297603Smrgfor src
247b1297603Smrgdo
24833734831Smrg  # Protect names problematic for 'test' and other utilities.
249b1297603Smrg  case $src in
25033734831Smrg    -* | [=\(\)!]) src=./$src;;
251b1297603Smrg  esac
252b1297603Smrg
253b1297603Smrg  if test -n "$dir_arg"; then
254b1297603Smrg    dst=$src
2555b944e2aSmrg    dstdir=$dst
2565b944e2aSmrg    test -d "$dstdir"
2575b944e2aSmrg    dstdir_status=$?
258b1297603Smrg  else
2595b944e2aSmrg
260b1297603Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261b1297603Smrg    # might cause directories to be created, which would be especially bad
262b1297603Smrg    # if $src (and thus $dsttmp) contains '*'.
263b1297603Smrg    if test ! -f "$src" && test ! -d "$src"; then
264b1297603Smrg      echo "$0: $src does not exist." >&2
265b1297603Smrg      exit 1
266b1297603Smrg    fi
267b1297603Smrg
26833734831Smrg    if test -z "$dst_arg"; then
269b1297603Smrg      echo "$0: no destination specified." >&2
270b1297603Smrg      exit 1
271b1297603Smrg    fi
27233734831Smrg    dst=$dst_arg
273b1297603Smrg
274a570218aSmrg    # If destination is a directory, append the input filename.
275b1297603Smrg    if test -d "$dst"; then
276a570218aSmrg      if test "$is_target_a_directory" = never; then
277a570218aSmrg        echo "$0: $dst_arg: Is a directory" >&2
278a570218aSmrg        exit 1
279b1297603Smrg      fi
2805b944e2aSmrg      dstdir=$dst
281a570218aSmrg      dstbase=`basename "$src"`
282a570218aSmrg      case $dst in
283a570218aSmrg	*/) dst=$dst$dstbase;;
284a570218aSmrg	*)  dst=$dst/$dstbase;;
285a570218aSmrg      esac
2865b944e2aSmrg      dstdir_status=0
2875b944e2aSmrg    else
288a570218aSmrg      dstdir=`dirname "$dst"`
2895b944e2aSmrg      test -d "$dstdir"
2905b944e2aSmrg      dstdir_status=$?
291b1297603Smrg    fi
292b1297603Smrg  fi
293b1297603Smrg
294a570218aSmrg  case $dstdir in
295a570218aSmrg    */) dstdirslash=$dstdir;;
296a570218aSmrg    *)  dstdirslash=$dstdir/;;
297a570218aSmrg  esac
298a570218aSmrg
2995b944e2aSmrg  obsolete_mkdir_used=false
3005b944e2aSmrg
3015b944e2aSmrg  if test $dstdir_status != 0; then
3025b944e2aSmrg    case $posix_mkdir in
3035b944e2aSmrg      '')
304a570218aSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
305a570218aSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
306a570218aSmrg        umask=`umask`
307a570218aSmrg        case $stripcmd.$umask in
308a570218aSmrg          # Optimize common cases.
309a570218aSmrg          *[2367][2367]) mkdir_umask=$umask;;
310a570218aSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311a570218aSmrg
312a570218aSmrg          *[0-7])
313a570218aSmrg            mkdir_umask=`expr $umask + 22 \
314a570218aSmrg              - $umask % 100 % 40 + $umask % 20 \
315a570218aSmrg              - $umask % 10 % 4 + $umask % 2
316a570218aSmrg            `;;
317a570218aSmrg          *) mkdir_umask=$umask,go-w;;
318a570218aSmrg        esac
319a570218aSmrg
320a570218aSmrg        # With -d, create the new directory with the user-specified mode.
321a570218aSmrg        # Otherwise, rely on $mkdir_umask.
322a570218aSmrg        if test -n "$dir_arg"; then
323a570218aSmrg          mkdir_mode=-m$mode
324a570218aSmrg        else
325a570218aSmrg          mkdir_mode=
326a570218aSmrg        fi
327a570218aSmrg
328a570218aSmrg        posix_mkdir=false
329a570218aSmrg        case $umask in
330a570218aSmrg          *[123567][0-7][0-7])
331a570218aSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
332a570218aSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333a570218aSmrg            ;;
334a570218aSmrg          *)
335a570218aSmrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
336a570218aSmrg            # here however when possible just to lower collision chance.
337a570218aSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
338a570218aSmrg
339a570218aSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
340a570218aSmrg
341a570218aSmrg            # Because "mkdir -p" follows existing symlinks and we likely work
342a570218aSmrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
343a570218aSmrg            # directory is successfully created first before we actually test
344a570218aSmrg            # 'mkdir -p' feature.
345a570218aSmrg            if (umask $mkdir_umask &&
346a570218aSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
347a570218aSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
348a570218aSmrg            then
349a570218aSmrg              if test -z "$dir_arg" || {
350a570218aSmrg                   # Check for POSIX incompatibilities with -m.
351a570218aSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
352a570218aSmrg                   # other-writable bit of parent directory when it shouldn't.
353a570218aSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
354a570218aSmrg                   test_tmpdir="$tmpdir/a"
355a570218aSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
356a570218aSmrg                   case $ls_ld_tmpdir in
357a570218aSmrg                     d????-?r-*) different_mode=700;;
358a570218aSmrg                     d????-?--*) different_mode=755;;
359a570218aSmrg                     *) false;;
360a570218aSmrg                   esac &&
361a570218aSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
362a570218aSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
363a570218aSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
364a570218aSmrg                   }
365a570218aSmrg                 }
366a570218aSmrg              then posix_mkdir=:
367a570218aSmrg              fi
368a570218aSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
369a570218aSmrg            else
370a570218aSmrg              # Remove any dirs left behind by ancient mkdir implementations.
371a570218aSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
372a570218aSmrg            fi
373a570218aSmrg            trap '' 0;;
374a570218aSmrg        esac;;
3755b944e2aSmrg    esac
376b1297603Smrg
3775b944e2aSmrg    if
3785b944e2aSmrg      $posix_mkdir && (
379a570218aSmrg        umask $mkdir_umask &&
380a570218aSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3815b944e2aSmrg      )
3825b944e2aSmrg    then :
3835b944e2aSmrg    else
384b1297603Smrg
3855b944e2aSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3865b944e2aSmrg      # or it failed possibly due to a race condition.  Create the
3875b944e2aSmrg      # directory the slow way, step by step, checking for races as we go.
3885b944e2aSmrg
3895b944e2aSmrg      case $dstdir in
390a570218aSmrg        /*) prefix='/';;
391a570218aSmrg        [-=\(\)!]*) prefix='./';;
392a570218aSmrg        *)  prefix='';;
3935b944e2aSmrg      esac
3945b944e2aSmrg
3955b944e2aSmrg      oIFS=$IFS
3965b944e2aSmrg      IFS=/
397a570218aSmrg      set -f
3985b944e2aSmrg      set fnord $dstdir
399b1297603Smrg      shift
400a570218aSmrg      set +f
4015b944e2aSmrg      IFS=$oIFS
4025b944e2aSmrg
4035b944e2aSmrg      prefixes=
4045b944e2aSmrg
4055b944e2aSmrg      for d
4065b944e2aSmrg      do
407a570218aSmrg        test X"$d" = X && continue
408a570218aSmrg
409a570218aSmrg        prefix=$prefix$d
410a570218aSmrg        if test -d "$prefix"; then
411a570218aSmrg          prefixes=
412a570218aSmrg        else
413a570218aSmrg          if $posix_mkdir; then
414a570218aSmrg            (umask=$mkdir_umask &&
415a570218aSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416a570218aSmrg            # Don't fail if two instances are running concurrently.
417a570218aSmrg            test -d "$prefix" || exit 1
418a570218aSmrg          else
419a570218aSmrg            case $prefix in
420a570218aSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421a570218aSmrg              *) qprefix=$prefix;;
422a570218aSmrg            esac
423a570218aSmrg            prefixes="$prefixes '$qprefix'"
424a570218aSmrg          fi
425a570218aSmrg        fi
426a570218aSmrg        prefix=$prefix/
4275b944e2aSmrg      done
4285b944e2aSmrg
4295b944e2aSmrg      if test -n "$prefixes"; then
430a570218aSmrg        # Don't fail if two instances are running concurrently.
431a570218aSmrg        (umask $mkdir_umask &&
432a570218aSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
433a570218aSmrg          test -d "$dstdir" || exit 1
434a570218aSmrg        obsolete_mkdir_used=true
435b1297603Smrg      fi
4365b944e2aSmrg    fi
437b1297603Smrg  fi
438b1297603Smrg
439b1297603Smrg  if test -n "$dir_arg"; then
4405b944e2aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4415b944e2aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4425b944e2aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4435b944e2aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444b1297603Smrg  else
445b1297603Smrg
446b1297603Smrg    # Make a couple of temp file names in the proper directory.
447a570218aSmrg    dsttmp=${dstdirslash}_inst.$$_
448a570218aSmrg    rmtmp=${dstdirslash}_rm.$$_
449b1297603Smrg
450b1297603Smrg    # Trap to clean up those temp files at exit.
451b1297603Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452b1297603Smrg
453b1297603Smrg    # Copy the file name to the temp name.
4545b944e2aSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
455b1297603Smrg
456b1297603Smrg    # and set any options; do chmod last to preserve setuid bits.
457b1297603Smrg    #
458b1297603Smrg    # If any of these fail, we abort the whole thing.  If we want to
459b1297603Smrg    # ignore errors from any of these, just make sure not to ignore
460b1297603Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
461b1297603Smrg    #
46233734831Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46333734831Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46433734831Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46533734831Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46633734831Smrg
46733734831Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46833734831Smrg    if $copy_on_change &&
469a570218aSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
470a570218aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
471a570218aSmrg       set -f &&
47233734831Smrg       set X $old && old=:$2:$4:$5:$6 &&
47333734831Smrg       set X $new && new=:$2:$4:$5:$6 &&
474a570218aSmrg       set +f &&
47533734831Smrg       test "$old" = "$new" &&
47633734831Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47733734831Smrg    then
47833734831Smrg      rm -f "$dsttmp"
47933734831Smrg    else
48033734831Smrg      # Rename the file to the real destination.
48133734831Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48233734831Smrg
48333734831Smrg      # The rename failed, perhaps because mv can't rename something else
48433734831Smrg      # to itself, or perhaps because mv is so ancient that it does not
48533734831Smrg      # support -f.
48633734831Smrg      {
487a570218aSmrg        # Now remove or move aside any old file at destination location.
488a570218aSmrg        # We try this two ways since rm can't unlink itself on some
489a570218aSmrg        # systems and the destination file might be busy for other
490a570218aSmrg        # reasons.  In this case, the final cleanup might fail but the new
491a570218aSmrg        # file should still install successfully.
492a570218aSmrg        {
493a570218aSmrg          test ! -f "$dst" ||
494a570218aSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
495a570218aSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
496a570218aSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
497a570218aSmrg          } ||
498a570218aSmrg          { echo "$0: cannot unlink or rename $dst" >&2
499a570218aSmrg            (exit 1); exit 1
500a570218aSmrg          }
501a570218aSmrg        } &&
502a570218aSmrg
503a570218aSmrg        # Now rename the file to the real destination.
504a570218aSmrg        $doit $mvcmd "$dsttmp" "$dst"
50533734831Smrg      }
50633734831Smrg    fi || exit 1
507b1297603Smrg
5085b944e2aSmrg    trap '' 0
5095b944e2aSmrg  fi
5105b944e2aSmrgdone
511b1297603Smrg
512b1297603Smrg# Local variables:
513a570218aSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
514b1297603Smrg# time-stamp-start: "scriptversion="
515b1297603Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
516a570218aSmrg# time-stamp-time-zone: "UTC0"
51733734831Smrg# time-stamp-end: "; # UTC"
518b1297603Smrg# End:
519