18c9fbc29Smrg#!/bin/sh
28c9fbc29Smrg# install - install a program, script, or datafile
38c9fbc29Smrg
454cef2ddSmrgscriptversion=2020-11-14.01; # UTC
58c9fbc29Smrg
68c9fbc29Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
78c9fbc29Smrg# later released in X11R6 (xc/config/util/install.sh) with the
88c9fbc29Smrg# following copyright and license.
98c9fbc29Smrg#
108c9fbc29Smrg# Copyright (C) 1994 X Consortium
118c9fbc29Smrg#
128c9fbc29Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
138c9fbc29Smrg# of this software and associated documentation files (the "Software"), to
148c9fbc29Smrg# deal in the Software without restriction, including without limitation the
158c9fbc29Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168c9fbc29Smrg# sell copies of the Software, and to permit persons to whom the Software is
178c9fbc29Smrg# furnished to do so, subject to the following conditions:
188c9fbc29Smrg#
198c9fbc29Smrg# The above copyright notice and this permission notice shall be included in
208c9fbc29Smrg# all copies or substantial portions of the Software.
218c9fbc29Smrg#
228c9fbc29Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238c9fbc29Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248c9fbc29Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258c9fbc29Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268c9fbc29Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
278c9fbc29Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288c9fbc29Smrg#
298c9fbc29Smrg# Except as contained in this notice, the name of the X Consortium shall not
308c9fbc29Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
318c9fbc29Smrg# ings in this Software without prior written authorization from the X Consor-
328c9fbc29Smrg# tium.
338c9fbc29Smrg#
348c9fbc29Smrg#
358c9fbc29Smrg# FSF changes to this file are in the public domain.
368c9fbc29Smrg#
378c9fbc29Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3870728a38Smrg# 'make' implicit rules from creating a file called install from it
398c9fbc29Smrg# when there is no Makefile.
408c9fbc29Smrg#
418c9fbc29Smrg# This script is compatible with the BSD install script, but was written
428c9fbc29Smrg# from scratch.
438c9fbc29Smrg
4470728a38Smrgtab='	'
458c9fbc29Smrgnl='
468c9fbc29Smrg'
4770728a38SmrgIFS=" $tab$nl"
488c9fbc29Smrg
4970728a38Smrg# Set DOITPROG to "echo" to test this script.
508c9fbc29Smrg
514e411241Smrgdoit=${DOITPROG-}
5270728a38Smrgdoit_exec=${doit:-exec}
538c9fbc29Smrg
548c9fbc29Smrg# Put in absolute file names if you don't have them in your path;
558c9fbc29Smrg# or use environment vars.
568c9fbc29Smrg
574e411241Smrgchgrpprog=${CHGRPPROG-chgrp}
584e411241Smrgchmodprog=${CHMODPROG-chmod}
594e411241Smrgchownprog=${CHOWNPROG-chown}
604e411241Smrgcmpprog=${CMPPROG-cmp}
614e411241Smrgcpprog=${CPPROG-cp}
624e411241Smrgmkdirprog=${MKDIRPROG-mkdir}
634e411241Smrgmvprog=${MVPROG-mv}
644e411241Smrgrmprog=${RMPROG-rm}
654e411241Smrgstripprog=${STRIPPROG-strip}
664e411241Smrg
678c9fbc29Smrgposix_mkdir=
688c9fbc29Smrg
698c9fbc29Smrg# Desired mode of installed file.
708c9fbc29Smrgmode=0755
718c9fbc29Smrg
7254cef2ddSmrg# Create dirs (including intermediate dirs) using mode 755.
7354cef2ddSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7454cef2ddSmrgmkdir_umask=22
7554cef2ddSmrg
7654cef2ddSmrgbackupsuffix=
774e411241Smrgchgrpcmd=
788c9fbc29Smrgchmodcmd=$chmodprog
798c9fbc29Smrgchowncmd=
804e411241Smrgmvcmd=$mvprog
818c9fbc29Smrgrmcmd="$rmprog -f"
824e411241Smrgstripcmd=
834e411241Smrg
848c9fbc29Smrgsrc=
858c9fbc29Smrgdst=
868c9fbc29Smrgdir_arg=
874e411241Smrgdst_arg=
884e411241Smrg
894e411241Smrgcopy_on_change=false
9070728a38Smrgis_target_a_directory=possibly
918c9fbc29Smrg
924e411241Smrgusage="\
934e411241SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
948c9fbc29Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
958c9fbc29Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
968c9fbc29Smrg   or: $0 [OPTION]... -d DIRECTORIES...
978c9fbc29Smrg
988c9fbc29SmrgIn the 1st form, copy SRCFILE to DSTFILE.
998c9fbc29SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1008c9fbc29SmrgIn the 4th, create DIRECTORIES.
1018c9fbc29Smrg
1028c9fbc29SmrgOptions:
1034e411241Smrg     --help     display this help and exit.
1044e411241Smrg     --version  display version info and exit.
1054e411241Smrg
1064e411241Smrg  -c            (ignored)
10754cef2ddSmrg  -C            install only if different (preserve data modification time)
1084e411241Smrg  -d            create directories instead of installing files.
1094e411241Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1104e411241Smrg  -m MODE       $chmodprog installed files to MODE.
1114e411241Smrg  -o USER       $chownprog installed files to USER.
11254cef2ddSmrg  -p            pass -p to $cpprog.
1134e411241Smrg  -s            $stripprog installed files.
11454cef2ddSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1154e411241Smrg  -t DIRECTORY  install into DIRECTORY.
1164e411241Smrg  -T            report an error if DSTFILE is a directory.
1178c9fbc29Smrg
1188c9fbc29SmrgEnvironment variables override the default commands:
1194e411241Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1204e411241Smrg  RMPROG STRIPPROG
12154cef2ddSmrg
12254cef2ddSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12354cef2ddSmrgit's up to you to specify -f if you want it.
12454cef2ddSmrg
12554cef2ddSmrgIf -S is not specified, no backups are attempted.
12654cef2ddSmrg
12754cef2ddSmrgEmail bug reports to bug-automake@gnu.org.
12854cef2ddSmrgAutomake home page: https://www.gnu.org/software/automake/
1298c9fbc29Smrg"
1308c9fbc29Smrg
1318c9fbc29Smrgwhile test $# -ne 0; do
1328c9fbc29Smrg  case $1 in
1334e411241Smrg    -c) ;;
1344e411241Smrg
1354e411241Smrg    -C) copy_on_change=true;;
1368c9fbc29Smrg
1374e411241Smrg    -d) dir_arg=true;;
1388c9fbc29Smrg
1398c9fbc29Smrg    -g) chgrpcmd="$chgrpprog $2"
14070728a38Smrg        shift;;
1418c9fbc29Smrg
1428c9fbc29Smrg    --help) echo "$usage"; exit $?;;
1438c9fbc29Smrg
1448c9fbc29Smrg    -m) mode=$2
14570728a38Smrg        case $mode in
14670728a38Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14770728a38Smrg            echo "$0: invalid mode: $mode" >&2
14870728a38Smrg            exit 1;;
14970728a38Smrg        esac
15070728a38Smrg        shift;;
1518c9fbc29Smrg
1528c9fbc29Smrg    -o) chowncmd="$chownprog $2"
15370728a38Smrg        shift;;
1548c9fbc29Smrg
15554cef2ddSmrg    -p) cpprog="$cpprog -p";;
15654cef2ddSmrg
1574e411241Smrg    -s) stripcmd=$stripprog;;
1588c9fbc29Smrg
15954cef2ddSmrg    -S) backupsuffix="$2"
16054cef2ddSmrg        shift;;
16154cef2ddSmrg
16270728a38Smrg    -t)
16370728a38Smrg        is_target_a_directory=always
16470728a38Smrg        dst_arg=$2
16570728a38Smrg        # Protect names problematic for 'test' and other utilities.
16670728a38Smrg        case $dst_arg in
16770728a38Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16870728a38Smrg        esac
16970728a38Smrg        shift;;
1708c9fbc29Smrg
17170728a38Smrg    -T) is_target_a_directory=never;;
1728c9fbc29Smrg
1738c9fbc29Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1748c9fbc29Smrg
17570728a38Smrg    --) shift
17670728a38Smrg        break;;
1778c9fbc29Smrg
17870728a38Smrg    -*) echo "$0: invalid option: $1" >&2
17970728a38Smrg        exit 1;;
1808c9fbc29Smrg
1818c9fbc29Smrg    *)  break;;
1828c9fbc29Smrg  esac
1834e411241Smrg  shift
1848c9fbc29Smrgdone
1858c9fbc29Smrg
18670728a38Smrg# We allow the use of options -d and -T together, by making -d
18770728a38Smrg# take the precedence; this is for compatibility with GNU install.
18870728a38Smrg
18970728a38Smrgif test -n "$dir_arg"; then
19070728a38Smrg  if test -n "$dst_arg"; then
19170728a38Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19270728a38Smrg    exit 1
19370728a38Smrg  fi
19470728a38Smrgfi
19570728a38Smrg
1964e411241Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1978c9fbc29Smrg  # When -d is used, all remaining arguments are directories to create.
1988c9fbc29Smrg  # When -t is used, the destination is already specified.
1998c9fbc29Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2008c9fbc29Smrg  for arg
2018c9fbc29Smrg  do
2024e411241Smrg    if test -n "$dst_arg"; then
2038c9fbc29Smrg      # $@ is not empty: it contains at least $arg.
2044e411241Smrg      set fnord "$@" "$dst_arg"
2058c9fbc29Smrg      shift # fnord
2068c9fbc29Smrg    fi
2078c9fbc29Smrg    shift # arg
2084e411241Smrg    dst_arg=$arg
20970728a38Smrg    # Protect names problematic for 'test' and other utilities.
21070728a38Smrg    case $dst_arg in
21170728a38Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21270728a38Smrg    esac
2138c9fbc29Smrg  done
2148c9fbc29Smrgfi
2158c9fbc29Smrg
2168c9fbc29Smrgif test $# -eq 0; then
2178c9fbc29Smrg  if test -z "$dir_arg"; then
2188c9fbc29Smrg    echo "$0: no input file specified." >&2
2198c9fbc29Smrg    exit 1
2208c9fbc29Smrg  fi
22170728a38Smrg  # It's OK to call 'install-sh -d' without argument.
2228c9fbc29Smrg  # This can happen when creating conditional directories.
2238c9fbc29Smrg  exit 0
2248c9fbc29Smrgfi
2258c9fbc29Smrg
2268c9fbc29Smrgif test -z "$dir_arg"; then
22770728a38Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22870728a38Smrg    if test ! -d "$dst_arg"; then
22970728a38Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23070728a38Smrg      exit 1
23170728a38Smrg    fi
23270728a38Smrg  fi
23370728a38Smrgfi
23470728a38Smrg
23570728a38Smrgif test -z "$dir_arg"; then
23670728a38Smrg  do_exit='(exit $ret); exit $ret'
23770728a38Smrg  trap "ret=129; $do_exit" 1
23870728a38Smrg  trap "ret=130; $do_exit" 2
23970728a38Smrg  trap "ret=141; $do_exit" 13
24070728a38Smrg  trap "ret=143; $do_exit" 15
2418c9fbc29Smrg
2428c9fbc29Smrg  # Set umask so as not to create temps with too-generous modes.
2438c9fbc29Smrg  # However, 'strip' requires both read and write access to temps.
2448c9fbc29Smrg  case $mode in
2458c9fbc29Smrg    # Optimize common cases.
2468c9fbc29Smrg    *644) cp_umask=133;;
2478c9fbc29Smrg    *755) cp_umask=22;;
2488c9fbc29Smrg
2498c9fbc29Smrg    *[0-7])
2508c9fbc29Smrg      if test -z "$stripcmd"; then
25170728a38Smrg        u_plus_rw=
2528c9fbc29Smrg      else
25370728a38Smrg        u_plus_rw='% 200'
2548c9fbc29Smrg      fi
2558c9fbc29Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2568c9fbc29Smrg    *)
2578c9fbc29Smrg      if test -z "$stripcmd"; then
25870728a38Smrg        u_plus_rw=
2598c9fbc29Smrg      else
26070728a38Smrg        u_plus_rw=,u+rw
2618c9fbc29Smrg      fi
2628c9fbc29Smrg      cp_umask=$mode$u_plus_rw;;
2638c9fbc29Smrg  esac
2648c9fbc29Smrgfi
2658c9fbc29Smrg
2668c9fbc29Smrgfor src
2678c9fbc29Smrgdo
26870728a38Smrg  # Protect names problematic for 'test' and other utilities.
2698c9fbc29Smrg  case $src in
27070728a38Smrg    -* | [=\(\)!]) src=./$src;;
2718c9fbc29Smrg  esac
2728c9fbc29Smrg
2738c9fbc29Smrg  if test -n "$dir_arg"; then
2748c9fbc29Smrg    dst=$src
2758c9fbc29Smrg    dstdir=$dst
2768c9fbc29Smrg    test -d "$dstdir"
2778c9fbc29Smrg    dstdir_status=$?
27854cef2ddSmrg    # Don't chown directories that already exist.
27954cef2ddSmrg    if test $dstdir_status = 0; then
28054cef2ddSmrg      chowncmd=""
28154cef2ddSmrg    fi
2828c9fbc29Smrg  else
2838c9fbc29Smrg
2848c9fbc29Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2858c9fbc29Smrg    # might cause directories to be created, which would be especially bad
2868c9fbc29Smrg    # if $src (and thus $dsttmp) contains '*'.
2878c9fbc29Smrg    if test ! -f "$src" && test ! -d "$src"; then
2888c9fbc29Smrg      echo "$0: $src does not exist." >&2
2898c9fbc29Smrg      exit 1
2908c9fbc29Smrg    fi
2918c9fbc29Smrg
2924e411241Smrg    if test -z "$dst_arg"; then
2938c9fbc29Smrg      echo "$0: no destination specified." >&2
2948c9fbc29Smrg      exit 1
2958c9fbc29Smrg    fi
2964e411241Smrg    dst=$dst_arg
2978c9fbc29Smrg
29854cef2ddSmrg    # If destination is a directory, append the input filename.
2998c9fbc29Smrg    if test -d "$dst"; then
30070728a38Smrg      if test "$is_target_a_directory" = never; then
30170728a38Smrg        echo "$0: $dst_arg: Is a directory" >&2
30270728a38Smrg        exit 1
3038c9fbc29Smrg      fi
3048c9fbc29Smrg      dstdir=$dst
30554cef2ddSmrg      dstbase=`basename "$src"`
30654cef2ddSmrg      case $dst in
30754cef2ddSmrg	*/) dst=$dst$dstbase;;
30854cef2ddSmrg	*)  dst=$dst/$dstbase;;
30954cef2ddSmrg      esac
3108c9fbc29Smrg      dstdir_status=0
3118c9fbc29Smrg    else
31270728a38Smrg      dstdir=`dirname "$dst"`
3138c9fbc29Smrg      test -d "$dstdir"
3148c9fbc29Smrg      dstdir_status=$?
3158c9fbc29Smrg    fi
3168c9fbc29Smrg  fi
3178c9fbc29Smrg
31854cef2ddSmrg  case $dstdir in
31954cef2ddSmrg    */) dstdirslash=$dstdir;;
32054cef2ddSmrg    *)  dstdirslash=$dstdir/;;
32154cef2ddSmrg  esac
32254cef2ddSmrg
3238c9fbc29Smrg  obsolete_mkdir_used=false
3248c9fbc29Smrg
3258c9fbc29Smrg  if test $dstdir_status != 0; then
3268c9fbc29Smrg    case $posix_mkdir in
3278c9fbc29Smrg      '')
32870728a38Smrg        # With -d, create the new directory with the user-specified mode.
32970728a38Smrg        # Otherwise, rely on $mkdir_umask.
33070728a38Smrg        if test -n "$dir_arg"; then
33170728a38Smrg          mkdir_mode=-m$mode
33270728a38Smrg        else
33370728a38Smrg          mkdir_mode=
33470728a38Smrg        fi
33570728a38Smrg
33670728a38Smrg        posix_mkdir=false
33754cef2ddSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33854cef2ddSmrg	# here however when possible just to lower collision chance.
33954cef2ddSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34054cef2ddSmrg
34154cef2ddSmrg	trap '
34254cef2ddSmrg	  ret=$?
34354cef2ddSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34454cef2ddSmrg	  exit $ret
34554cef2ddSmrg	' 0
34654cef2ddSmrg
34754cef2ddSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34854cef2ddSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34954cef2ddSmrg	# directory is successfully created first before we actually test
35054cef2ddSmrg	# 'mkdir -p'.
35154cef2ddSmrg	if (umask $mkdir_umask &&
35254cef2ddSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35354cef2ddSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35454cef2ddSmrg	then
35554cef2ddSmrg	  if test -z "$dir_arg" || {
35654cef2ddSmrg	       # Check for POSIX incompatibilities with -m.
35754cef2ddSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35854cef2ddSmrg	       # other-writable bit of parent directory when it shouldn't.
35954cef2ddSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36054cef2ddSmrg	       test_tmpdir="$tmpdir/a"
36154cef2ddSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36254cef2ddSmrg	       case $ls_ld_tmpdir in
36354cef2ddSmrg		 d????-?r-*) different_mode=700;;
36454cef2ddSmrg		 d????-?--*) different_mode=755;;
36554cef2ddSmrg		 *) false;;
36654cef2ddSmrg	       esac &&
36754cef2ddSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36854cef2ddSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36954cef2ddSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37054cef2ddSmrg	       }
37154cef2ddSmrg	     }
37254cef2ddSmrg	  then posix_mkdir=:
37354cef2ddSmrg	  fi
37454cef2ddSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37554cef2ddSmrg	else
37654cef2ddSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37754cef2ddSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37854cef2ddSmrg	fi
37954cef2ddSmrg	trap '' 0;;
3808c9fbc29Smrg    esac
3818c9fbc29Smrg
3828c9fbc29Smrg    if
3838c9fbc29Smrg      $posix_mkdir && (
38470728a38Smrg        umask $mkdir_umask &&
38570728a38Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3868c9fbc29Smrg      )
3878c9fbc29Smrg    then :
3888c9fbc29Smrg    else
3898c9fbc29Smrg
39054cef2ddSmrg      # mkdir does not conform to POSIX,
3918c9fbc29Smrg      # or it failed possibly due to a race condition.  Create the
3928c9fbc29Smrg      # directory the slow way, step by step, checking for races as we go.
3938c9fbc29Smrg
3948c9fbc29Smrg      case $dstdir in
39570728a38Smrg        /*) prefix='/';;
39670728a38Smrg        [-=\(\)!]*) prefix='./';;
39770728a38Smrg        *)  prefix='';;
3988c9fbc29Smrg      esac
3998c9fbc29Smrg
4008c9fbc29Smrg      oIFS=$IFS
4018c9fbc29Smrg      IFS=/
40270728a38Smrg      set -f
4038c9fbc29Smrg      set fnord $dstdir
4048c9fbc29Smrg      shift
40570728a38Smrg      set +f
4068c9fbc29Smrg      IFS=$oIFS
4078c9fbc29Smrg
4088c9fbc29Smrg      prefixes=
4098c9fbc29Smrg
4108c9fbc29Smrg      for d
4118c9fbc29Smrg      do
41270728a38Smrg        test X"$d" = X && continue
41370728a38Smrg
41470728a38Smrg        prefix=$prefix$d
41570728a38Smrg        if test -d "$prefix"; then
41670728a38Smrg          prefixes=
41770728a38Smrg        else
41870728a38Smrg          if $posix_mkdir; then
41954cef2ddSmrg            (umask $mkdir_umask &&
42070728a38Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42170728a38Smrg            # Don't fail if two instances are running concurrently.
42270728a38Smrg            test -d "$prefix" || exit 1
42370728a38Smrg          else
42470728a38Smrg            case $prefix in
42570728a38Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42670728a38Smrg              *) qprefix=$prefix;;
42770728a38Smrg            esac
42870728a38Smrg            prefixes="$prefixes '$qprefix'"
42970728a38Smrg          fi
43070728a38Smrg        fi
43170728a38Smrg        prefix=$prefix/
4328c9fbc29Smrg      done
4338c9fbc29Smrg
4348c9fbc29Smrg      if test -n "$prefixes"; then
43570728a38Smrg        # Don't fail if two instances are running concurrently.
43670728a38Smrg        (umask $mkdir_umask &&
43770728a38Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43870728a38Smrg          test -d "$dstdir" || exit 1
43970728a38Smrg        obsolete_mkdir_used=true
4408c9fbc29Smrg      fi
4418c9fbc29Smrg    fi
4428c9fbc29Smrg  fi
4438c9fbc29Smrg
4448c9fbc29Smrg  if test -n "$dir_arg"; then
4458c9fbc29Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4468c9fbc29Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4478c9fbc29Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4488c9fbc29Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4498c9fbc29Smrg  else
4508c9fbc29Smrg
4518c9fbc29Smrg    # Make a couple of temp file names in the proper directory.
45254cef2ddSmrg    dsttmp=${dstdirslash}_inst.$$_
45354cef2ddSmrg    rmtmp=${dstdirslash}_rm.$$_
4548c9fbc29Smrg
4558c9fbc29Smrg    # Trap to clean up those temp files at exit.
4568c9fbc29Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4578c9fbc29Smrg
4588c9fbc29Smrg    # Copy the file name to the temp name.
45954cef2ddSmrg    (umask $cp_umask &&
46054cef2ddSmrg     { test -z "$stripcmd" || {
46154cef2ddSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46254cef2ddSmrg	 # which would cause strip to fail.
46354cef2ddSmrg	 if test -z "$doit"; then
46454cef2ddSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46554cef2ddSmrg	 else
46654cef2ddSmrg	   $doit touch "$dsttmp"
46754cef2ddSmrg	 fi
46854cef2ddSmrg       }
46954cef2ddSmrg     } &&
47054cef2ddSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4718c9fbc29Smrg
4728c9fbc29Smrg    # and set any options; do chmod last to preserve setuid bits.
4738c9fbc29Smrg    #
4748c9fbc29Smrg    # If any of these fail, we abort the whole thing.  If we want to
4758c9fbc29Smrg    # ignore errors from any of these, just make sure not to ignore
4768c9fbc29Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4778c9fbc29Smrg    #
4784e411241Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4794e411241Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4804e411241Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4814e411241Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4824e411241Smrg
4834e411241Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4844e411241Smrg    if $copy_on_change &&
48570728a38Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48670728a38Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48770728a38Smrg       set -f &&
4884e411241Smrg       set X $old && old=:$2:$4:$5:$6 &&
4894e411241Smrg       set X $new && new=:$2:$4:$5:$6 &&
49070728a38Smrg       set +f &&
4914e411241Smrg       test "$old" = "$new" &&
4924e411241Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4934e411241Smrg    then
4944e411241Smrg      rm -f "$dsttmp"
4954e411241Smrg    else
49654cef2ddSmrg      # If $backupsuffix is set, and the file being installed
49754cef2ddSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49854cef2ddSmrg      # e.g., if mv doesn't support -f.
49954cef2ddSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50054cef2ddSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50154cef2ddSmrg      fi
50254cef2ddSmrg
5034e411241Smrg      # Rename the file to the real destination.
5044e411241Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5054e411241Smrg
5064e411241Smrg      # The rename failed, perhaps because mv can't rename something else
5074e411241Smrg      # to itself, or perhaps because mv is so ancient that it does not
5084e411241Smrg      # support -f.
5094e411241Smrg      {
51070728a38Smrg        # Now remove or move aside any old file at destination location.
51170728a38Smrg        # We try this two ways since rm can't unlink itself on some
51270728a38Smrg        # systems and the destination file might be busy for other
51370728a38Smrg        # reasons.  In this case, the final cleanup might fail but the new
51470728a38Smrg        # file should still install successfully.
51570728a38Smrg        {
51670728a38Smrg          test ! -f "$dst" ||
51754cef2ddSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51870728a38Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51954cef2ddSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52070728a38Smrg          } ||
52170728a38Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52270728a38Smrg            (exit 1); exit 1
52370728a38Smrg          }
52470728a38Smrg        } &&
52570728a38Smrg
52670728a38Smrg        # Now rename the file to the real destination.
52770728a38Smrg        $doit $mvcmd "$dsttmp" "$dst"
5284e411241Smrg      }
5294e411241Smrg    fi || exit 1
5308c9fbc29Smrg
5318c9fbc29Smrg    trap '' 0
5328c9fbc29Smrg  fi
5338c9fbc29Smrgdone
5348c9fbc29Smrg
5358c9fbc29Smrg# Local variables:
53654cef2ddSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5378c9fbc29Smrg# time-stamp-start: "scriptversion="
5388c9fbc29Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53954cef2ddSmrg# time-stamp-time-zone: "UTC0"
5404e411241Smrg# time-stamp-end: "; # UTC"
5418c9fbc29Smrg# End:
542