144269bb5Schristos#!/bin/sh 244269bb5Schristos# install - install a program, script, or datafile 344269bb5Schristos 4*024a2e8cSchristosscriptversion=2020-11-14.01; # UTC 544269bb5Schristos 644269bb5Schristos# This originates from X11R5 (mit/util/scripts/install.sh), which was 744269bb5Schristos# later released in X11R6 (xc/config/util/install.sh) with the 844269bb5Schristos# following copyright and license. 944269bb5Schristos# 1044269bb5Schristos# Copyright (C) 1994 X Consortium 1144269bb5Schristos# 1244269bb5Schristos# Permission is hereby granted, free of charge, to any person obtaining a copy 1344269bb5Schristos# of this software and associated documentation files (the "Software"), to 1444269bb5Schristos# deal in the Software without restriction, including without limitation the 1544269bb5Schristos# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1644269bb5Schristos# sell copies of the Software, and to permit persons to whom the Software is 1744269bb5Schristos# furnished to do so, subject to the following conditions: 1844269bb5Schristos# 1944269bb5Schristos# The above copyright notice and this permission notice shall be included in 2044269bb5Schristos# all copies or substantial portions of the Software. 2144269bb5Schristos# 2244269bb5Schristos# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2344269bb5Schristos# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2444269bb5Schristos# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2544269bb5Schristos# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2644269bb5Schristos# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2744269bb5Schristos# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2844269bb5Schristos# 2944269bb5Schristos# Except as contained in this notice, the name of the X Consortium shall not 3044269bb5Schristos# be used in advertising or otherwise to promote the sale, use or other deal- 3144269bb5Schristos# ings in this Software without prior written authorization from the X Consor- 3244269bb5Schristos# tium. 3344269bb5Schristos# 3444269bb5Schristos# 3544269bb5Schristos# FSF changes to this file are in the public domain. 3644269bb5Schristos# 3744269bb5Schristos# Calling this script install-sh is preferred over install.sh, to prevent 380bbc3b9fSchristos# 'make' implicit rules from creating a file called install from it 3944269bb5Schristos# when there is no Makefile. 4044269bb5Schristos# 4144269bb5Schristos# This script is compatible with the BSD install script, but was written 4244269bb5Schristos# from scratch. 4344269bb5Schristos 445ca12230Schristostab=' ' 4544269bb5Schristosnl=' 4644269bb5Schristos' 475ca12230SchristosIFS=" $tab$nl" 4844269bb5Schristos 495ca12230Schristos# Set DOITPROG to "echo" to test this script. 5044269bb5Schristos 5144269bb5Schristosdoit=${DOITPROG-} 525ca12230Schristosdoit_exec=${doit:-exec} 5344269bb5Schristos 5444269bb5Schristos# Put in absolute file names if you don't have them in your path; 5544269bb5Schristos# or use environment vars. 5644269bb5Schristos 5744269bb5Schristoschgrpprog=${CHGRPPROG-chgrp} 5844269bb5Schristoschmodprog=${CHMODPROG-chmod} 5944269bb5Schristoschownprog=${CHOWNPROG-chown} 6044269bb5Schristoscmpprog=${CMPPROG-cmp} 6144269bb5Schristoscpprog=${CPPROG-cp} 6244269bb5Schristosmkdirprog=${MKDIRPROG-mkdir} 6344269bb5Schristosmvprog=${MVPROG-mv} 6444269bb5Schristosrmprog=${RMPROG-rm} 6544269bb5Schristosstripprog=${STRIPPROG-strip} 6644269bb5Schristos 6744269bb5Schristosposix_mkdir= 6844269bb5Schristos 6944269bb5Schristos# Desired mode of installed file. 7044269bb5Schristosmode=0755 7144269bb5Schristos 72*024a2e8cSchristos# Create dirs (including intermediate dirs) using mode 755. 73*024a2e8cSchristos# This is like GNU 'install' as of coreutils 8.32 (2020). 74*024a2e8cSchristosmkdir_umask=22 75*024a2e8cSchristos 76*024a2e8cSchristosbackupsuffix= 7744269bb5Schristoschgrpcmd= 7844269bb5Schristoschmodcmd=$chmodprog 7944269bb5Schristoschowncmd= 8044269bb5Schristosmvcmd=$mvprog 8144269bb5Schristosrmcmd="$rmprog -f" 8244269bb5Schristosstripcmd= 8344269bb5Schristos 8444269bb5Schristossrc= 8544269bb5Schristosdst= 8644269bb5Schristosdir_arg= 8744269bb5Schristosdst_arg= 8844269bb5Schristos 8944269bb5Schristoscopy_on_change=false 905ca12230Schristosis_target_a_directory=possibly 9144269bb5Schristos 9244269bb5Schristosusage="\ 9344269bb5SchristosUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 9444269bb5Schristos or: $0 [OPTION]... SRCFILES... DIRECTORY 9544269bb5Schristos or: $0 [OPTION]... -t DIRECTORY SRCFILES... 9644269bb5Schristos or: $0 [OPTION]... -d DIRECTORIES... 9744269bb5Schristos 9844269bb5SchristosIn the 1st form, copy SRCFILE to DSTFILE. 9944269bb5SchristosIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 10044269bb5SchristosIn the 4th, create DIRECTORIES. 10144269bb5Schristos 10244269bb5SchristosOptions: 10344269bb5Schristos --help display this help and exit. 10444269bb5Schristos --version display version info and exit. 10544269bb5Schristos 10644269bb5Schristos -c (ignored) 107*024a2e8cSchristos -C install only if different (preserve data modification time) 10844269bb5Schristos -d create directories instead of installing files. 10944269bb5Schristos -g GROUP $chgrpprog installed files to GROUP. 11044269bb5Schristos -m MODE $chmodprog installed files to MODE. 11144269bb5Schristos -o USER $chownprog installed files to USER. 112*024a2e8cSchristos -p pass -p to $cpprog. 11344269bb5Schristos -s $stripprog installed files. 114*024a2e8cSchristos -S SUFFIX attempt to back up existing files, with suffix SUFFIX. 11544269bb5Schristos -t DIRECTORY install into DIRECTORY. 11644269bb5Schristos -T report an error if DSTFILE is a directory. 11744269bb5Schristos 11844269bb5SchristosEnvironment variables override the default commands: 11944269bb5Schristos CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 12044269bb5Schristos RMPROG STRIPPROG 121*024a2e8cSchristos 122*024a2e8cSchristosBy default, rm is invoked with -f; when overridden with RMPROG, 123*024a2e8cSchristosit's up to you to specify -f if you want it. 124*024a2e8cSchristos 125*024a2e8cSchristosIf -S is not specified, no backups are attempted. 126*024a2e8cSchristos 127*024a2e8cSchristosEmail bug reports to bug-automake@gnu.org. 128*024a2e8cSchristosAutomake home page: https://www.gnu.org/software/automake/ 12944269bb5Schristos" 13044269bb5Schristos 13144269bb5Schristoswhile test $# -ne 0; do 13244269bb5Schristos case $1 in 13344269bb5Schristos -c) ;; 13444269bb5Schristos 13544269bb5Schristos -C) copy_on_change=true;; 13644269bb5Schristos 13744269bb5Schristos -d) dir_arg=true;; 13844269bb5Schristos 13944269bb5Schristos -g) chgrpcmd="$chgrpprog $2" 14044269bb5Schristos shift;; 14144269bb5Schristos 14244269bb5Schristos --help) echo "$usage"; exit $?;; 14344269bb5Schristos 14444269bb5Schristos -m) mode=$2 14544269bb5Schristos case $mode in 1465ca12230Schristos *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*) 14744269bb5Schristos echo "$0: invalid mode: $mode" >&2 14844269bb5Schristos exit 1;; 14944269bb5Schristos esac 15044269bb5Schristos shift;; 15144269bb5Schristos 15244269bb5Schristos -o) chowncmd="$chownprog $2" 15344269bb5Schristos shift;; 15444269bb5Schristos 155*024a2e8cSchristos -p) cpprog="$cpprog -p";; 156*024a2e8cSchristos 15744269bb5Schristos -s) stripcmd=$stripprog;; 15844269bb5Schristos 159*024a2e8cSchristos -S) backupsuffix="$2" 160*024a2e8cSchristos shift;; 161*024a2e8cSchristos 1625ca12230Schristos -t) 1635ca12230Schristos is_target_a_directory=always 1645ca12230Schristos dst_arg=$2 1650bbc3b9fSchristos # Protect names problematic for 'test' and other utilities. 1660bbc3b9fSchristos case $dst_arg in 1670bbc3b9fSchristos -* | [=\(\)!]) dst_arg=./$dst_arg;; 1680bbc3b9fSchristos esac 16944269bb5Schristos shift;; 17044269bb5Schristos 1715ca12230Schristos -T) is_target_a_directory=never;; 17244269bb5Schristos 17344269bb5Schristos --version) echo "$0 $scriptversion"; exit $?;; 17444269bb5Schristos 17544269bb5Schristos --) shift 17644269bb5Schristos break;; 17744269bb5Schristos 17844269bb5Schristos -*) echo "$0: invalid option: $1" >&2 17944269bb5Schristos exit 1;; 18044269bb5Schristos 18144269bb5Schristos *) break;; 18244269bb5Schristos esac 18344269bb5Schristos shift 18444269bb5Schristosdone 18544269bb5Schristos 1865ca12230Schristos# We allow the use of options -d and -T together, by making -d 1875ca12230Schristos# take the precedence; this is for compatibility with GNU install. 1885ca12230Schristos 1895ca12230Schristosif test -n "$dir_arg"; then 1905ca12230Schristos if test -n "$dst_arg"; then 1915ca12230Schristos echo "$0: target directory not allowed when installing a directory." >&2 1925ca12230Schristos exit 1 1935ca12230Schristos fi 1945ca12230Schristosfi 1955ca12230Schristos 19644269bb5Schristosif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 19744269bb5Schristos # When -d is used, all remaining arguments are directories to create. 19844269bb5Schristos # When -t is used, the destination is already specified. 19944269bb5Schristos # Otherwise, the last argument is the destination. Remove it from $@. 20044269bb5Schristos for arg 20144269bb5Schristos do 20244269bb5Schristos if test -n "$dst_arg"; then 20344269bb5Schristos # $@ is not empty: it contains at least $arg. 20444269bb5Schristos set fnord "$@" "$dst_arg" 20544269bb5Schristos shift # fnord 20644269bb5Schristos fi 20744269bb5Schristos shift # arg 20844269bb5Schristos dst_arg=$arg 2090bbc3b9fSchristos # Protect names problematic for 'test' and other utilities. 2100bbc3b9fSchristos case $dst_arg in 2110bbc3b9fSchristos -* | [=\(\)!]) dst_arg=./$dst_arg;; 2120bbc3b9fSchristos esac 21344269bb5Schristos done 21444269bb5Schristosfi 21544269bb5Schristos 21644269bb5Schristosif test $# -eq 0; then 21744269bb5Schristos if test -z "$dir_arg"; then 21844269bb5Schristos echo "$0: no input file specified." >&2 21944269bb5Schristos exit 1 22044269bb5Schristos fi 2210bbc3b9fSchristos # It's OK to call 'install-sh -d' without argument. 22244269bb5Schristos # This can happen when creating conditional directories. 22344269bb5Schristos exit 0 22444269bb5Schristosfi 22544269bb5Schristos 22644269bb5Schristosif test -z "$dir_arg"; then 2275ca12230Schristos if test $# -gt 1 || test "$is_target_a_directory" = always; then 2285ca12230Schristos if test ! -d "$dst_arg"; then 2295ca12230Schristos echo "$0: $dst_arg: Is not a directory." >&2 2305ca12230Schristos exit 1 2315ca12230Schristos fi 2325ca12230Schristos fi 2335ca12230Schristosfi 2345ca12230Schristos 2355ca12230Schristosif test -z "$dir_arg"; then 2360bbc3b9fSchristos do_exit='(exit $ret); exit $ret' 2370bbc3b9fSchristos trap "ret=129; $do_exit" 1 2380bbc3b9fSchristos trap "ret=130; $do_exit" 2 2390bbc3b9fSchristos trap "ret=141; $do_exit" 13 2400bbc3b9fSchristos trap "ret=143; $do_exit" 15 24144269bb5Schristos 24244269bb5Schristos # Set umask so as not to create temps with too-generous modes. 24344269bb5Schristos # However, 'strip' requires both read and write access to temps. 24444269bb5Schristos case $mode in 24544269bb5Schristos # Optimize common cases. 24644269bb5Schristos *644) cp_umask=133;; 24744269bb5Schristos *755) cp_umask=22;; 24844269bb5Schristos 24944269bb5Schristos *[0-7]) 25044269bb5Schristos if test -z "$stripcmd"; then 25144269bb5Schristos u_plus_rw= 25244269bb5Schristos else 25344269bb5Schristos u_plus_rw='% 200' 25444269bb5Schristos fi 25544269bb5Schristos cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 25644269bb5Schristos *) 25744269bb5Schristos if test -z "$stripcmd"; then 25844269bb5Schristos u_plus_rw= 25944269bb5Schristos else 26044269bb5Schristos u_plus_rw=,u+rw 26144269bb5Schristos fi 26244269bb5Schristos cp_umask=$mode$u_plus_rw;; 26344269bb5Schristos esac 26444269bb5Schristosfi 26544269bb5Schristos 26644269bb5Schristosfor src 26744269bb5Schristosdo 2680bbc3b9fSchristos # Protect names problematic for 'test' and other utilities. 26944269bb5Schristos case $src in 2700bbc3b9fSchristos -* | [=\(\)!]) src=./$src;; 27144269bb5Schristos esac 27244269bb5Schristos 27344269bb5Schristos if test -n "$dir_arg"; then 27444269bb5Schristos dst=$src 27544269bb5Schristos dstdir=$dst 27644269bb5Schristos test -d "$dstdir" 27744269bb5Schristos dstdir_status=$? 278*024a2e8cSchristos # Don't chown directories that already exist. 279*024a2e8cSchristos if test $dstdir_status = 0; then 280*024a2e8cSchristos chowncmd="" 281*024a2e8cSchristos fi 28244269bb5Schristos else 28344269bb5Schristos 28444269bb5Schristos # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 28544269bb5Schristos # might cause directories to be created, which would be especially bad 28644269bb5Schristos # if $src (and thus $dsttmp) contains '*'. 28744269bb5Schristos if test ! -f "$src" && test ! -d "$src"; then 28844269bb5Schristos echo "$0: $src does not exist." >&2 28944269bb5Schristos exit 1 29044269bb5Schristos fi 29144269bb5Schristos 29244269bb5Schristos if test -z "$dst_arg"; then 29344269bb5Schristos echo "$0: no destination specified." >&2 29444269bb5Schristos exit 1 29544269bb5Schristos fi 29644269bb5Schristos dst=$dst_arg 29744269bb5Schristos 298*024a2e8cSchristos # If destination is a directory, append the input filename. 29944269bb5Schristos if test -d "$dst"; then 3005ca12230Schristos if test "$is_target_a_directory" = never; then 30144269bb5Schristos echo "$0: $dst_arg: Is a directory" >&2 30244269bb5Schristos exit 1 30344269bb5Schristos fi 30444269bb5Schristos dstdir=$dst 305*024a2e8cSchristos dstbase=`basename "$src"` 306*024a2e8cSchristos case $dst in 307*024a2e8cSchristos */) dst=$dst$dstbase;; 308*024a2e8cSchristos *) dst=$dst/$dstbase;; 309*024a2e8cSchristos esac 31044269bb5Schristos dstdir_status=0 31144269bb5Schristos else 3125ca12230Schristos dstdir=`dirname "$dst"` 31344269bb5Schristos test -d "$dstdir" 31444269bb5Schristos dstdir_status=$? 31544269bb5Schristos fi 31644269bb5Schristos fi 31744269bb5Schristos 318*024a2e8cSchristos case $dstdir in 319*024a2e8cSchristos */) dstdirslash=$dstdir;; 320*024a2e8cSchristos *) dstdirslash=$dstdir/;; 321*024a2e8cSchristos esac 322*024a2e8cSchristos 32344269bb5Schristos obsolete_mkdir_used=false 32444269bb5Schristos 32544269bb5Schristos if test $dstdir_status != 0; then 32644269bb5Schristos case $posix_mkdir in 32744269bb5Schristos '') 32844269bb5Schristos # With -d, create the new directory with the user-specified mode. 32944269bb5Schristos # Otherwise, rely on $mkdir_umask. 33044269bb5Schristos if test -n "$dir_arg"; then 33144269bb5Schristos mkdir_mode=-m$mode 33244269bb5Schristos else 33344269bb5Schristos mkdir_mode= 33444269bb5Schristos fi 33544269bb5Schristos 33644269bb5Schristos posix_mkdir=false 337*024a2e8cSchristos # The $RANDOM variable is not portable (e.g., dash). Use it 338*024a2e8cSchristos # here however when possible just to lower collision chance. 33944269bb5Schristos tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 34044269bb5Schristos 341*024a2e8cSchristos trap ' 342*024a2e8cSchristos ret=$? 343*024a2e8cSchristos rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null 344*024a2e8cSchristos exit $ret 345*024a2e8cSchristos ' 0 346*024a2e8cSchristos 347*024a2e8cSchristos # Because "mkdir -p" follows existing symlinks and we likely work 348*024a2e8cSchristos # directly in world-writeable /tmp, make sure that the '$tmpdir' 349*024a2e8cSchristos # directory is successfully created first before we actually test 350*024a2e8cSchristos # 'mkdir -p'. 35144269bb5Schristos if (umask $mkdir_umask && 352*024a2e8cSchristos $mkdirprog $mkdir_mode "$tmpdir" && 353*024a2e8cSchristos exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1 35444269bb5Schristos then 35544269bb5Schristos if test -z "$dir_arg" || { 35644269bb5Schristos # Check for POSIX incompatibilities with -m. 35744269bb5Schristos # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 3580bbc3b9fSchristos # other-writable bit of parent directory when it shouldn't. 35944269bb5Schristos # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 360*024a2e8cSchristos test_tmpdir="$tmpdir/a" 361*024a2e8cSchristos ls_ld_tmpdir=`ls -ld "$test_tmpdir"` 36244269bb5Schristos case $ls_ld_tmpdir in 36344269bb5Schristos d????-?r-*) different_mode=700;; 36444269bb5Schristos d????-?--*) different_mode=755;; 36544269bb5Schristos *) false;; 36644269bb5Schristos esac && 367*024a2e8cSchristos $mkdirprog -m$different_mode -p -- "$test_tmpdir" && { 368*024a2e8cSchristos ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"` 36944269bb5Schristos test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 37044269bb5Schristos } 37144269bb5Schristos } 37244269bb5Schristos then posix_mkdir=: 37344269bb5Schristos fi 374*024a2e8cSchristos rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 37544269bb5Schristos else 37644269bb5Schristos # Remove any dirs left behind by ancient mkdir implementations. 377*024a2e8cSchristos rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null 37844269bb5Schristos fi 37944269bb5Schristos trap '' 0;; 38044269bb5Schristos esac 38144269bb5Schristos 38244269bb5Schristos if 38344269bb5Schristos $posix_mkdir && ( 38444269bb5Schristos umask $mkdir_umask && 38544269bb5Schristos $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 38644269bb5Schristos ) 38744269bb5Schristos then : 38844269bb5Schristos else 38944269bb5Schristos 390*024a2e8cSchristos # mkdir does not conform to POSIX, 39144269bb5Schristos # or it failed possibly due to a race condition. Create the 39244269bb5Schristos # directory the slow way, step by step, checking for races as we go. 39344269bb5Schristos 39444269bb5Schristos case $dstdir in 39544269bb5Schristos /*) prefix='/';; 3960bbc3b9fSchristos [-=\(\)!]*) prefix='./';; 39744269bb5Schristos *) prefix='';; 39844269bb5Schristos esac 39944269bb5Schristos 40044269bb5Schristos oIFS=$IFS 40144269bb5Schristos IFS=/ 4025ca12230Schristos set -f 40344269bb5Schristos set fnord $dstdir 40444269bb5Schristos shift 4055ca12230Schristos set +f 40644269bb5Schristos IFS=$oIFS 40744269bb5Schristos 40844269bb5Schristos prefixes= 40944269bb5Schristos 41044269bb5Schristos for d 41144269bb5Schristos do 4120bbc3b9fSchristos test X"$d" = X && continue 41344269bb5Schristos 41444269bb5Schristos prefix=$prefix$d 41544269bb5Schristos if test -d "$prefix"; then 41644269bb5Schristos prefixes= 41744269bb5Schristos else 41844269bb5Schristos if $posix_mkdir; then 419*024a2e8cSchristos (umask $mkdir_umask && 42044269bb5Schristos $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 42144269bb5Schristos # Don't fail if two instances are running concurrently. 42244269bb5Schristos test -d "$prefix" || exit 1 42344269bb5Schristos else 42444269bb5Schristos case $prefix in 42544269bb5Schristos *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 42644269bb5Schristos *) qprefix=$prefix;; 42744269bb5Schristos esac 42844269bb5Schristos prefixes="$prefixes '$qprefix'" 42944269bb5Schristos fi 43044269bb5Schristos fi 43144269bb5Schristos prefix=$prefix/ 43244269bb5Schristos done 43344269bb5Schristos 43444269bb5Schristos if test -n "$prefixes"; then 43544269bb5Schristos # Don't fail if two instances are running concurrently. 43644269bb5Schristos (umask $mkdir_umask && 43744269bb5Schristos eval "\$doit_exec \$mkdirprog $prefixes") || 43844269bb5Schristos test -d "$dstdir" || exit 1 43944269bb5Schristos obsolete_mkdir_used=true 44044269bb5Schristos fi 44144269bb5Schristos fi 44244269bb5Schristos fi 44344269bb5Schristos 44444269bb5Schristos if test -n "$dir_arg"; then 44544269bb5Schristos { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 44644269bb5Schristos { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 44744269bb5Schristos { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 44844269bb5Schristos test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 44944269bb5Schristos else 45044269bb5Schristos 45144269bb5Schristos # Make a couple of temp file names in the proper directory. 452*024a2e8cSchristos dsttmp=${dstdirslash}_inst.$$_ 453*024a2e8cSchristos rmtmp=${dstdirslash}_rm.$$_ 45444269bb5Schristos 45544269bb5Schristos # Trap to clean up those temp files at exit. 45644269bb5Schristos trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 45744269bb5Schristos 45844269bb5Schristos # Copy the file name to the temp name. 459*024a2e8cSchristos (umask $cp_umask && 460*024a2e8cSchristos { test -z "$stripcmd" || { 461*024a2e8cSchristos # Create $dsttmp read-write so that cp doesn't create it read-only, 462*024a2e8cSchristos # which would cause strip to fail. 463*024a2e8cSchristos if test -z "$doit"; then 464*024a2e8cSchristos : >"$dsttmp" # No need to fork-exec 'touch'. 465*024a2e8cSchristos else 466*024a2e8cSchristos $doit touch "$dsttmp" 467*024a2e8cSchristos fi 468*024a2e8cSchristos } 469*024a2e8cSchristos } && 470*024a2e8cSchristos $doit_exec $cpprog "$src" "$dsttmp") && 47144269bb5Schristos 47244269bb5Schristos # and set any options; do chmod last to preserve setuid bits. 47344269bb5Schristos # 47444269bb5Schristos # If any of these fail, we abort the whole thing. If we want to 47544269bb5Schristos # ignore errors from any of these, just make sure not to ignore 47644269bb5Schristos # errors from the above "$doit $cpprog $src $dsttmp" command. 47744269bb5Schristos # 47844269bb5Schristos { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 47944269bb5Schristos { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 48044269bb5Schristos { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 48144269bb5Schristos { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 48244269bb5Schristos 48344269bb5Schristos # If -C, don't bother to copy if it wouldn't change the file. 48444269bb5Schristos if $copy_on_change && 48544269bb5Schristos old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 48644269bb5Schristos new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 4875ca12230Schristos set -f && 48844269bb5Schristos set X $old && old=:$2:$4:$5:$6 && 48944269bb5Schristos set X $new && new=:$2:$4:$5:$6 && 4905ca12230Schristos set +f && 49144269bb5Schristos test "$old" = "$new" && 49244269bb5Schristos $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 49344269bb5Schristos then 49444269bb5Schristos rm -f "$dsttmp" 49544269bb5Schristos else 496*024a2e8cSchristos # If $backupsuffix is set, and the file being installed 497*024a2e8cSchristos # already exists, attempt a backup. Don't worry if it fails, 498*024a2e8cSchristos # e.g., if mv doesn't support -f. 499*024a2e8cSchristos if test -n "$backupsuffix" && test -f "$dst"; then 500*024a2e8cSchristos $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null 501*024a2e8cSchristos fi 502*024a2e8cSchristos 50344269bb5Schristos # Rename the file to the real destination. 50444269bb5Schristos $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 50544269bb5Schristos 50644269bb5Schristos # The rename failed, perhaps because mv can't rename something else 50744269bb5Schristos # to itself, or perhaps because mv is so ancient that it does not 50844269bb5Schristos # support -f. 50944269bb5Schristos { 51044269bb5Schristos # Now remove or move aside any old file at destination location. 51144269bb5Schristos # We try this two ways since rm can't unlink itself on some 51244269bb5Schristos # systems and the destination file might be busy for other 51344269bb5Schristos # reasons. In this case, the final cleanup might fail but the new 51444269bb5Schristos # file should still install successfully. 51544269bb5Schristos { 51644269bb5Schristos test ! -f "$dst" || 517*024a2e8cSchristos $doit $rmcmd "$dst" 2>/dev/null || 51844269bb5Schristos { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 519*024a2e8cSchristos { $doit $rmcmd "$rmtmp" 2>/dev/null; :; } 52044269bb5Schristos } || 52144269bb5Schristos { echo "$0: cannot unlink or rename $dst" >&2 52244269bb5Schristos (exit 1); exit 1 52344269bb5Schristos } 52444269bb5Schristos } && 52544269bb5Schristos 52644269bb5Schristos # Now rename the file to the real destination. 52744269bb5Schristos $doit $mvcmd "$dsttmp" "$dst" 52844269bb5Schristos } 52944269bb5Schristos fi || exit 1 53044269bb5Schristos 53144269bb5Schristos trap '' 0 53244269bb5Schristos fi 53344269bb5Schristosdone 53444269bb5Schristos 53544269bb5Schristos# Local variables: 536*024a2e8cSchristos# eval: (add-hook 'before-save-hook 'time-stamp) 53744269bb5Schristos# time-stamp-start: "scriptversion=" 53844269bb5Schristos# time-stamp-format: "%:y-%02m-%02d.%02H" 53977513ecfSchristos# time-stamp-time-zone: "UTC0" 54044269bb5Schristos# time-stamp-end: "; # UTC" 54144269bb5Schristos# End: 542