install-sh revision 3da084b3
13da084b3Smrg#!/bin/sh 23da084b3Smrg# install - install a program, script, or datafile 33da084b3Smrg 43da084b3Smrgscriptversion=2005-05-14.22 53da084b3Smrg 63da084b3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 73da084b3Smrg# later released in X11R6 (xc/config/util/install.sh) with the 83da084b3Smrg# following copyright and license. 93da084b3Smrg# 103da084b3Smrg# Copyright (C) 1994 X Consortium 113da084b3Smrg# 123da084b3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 133da084b3Smrg# of this software and associated documentation files (the "Software"), to 143da084b3Smrg# deal in the Software without restriction, including without limitation the 153da084b3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 163da084b3Smrg# sell copies of the Software, and to permit persons to whom the Software is 173da084b3Smrg# furnished to do so, subject to the following conditions: 183da084b3Smrg# 193da084b3Smrg# The above copyright notice and this permission notice shall be included in 203da084b3Smrg# all copies or substantial portions of the Software. 213da084b3Smrg# 223da084b3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 233da084b3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 243da084b3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 253da084b3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 263da084b3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 273da084b3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 283da084b3Smrg# 293da084b3Smrg# Except as contained in this notice, the name of the X Consortium shall not 303da084b3Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 313da084b3Smrg# ings in this Software without prior written authorization from the X Consor- 323da084b3Smrg# tium. 333da084b3Smrg# 343da084b3Smrg# 353da084b3Smrg# FSF changes to this file are in the public domain. 363da084b3Smrg# 373da084b3Smrg# Calling this script install-sh is preferred over install.sh, to prevent 383da084b3Smrg# `make' implicit rules from creating a file called install from it 393da084b3Smrg# when there is no Makefile. 403da084b3Smrg# 413da084b3Smrg# This script is compatible with the BSD install script, but was written 423da084b3Smrg# from scratch. It can only install one file at a time, a restriction 433da084b3Smrg# shared with many OS's install programs. 443da084b3Smrg 453da084b3Smrg# set DOITPROG to echo to test this script 463da084b3Smrg 473da084b3Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 483da084b3Smrgdoit="${DOITPROG-}" 493da084b3Smrg 503da084b3Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 513da084b3Smrg 523da084b3Smrgmvprog="${MVPROG-mv}" 533da084b3Smrgcpprog="${CPPROG-cp}" 543da084b3Smrgchmodprog="${CHMODPROG-chmod}" 553da084b3Smrgchownprog="${CHOWNPROG-chown}" 563da084b3Smrgchgrpprog="${CHGRPPROG-chgrp}" 573da084b3Smrgstripprog="${STRIPPROG-strip}" 583da084b3Smrgrmprog="${RMPROG-rm}" 593da084b3Smrgmkdirprog="${MKDIRPROG-mkdir}" 603da084b3Smrg 613da084b3Smrgchmodcmd="$chmodprog 0755" 623da084b3Smrgchowncmd= 633da084b3Smrgchgrpcmd= 643da084b3Smrgstripcmd= 653da084b3Smrgrmcmd="$rmprog -f" 663da084b3Smrgmvcmd="$mvprog" 673da084b3Smrgsrc= 683da084b3Smrgdst= 693da084b3Smrgdir_arg= 703da084b3Smrgdstarg= 713da084b3Smrgno_target_directory= 723da084b3Smrg 733da084b3Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 743da084b3Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 753da084b3Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 763da084b3Smrg or: $0 [OPTION]... -d DIRECTORIES... 773da084b3Smrg 783da084b3SmrgIn the 1st form, copy SRCFILE to DSTFILE. 793da084b3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 803da084b3SmrgIn the 4th, create DIRECTORIES. 813da084b3Smrg 823da084b3SmrgOptions: 833da084b3Smrg-c (ignored) 843da084b3Smrg-d create directories instead of installing files. 853da084b3Smrg-g GROUP $chgrpprog installed files to GROUP. 863da084b3Smrg-m MODE $chmodprog installed files to MODE. 873da084b3Smrg-o USER $chownprog installed files to USER. 883da084b3Smrg-s $stripprog installed files. 893da084b3Smrg-t DIRECTORY install into DIRECTORY. 903da084b3Smrg-T report an error if DSTFILE is a directory. 913da084b3Smrg--help display this help and exit. 923da084b3Smrg--version display version info and exit. 933da084b3Smrg 943da084b3SmrgEnvironment variables override the default commands: 953da084b3Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 963da084b3Smrg" 973da084b3Smrg 983da084b3Smrgwhile test -n "$1"; do 993da084b3Smrg case $1 in 1003da084b3Smrg -c) shift 1013da084b3Smrg continue;; 1023da084b3Smrg 1033da084b3Smrg -d) dir_arg=true 1043da084b3Smrg shift 1053da084b3Smrg continue;; 1063da084b3Smrg 1073da084b3Smrg -g) chgrpcmd="$chgrpprog $2" 1083da084b3Smrg shift 1093da084b3Smrg shift 1103da084b3Smrg continue;; 1113da084b3Smrg 1123da084b3Smrg --help) echo "$usage"; exit $?;; 1133da084b3Smrg 1143da084b3Smrg -m) chmodcmd="$chmodprog $2" 1153da084b3Smrg shift 1163da084b3Smrg shift 1173da084b3Smrg continue;; 1183da084b3Smrg 1193da084b3Smrg -o) chowncmd="$chownprog $2" 1203da084b3Smrg shift 1213da084b3Smrg shift 1223da084b3Smrg continue;; 1233da084b3Smrg 1243da084b3Smrg -s) stripcmd=$stripprog 1253da084b3Smrg shift 1263da084b3Smrg continue;; 1273da084b3Smrg 1283da084b3Smrg -t) dstarg=$2 1293da084b3Smrg shift 1303da084b3Smrg shift 1313da084b3Smrg continue;; 1323da084b3Smrg 1333da084b3Smrg -T) no_target_directory=true 1343da084b3Smrg shift 1353da084b3Smrg continue;; 1363da084b3Smrg 1373da084b3Smrg --version) echo "$0 $scriptversion"; exit $?;; 1383da084b3Smrg 1393da084b3Smrg *) # When -d is used, all remaining arguments are directories to create. 1403da084b3Smrg # When -t is used, the destination is already specified. 1413da084b3Smrg test -n "$dir_arg$dstarg" && break 1423da084b3Smrg # Otherwise, the last argument is the destination. Remove it from $@. 1433da084b3Smrg for arg 1443da084b3Smrg do 1453da084b3Smrg if test -n "$dstarg"; then 1463da084b3Smrg # $@ is not empty: it contains at least $arg. 1473da084b3Smrg set fnord "$@" "$dstarg" 1483da084b3Smrg shift # fnord 1493da084b3Smrg fi 1503da084b3Smrg shift # arg 1513da084b3Smrg dstarg=$arg 1523da084b3Smrg done 1533da084b3Smrg break;; 1543da084b3Smrg esac 1553da084b3Smrgdone 1563da084b3Smrg 1573da084b3Smrgif test -z "$1"; then 1583da084b3Smrg if test -z "$dir_arg"; then 1593da084b3Smrg echo "$0: no input file specified." >&2 1603da084b3Smrg exit 1 1613da084b3Smrg fi 1623da084b3Smrg # It's OK to call `install-sh -d' without argument. 1633da084b3Smrg # This can happen when creating conditional directories. 1643da084b3Smrg exit 0 1653da084b3Smrgfi 1663da084b3Smrg 1673da084b3Smrgfor src 1683da084b3Smrgdo 1693da084b3Smrg # Protect names starting with `-'. 1703da084b3Smrg case $src in 1713da084b3Smrg -*) src=./$src ;; 1723da084b3Smrg esac 1733da084b3Smrg 1743da084b3Smrg if test -n "$dir_arg"; then 1753da084b3Smrg dst=$src 1763da084b3Smrg src= 1773da084b3Smrg 1783da084b3Smrg if test -d "$dst"; then 1793da084b3Smrg mkdircmd=: 1803da084b3Smrg chmodcmd= 1813da084b3Smrg else 1823da084b3Smrg mkdircmd=$mkdirprog 1833da084b3Smrg fi 1843da084b3Smrg else 1853da084b3Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 1863da084b3Smrg # might cause directories to be created, which would be especially bad 1873da084b3Smrg # if $src (and thus $dsttmp) contains '*'. 1883da084b3Smrg if test ! -f "$src" && test ! -d "$src"; then 1893da084b3Smrg echo "$0: $src does not exist." >&2 1903da084b3Smrg exit 1 1913da084b3Smrg fi 1923da084b3Smrg 1933da084b3Smrg if test -z "$dstarg"; then 1943da084b3Smrg echo "$0: no destination specified." >&2 1953da084b3Smrg exit 1 1963da084b3Smrg fi 1973da084b3Smrg 1983da084b3Smrg dst=$dstarg 1993da084b3Smrg # Protect names starting with `-'. 2003da084b3Smrg case $dst in 2013da084b3Smrg -*) dst=./$dst ;; 2023da084b3Smrg esac 2033da084b3Smrg 2043da084b3Smrg # If destination is a directory, append the input filename; won't work 2053da084b3Smrg # if double slashes aren't ignored. 2063da084b3Smrg if test -d "$dst"; then 2073da084b3Smrg if test -n "$no_target_directory"; then 2083da084b3Smrg echo "$0: $dstarg: Is a directory" >&2 2093da084b3Smrg exit 1 2103da084b3Smrg fi 2113da084b3Smrg dst=$dst/`basename "$src"` 2123da084b3Smrg fi 2133da084b3Smrg fi 2143da084b3Smrg 2153da084b3Smrg # This sed command emulates the dirname command. 2163da084b3Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 2173da084b3Smrg 2183da084b3Smrg # Make sure that the destination directory exists. 2193da084b3Smrg 2203da084b3Smrg # Skip lots of stat calls in the usual case. 2213da084b3Smrg if test ! -d "$dstdir"; then 2223da084b3Smrg defaultIFS=' 2233da084b3Smrg ' 2243da084b3Smrg IFS="${IFS-$defaultIFS}" 2253da084b3Smrg 2263da084b3Smrg oIFS=$IFS 2273da084b3Smrg # Some sh's can't handle IFS=/ for some reason. 2283da084b3Smrg IFS='%' 2293da084b3Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 2303da084b3Smrg shift 2313da084b3Smrg IFS=$oIFS 2323da084b3Smrg 2333da084b3Smrg pathcomp= 2343da084b3Smrg 2353da084b3Smrg while test $# -ne 0 ; do 2363da084b3Smrg pathcomp=$pathcomp$1 2373da084b3Smrg shift 2383da084b3Smrg if test ! -d "$pathcomp"; then 2393da084b3Smrg $mkdirprog "$pathcomp" 2403da084b3Smrg # mkdir can fail with a `File exist' error in case several 2413da084b3Smrg # install-sh are creating the directory concurrently. This 2423da084b3Smrg # is OK. 2433da084b3Smrg test -d "$pathcomp" || exit 2443da084b3Smrg fi 2453da084b3Smrg pathcomp=$pathcomp/ 2463da084b3Smrg done 2473da084b3Smrg fi 2483da084b3Smrg 2493da084b3Smrg if test -n "$dir_arg"; then 2503da084b3Smrg $doit $mkdircmd "$dst" \ 2513da084b3Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 2523da084b3Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 2533da084b3Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 2543da084b3Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 2553da084b3Smrg 2563da084b3Smrg else 2573da084b3Smrg dstfile=`basename "$dst"` 2583da084b3Smrg 2593da084b3Smrg # Make a couple of temp file names in the proper directory. 2603da084b3Smrg dsttmp=$dstdir/_inst.$$_ 2613da084b3Smrg rmtmp=$dstdir/_rm.$$_ 2623da084b3Smrg 2633da084b3Smrg # Trap to clean up those temp files at exit. 2643da084b3Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 2653da084b3Smrg trap '(exit $?); exit' 1 2 13 15 2663da084b3Smrg 2673da084b3Smrg # Copy the file name to the temp name. 2683da084b3Smrg $doit $cpprog "$src" "$dsttmp" && 2693da084b3Smrg 2703da084b3Smrg # and set any options; do chmod last to preserve setuid bits. 2713da084b3Smrg # 2723da084b3Smrg # If any of these fail, we abort the whole thing. If we want to 2733da084b3Smrg # ignore errors from any of these, just make sure not to ignore 2743da084b3Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 2753da084b3Smrg # 2763da084b3Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 2773da084b3Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 2783da084b3Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 2793da084b3Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 2803da084b3Smrg 2813da084b3Smrg # Now rename the file to the real destination. 2823da084b3Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 2833da084b3Smrg || { 2843da084b3Smrg # The rename failed, perhaps because mv can't rename something else 2853da084b3Smrg # to itself, or perhaps because mv is so ancient that it does not 2863da084b3Smrg # support -f. 2873da084b3Smrg 2883da084b3Smrg # Now remove or move aside any old file at destination location. 2893da084b3Smrg # We try this two ways since rm can't unlink itself on some 2903da084b3Smrg # systems and the destination file might be busy for other 2913da084b3Smrg # reasons. In this case, the final cleanup might fail but the new 2923da084b3Smrg # file should still install successfully. 2933da084b3Smrg { 2943da084b3Smrg if test -f "$dstdir/$dstfile"; then 2953da084b3Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 2963da084b3Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 2973da084b3Smrg || { 2983da084b3Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 2993da084b3Smrg (exit 1); exit 1 3003da084b3Smrg } 3013da084b3Smrg else 3023da084b3Smrg : 3033da084b3Smrg fi 3043da084b3Smrg } && 3053da084b3Smrg 3063da084b3Smrg # Now rename the file to the real destination. 3073da084b3Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 3083da084b3Smrg } 3093da084b3Smrg } 3103da084b3Smrg fi || { (exit 1); exit 1; } 3113da084b3Smrgdone 3123da084b3Smrg 3133da084b3Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 3143da084b3Smrg{ 3153da084b3Smrg (exit 0); exit 0 3163da084b3Smrg} 3173da084b3Smrg 3183da084b3Smrg# Local variables: 3193da084b3Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 3203da084b3Smrg# time-stamp-start: "scriptversion=" 3213da084b3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 3223da084b3Smrg# time-stamp-end: "$" 3233da084b3Smrg# End: 324