install-sh revision 165cb819
1100ae103Smrg#!/bin/sh 2100ae103Smrg# install - install a program, script, or datafile 3100ae103Smrg 4165cb819Smrgscriptversion=2009-04-28.21; # 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 38100ae103Smrg# `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 44165cb819Smrgnl=' 45165cb819Smrg' 46165cb819SmrgIFS=" "" $nl" 47100ae103Smrg 48100ae103Smrg# set DOITPROG to echo to test this script 49100ae103Smrg 50100ae103Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 51165cb819Smrgdoit=${DOITPROG-} 52165cb819Smrgif test -z "$doit"; then 53165cb819Smrg doit_exec=exec 54165cb819Smrgelse 55165cb819Smrg doit_exec=$doit 56165cb819Smrgfi 57100ae103Smrg 58165cb819Smrg# Put in absolute file names if you don't have them in your path; 59165cb819Smrg# or use environment vars. 60165cb819Smrg 61165cb819Smrgchgrpprog=${CHGRPPROG-chgrp} 62165cb819Smrgchmodprog=${CHMODPROG-chmod} 63165cb819Smrgchownprog=${CHOWNPROG-chown} 64165cb819Smrgcmpprog=${CMPPROG-cmp} 65165cb819Smrgcpprog=${CPPROG-cp} 66165cb819Smrgmkdirprog=${MKDIRPROG-mkdir} 67165cb819Smrgmvprog=${MVPROG-mv} 68165cb819Smrgrmprog=${RMPROG-rm} 69165cb819Smrgstripprog=${STRIPPROG-strip} 70165cb819Smrg 71165cb819Smrgposix_glob='?' 72165cb819Smrginitialize_posix_glob=' 73165cb819Smrg test "$posix_glob" != "?" || { 74165cb819Smrg if (set -f) 2>/dev/null; then 75165cb819Smrg posix_glob= 76165cb819Smrg else 77165cb819Smrg posix_glob=: 78165cb819Smrg fi 79165cb819Smrg } 80165cb819Smrg' 81100ae103Smrg 82165cb819Smrgposix_mkdir= 83165cb819Smrg 84165cb819Smrg# Desired mode of installed file. 85165cb819Smrgmode=0755 86100ae103Smrg 87100ae103Smrgchgrpcmd= 88165cb819Smrgchmodcmd=$chmodprog 89165cb819Smrgchowncmd= 90165cb819Smrgmvcmd=$mvprog 91100ae103Smrgrmcmd="$rmprog -f" 92165cb819Smrgstripcmd= 93165cb819Smrg 94100ae103Smrgsrc= 95100ae103Smrgdst= 96100ae103Smrgdir_arg= 97165cb819Smrgdst_arg= 98165cb819Smrg 99165cb819Smrgcopy_on_change=false 100100ae103Smrgno_target_directory= 101100ae103Smrg 102165cb819Smrgusage="\ 103165cb819SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 104100ae103Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 105100ae103Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 106100ae103Smrg or: $0 [OPTION]... -d DIRECTORIES... 107100ae103Smrg 108100ae103SmrgIn the 1st form, copy SRCFILE to DSTFILE. 109100ae103SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 110100ae103SmrgIn the 4th, create DIRECTORIES. 111100ae103Smrg 112100ae103SmrgOptions: 113165cb819Smrg --help display this help and exit. 114165cb819Smrg --version display version info and exit. 115165cb819Smrg 116165cb819Smrg -c (ignored) 117165cb819Smrg -C install only if different (preserve the last data modification time) 118165cb819Smrg -d create directories instead of installing files. 119165cb819Smrg -g GROUP $chgrpprog installed files to GROUP. 120165cb819Smrg -m MODE $chmodprog installed files to MODE. 121165cb819Smrg -o USER $chownprog installed files to USER. 122165cb819Smrg -s $stripprog installed files. 123165cb819Smrg -t DIRECTORY install into DIRECTORY. 124165cb819Smrg -T report an error if DSTFILE is a directory. 125100ae103Smrg 126100ae103SmrgEnvironment variables override the default commands: 127165cb819Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 128165cb819Smrg RMPROG STRIPPROG 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" 140165cb819Smrg shift;; 141100ae103Smrg 142100ae103Smrg --help) echo "$usage"; exit $?;; 143100ae103Smrg 144165cb819Smrg -m) mode=$2 145165cb819Smrg case $mode in 146165cb819Smrg *' '* | *' '* | *' 147165cb819Smrg'* | *'*'* | *'?'* | *'['*) 148165cb819Smrg echo "$0: invalid mode: $mode" >&2 149165cb819Smrg exit 1;; 150165cb819Smrg esac 151165cb819Smrg shift;; 152100ae103Smrg 153100ae103Smrg -o) chowncmd="$chownprog $2" 154165cb819Smrg shift;; 155100ae103Smrg 156165cb819Smrg -s) stripcmd=$stripprog;; 157100ae103Smrg 158165cb819Smrg -t) dst_arg=$2 159165cb819Smrg shift;; 160100ae103Smrg 161165cb819Smrg -T) no_target_directory=true;; 162100ae103Smrg 163100ae103Smrg --version) echo "$0 $scriptversion"; exit $?;; 164100ae103Smrg 165165cb819Smrg --) shift 166100ae103Smrg break;; 167165cb819Smrg 168165cb819Smrg -*) echo "$0: invalid option: $1" >&2 169165cb819Smrg exit 1;; 170165cb819Smrg 171165cb819Smrg *) break;; 172100ae103Smrg esac 173165cb819Smrg shift 174100ae103Smrgdone 175100ae103Smrg 176165cb819Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 177165cb819Smrg # When -d is used, all remaining arguments are directories to create. 178165cb819Smrg # When -t is used, the destination is already specified. 179165cb819Smrg # Otherwise, the last argument is the destination. Remove it from $@. 180165cb819Smrg for arg 181165cb819Smrg do 182165cb819Smrg if test -n "$dst_arg"; then 183165cb819Smrg # $@ is not empty: it contains at least $arg. 184165cb819Smrg set fnord "$@" "$dst_arg" 185165cb819Smrg shift # fnord 186165cb819Smrg fi 187165cb819Smrg shift # arg 188165cb819Smrg dst_arg=$arg 189165cb819Smrg done 190165cb819Smrgfi 191165cb819Smrg 192165cb819Smrgif test $# -eq 0; then 193100ae103Smrg if test -z "$dir_arg"; then 194100ae103Smrg echo "$0: no input file specified." >&2 195100ae103Smrg exit 1 196100ae103Smrg fi 197100ae103Smrg # It's OK to call `install-sh -d' without argument. 198100ae103Smrg # This can happen when creating conditional directories. 199100ae103Smrg exit 0 200100ae103Smrgfi 201100ae103Smrg 202165cb819Smrgif test -z "$dir_arg"; then 203165cb819Smrg trap '(exit $?); exit' 1 2 13 15 204165cb819Smrg 205165cb819Smrg # Set umask so as not to create temps with too-generous modes. 206165cb819Smrg # However, 'strip' requires both read and write access to temps. 207165cb819Smrg case $mode in 208165cb819Smrg # Optimize common cases. 209165cb819Smrg *644) cp_umask=133;; 210165cb819Smrg *755) cp_umask=22;; 211165cb819Smrg 212165cb819Smrg *[0-7]) 213165cb819Smrg if test -z "$stripcmd"; then 214165cb819Smrg u_plus_rw= 215165cb819Smrg else 216165cb819Smrg u_plus_rw='% 200' 217165cb819Smrg fi 218165cb819Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 219165cb819Smrg *) 220165cb819Smrg if test -z "$stripcmd"; then 221165cb819Smrg u_plus_rw= 222165cb819Smrg else 223165cb819Smrg u_plus_rw=,u+rw 224165cb819Smrg fi 225165cb819Smrg cp_umask=$mode$u_plus_rw;; 226165cb819Smrg esac 227165cb819Smrgfi 228165cb819Smrg 229100ae103Smrgfor src 230100ae103Smrgdo 231100ae103Smrg # Protect names starting with `-'. 232100ae103Smrg case $src in 233165cb819Smrg -*) src=./$src;; 234100ae103Smrg esac 235100ae103Smrg 236100ae103Smrg if test -n "$dir_arg"; then 237100ae103Smrg dst=$src 238165cb819Smrg dstdir=$dst 239165cb819Smrg test -d "$dstdir" 240165cb819Smrg dstdir_status=$? 241100ae103Smrg else 242165cb819Smrg 243100ae103Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 244100ae103Smrg # might cause directories to be created, which would be especially bad 245100ae103Smrg # if $src (and thus $dsttmp) contains '*'. 246100ae103Smrg if test ! -f "$src" && test ! -d "$src"; then 247100ae103Smrg echo "$0: $src does not exist." >&2 248100ae103Smrg exit 1 249100ae103Smrg fi 250100ae103Smrg 251165cb819Smrg if test -z "$dst_arg"; then 252100ae103Smrg echo "$0: no destination specified." >&2 253100ae103Smrg exit 1 254100ae103Smrg fi 255100ae103Smrg 256165cb819Smrg dst=$dst_arg 257100ae103Smrg # Protect names starting with `-'. 258100ae103Smrg case $dst in 259165cb819Smrg -*) dst=./$dst;; 260100ae103Smrg esac 261100ae103Smrg 262100ae103Smrg # If destination is a directory, append the input filename; won't work 263100ae103Smrg # if double slashes aren't ignored. 264100ae103Smrg if test -d "$dst"; then 265100ae103Smrg if test -n "$no_target_directory"; then 266165cb819Smrg echo "$0: $dst_arg: Is a directory" >&2 267100ae103Smrg exit 1 268100ae103Smrg fi 269165cb819Smrg dstdir=$dst 270165cb819Smrg dst=$dstdir/`basename "$src"` 271165cb819Smrg dstdir_status=0 272165cb819Smrg else 273165cb819Smrg # Prefer dirname, but fall back on a substitute if dirname fails. 274165cb819Smrg dstdir=` 275165cb819Smrg (dirname "$dst") 2>/dev/null || 276165cb819Smrg expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 277165cb819Smrg X"$dst" : 'X\(//\)[^/]' \| \ 278165cb819Smrg X"$dst" : 'X\(//\)$' \| \ 279165cb819Smrg X"$dst" : 'X\(/\)' \| . 2>/dev/null || 280165cb819Smrg echo X"$dst" | 281165cb819Smrg sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 282165cb819Smrg s//\1/ 283165cb819Smrg q 284165cb819Smrg } 285165cb819Smrg /^X\(\/\/\)[^/].*/{ 286165cb819Smrg s//\1/ 287165cb819Smrg q 288165cb819Smrg } 289165cb819Smrg /^X\(\/\/\)$/{ 290165cb819Smrg s//\1/ 291165cb819Smrg q 292165cb819Smrg } 293165cb819Smrg /^X\(\/\).*/{ 294165cb819Smrg s//\1/ 295165cb819Smrg q 296165cb819Smrg } 297165cb819Smrg s/.*/./; q' 298165cb819Smrg ` 299165cb819Smrg 300165cb819Smrg test -d "$dstdir" 301165cb819Smrg dstdir_status=$? 302100ae103Smrg fi 303100ae103Smrg fi 304100ae103Smrg 305165cb819Smrg obsolete_mkdir_used=false 306165cb819Smrg 307165cb819Smrg if test $dstdir_status != 0; then 308165cb819Smrg case $posix_mkdir in 309165cb819Smrg '') 310165cb819Smrg # Create intermediate dirs using mode 755 as modified by the umask. 311165cb819Smrg # This is like FreeBSD 'install' as of 1997-10-28. 312165cb819Smrg umask=`umask` 313165cb819Smrg case $stripcmd.$umask in 314165cb819Smrg # Optimize common cases. 315165cb819Smrg *[2367][2367]) mkdir_umask=$umask;; 316165cb819Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 317165cb819Smrg 318165cb819Smrg *[0-7]) 319165cb819Smrg mkdir_umask=`expr $umask + 22 \ 320165cb819Smrg - $umask % 100 % 40 + $umask % 20 \ 321165cb819Smrg - $umask % 10 % 4 + $umask % 2 322165cb819Smrg `;; 323165cb819Smrg *) mkdir_umask=$umask,go-w;; 324165cb819Smrg esac 325165cb819Smrg 326165cb819Smrg # With -d, create the new directory with the user-specified mode. 327165cb819Smrg # Otherwise, rely on $mkdir_umask. 328165cb819Smrg if test -n "$dir_arg"; then 329165cb819Smrg mkdir_mode=-m$mode 330165cb819Smrg else 331165cb819Smrg mkdir_mode= 332165cb819Smrg fi 333165cb819Smrg 334165cb819Smrg posix_mkdir=false 335165cb819Smrg case $umask in 336165cb819Smrg *[123567][0-7][0-7]) 337165cb819Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 338165cb819Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 339165cb819Smrg ;; 340165cb819Smrg *) 341165cb819Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 342165cb819Smrg trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 343165cb819Smrg 344165cb819Smrg if (umask $mkdir_umask && 345165cb819Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 346165cb819Smrg then 347165cb819Smrg if test -z "$dir_arg" || { 348165cb819Smrg # Check for POSIX incompatibilities with -m. 349165cb819Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 350165cb819Smrg # other-writeable bit of parent directory when it shouldn't. 351165cb819Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 352165cb819Smrg ls_ld_tmpdir=`ls -ld "$tmpdir"` 353165cb819Smrg case $ls_ld_tmpdir in 354165cb819Smrg d????-?r-*) different_mode=700;; 355165cb819Smrg d????-?--*) different_mode=755;; 356165cb819Smrg *) false;; 357165cb819Smrg esac && 358165cb819Smrg $mkdirprog -m$different_mode -p -- "$tmpdir" && { 359165cb819Smrg ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 360165cb819Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 361165cb819Smrg } 362165cb819Smrg } 363165cb819Smrg then posix_mkdir=: 364165cb819Smrg fi 365165cb819Smrg rmdir "$tmpdir/d" "$tmpdir" 366165cb819Smrg else 367165cb819Smrg # Remove any dirs left behind by ancient mkdir implementations. 368165cb819Smrg rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 369165cb819Smrg fi 370165cb819Smrg trap '' 0;; 371165cb819Smrg esac;; 372165cb819Smrg esac 373100ae103Smrg 374165cb819Smrg if 375165cb819Smrg $posix_mkdir && ( 376165cb819Smrg umask $mkdir_umask && 377165cb819Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 378165cb819Smrg ) 379165cb819Smrg then : 380165cb819Smrg else 381100ae103Smrg 382165cb819Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 383165cb819Smrg # or it failed possibly due to a race condition. Create the 384165cb819Smrg # directory the slow way, step by step, checking for races as we go. 385100ae103Smrg 386165cb819Smrg case $dstdir in 387165cb819Smrg /*) prefix='/';; 388165cb819Smrg -*) prefix='./';; 389165cb819Smrg *) prefix='';; 390165cb819Smrg esac 391100ae103Smrg 392165cb819Smrg eval "$initialize_posix_glob" 393100ae103Smrg 394165cb819Smrg oIFS=$IFS 395165cb819Smrg IFS=/ 396165cb819Smrg $posix_glob set -f 397165cb819Smrg set fnord $dstdir 398100ae103Smrg shift 399165cb819Smrg $posix_glob set +f 400165cb819Smrg IFS=$oIFS 401165cb819Smrg 402165cb819Smrg prefixes= 403165cb819Smrg 404165cb819Smrg for d 405165cb819Smrg do 406165cb819Smrg test -z "$d" && continue 407165cb819Smrg 408165cb819Smrg prefix=$prefix$d 409165cb819Smrg if test -d "$prefix"; then 410165cb819Smrg prefixes= 411165cb819Smrg else 412165cb819Smrg if $posix_mkdir; then 413165cb819Smrg (umask=$mkdir_umask && 414165cb819Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 415165cb819Smrg # Don't fail if two instances are running concurrently. 416165cb819Smrg test -d "$prefix" || exit 1 417165cb819Smrg else 418165cb819Smrg case $prefix in 419165cb819Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 420165cb819Smrg *) qprefix=$prefix;; 421165cb819Smrg esac 422165cb819Smrg prefixes="$prefixes '$qprefix'" 423165cb819Smrg fi 424165cb819Smrg fi 425165cb819Smrg prefix=$prefix/ 426165cb819Smrg done 427165cb819Smrg 428165cb819Smrg if test -n "$prefixes"; then 429165cb819Smrg # Don't fail if two instances are running concurrently. 430165cb819Smrg (umask $mkdir_umask && 431165cb819Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 432165cb819Smrg test -d "$dstdir" || exit 1 433165cb819Smrg obsolete_mkdir_used=true 434100ae103Smrg fi 435165cb819Smrg fi 436100ae103Smrg fi 437100ae103Smrg 438100ae103Smrg if test -n "$dir_arg"; then 439165cb819Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 440165cb819Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 441165cb819Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 442165cb819Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 443100ae103Smrg else 444100ae103Smrg 445100ae103Smrg # Make a couple of temp file names in the proper directory. 446100ae103Smrg dsttmp=$dstdir/_inst.$$_ 447100ae103Smrg rmtmp=$dstdir/_rm.$$_ 448100ae103Smrg 449100ae103Smrg # Trap to clean up those temp files at exit. 450100ae103Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 451100ae103Smrg 452100ae103Smrg # Copy the file name to the temp name. 453165cb819Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 454100ae103Smrg 455100ae103Smrg # and set any options; do chmod last to preserve setuid bits. 456100ae103Smrg # 457100ae103Smrg # If any of these fail, we abort the whole thing. If we want to 458100ae103Smrg # ignore errors from any of these, just make sure not to ignore 459100ae103Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 460100ae103Smrg # 461165cb819Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 462165cb819Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 463165cb819Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 464165cb819Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 465165cb819Smrg 466165cb819Smrg # If -C, don't bother to copy if it wouldn't change the file. 467165cb819Smrg if $copy_on_change && 468165cb819Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 469165cb819Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 470165cb819Smrg 471165cb819Smrg eval "$initialize_posix_glob" && 472165cb819Smrg $posix_glob set -f && 473165cb819Smrg set X $old && old=:$2:$4:$5:$6 && 474165cb819Smrg set X $new && new=:$2:$4:$5:$6 && 475165cb819Smrg $posix_glob set +f && 476165cb819Smrg 477165cb819Smrg test "$old" = "$new" && 478165cb819Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 479165cb819Smrg then 480165cb819Smrg rm -f "$dsttmp" 481165cb819Smrg else 482165cb819Smrg # Rename the file to the real destination. 483165cb819Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 484165cb819Smrg 485165cb819Smrg # The rename failed, perhaps because mv can't rename something else 486165cb819Smrg # to itself, or perhaps because mv is so ancient that it does not 487165cb819Smrg # support -f. 488165cb819Smrg { 489165cb819Smrg # Now remove or move aside any old file at destination location. 490165cb819Smrg # We try this two ways since rm can't unlink itself on some 491165cb819Smrg # systems and the destination file might be busy for other 492165cb819Smrg # reasons. In this case, the final cleanup might fail but the new 493165cb819Smrg # file should still install successfully. 494165cb819Smrg { 495165cb819Smrg test ! -f "$dst" || 496165cb819Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 497165cb819Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 498165cb819Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 499165cb819Smrg } || 500165cb819Smrg { echo "$0: cannot unlink or rename $dst" >&2 501165cb819Smrg (exit 1); exit 1 502165cb819Smrg } 503165cb819Smrg } && 504165cb819Smrg 505165cb819Smrg # Now rename the file to the real destination. 506165cb819Smrg $doit $mvcmd "$dsttmp" "$dst" 507165cb819Smrg } 508165cb819Smrg fi || exit 1 509165cb819Smrg 510165cb819Smrg trap '' 0 511165cb819Smrg fi 512100ae103Smrgdone 513100ae103Smrg 514100ae103Smrg# Local variables: 515100ae103Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 516100ae103Smrg# time-stamp-start: "scriptversion=" 517100ae103Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 518165cb819Smrg# time-stamp-time-zone: "UTC" 519165cb819Smrg# time-stamp-end: "; # UTC" 520100ae103Smrg# End: 521