install-sh revision 66fe65f6
166fe65f6Smrg#!/bin/sh 266fe65f6Smrg# install - install a program, script, or datafile 366fe65f6Smrg 466fe65f6Smrgscriptversion=2005-05-14.22 566fe65f6Smrg 666fe65f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 766fe65f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the 866fe65f6Smrg# following copyright and license. 966fe65f6Smrg# 1066fe65f6Smrg# Copyright (C) 1994 X Consortium 1166fe65f6Smrg# 1266fe65f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 1366fe65f6Smrg# of this software and associated documentation files (the "Software"), to 1466fe65f6Smrg# deal in the Software without restriction, including without limitation the 1566fe65f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1666fe65f6Smrg# sell copies of the Software, and to permit persons to whom the Software is 1766fe65f6Smrg# furnished to do so, subject to the following conditions: 1866fe65f6Smrg# 1966fe65f6Smrg# The above copyright notice and this permission notice shall be included in 2066fe65f6Smrg# all copies or substantial portions of the Software. 2166fe65f6Smrg# 2266fe65f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2366fe65f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2466fe65f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2566fe65f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2666fe65f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2766fe65f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2866fe65f6Smrg# 2966fe65f6Smrg# Except as contained in this notice, the name of the X Consortium shall not 3066fe65f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 3166fe65f6Smrg# ings in this Software without prior written authorization from the X Consor- 3266fe65f6Smrg# tium. 3366fe65f6Smrg# 3466fe65f6Smrg# 3566fe65f6Smrg# FSF changes to this file are in the public domain. 3666fe65f6Smrg# 3766fe65f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent 3866fe65f6Smrg# `make' implicit rules from creating a file called install from it 3966fe65f6Smrg# when there is no Makefile. 4066fe65f6Smrg# 4166fe65f6Smrg# This script is compatible with the BSD install script, but was written 4266fe65f6Smrg# from scratch. It can only install one file at a time, a restriction 4366fe65f6Smrg# shared with many OS's install programs. 4466fe65f6Smrg 4566fe65f6Smrg# set DOITPROG to echo to test this script 4666fe65f6Smrg 4766fe65f6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 4866fe65f6Smrgdoit="${DOITPROG-}" 4966fe65f6Smrg 5066fe65f6Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 5166fe65f6Smrg 5266fe65f6Smrgmvprog="${MVPROG-mv}" 5366fe65f6Smrgcpprog="${CPPROG-cp}" 5466fe65f6Smrgchmodprog="${CHMODPROG-chmod}" 5566fe65f6Smrgchownprog="${CHOWNPROG-chown}" 5666fe65f6Smrgchgrpprog="${CHGRPPROG-chgrp}" 5766fe65f6Smrgstripprog="${STRIPPROG-strip}" 5866fe65f6Smrgrmprog="${RMPROG-rm}" 5966fe65f6Smrgmkdirprog="${MKDIRPROG-mkdir}" 6066fe65f6Smrg 6166fe65f6Smrgchmodcmd="$chmodprog 0755" 6266fe65f6Smrgchowncmd= 6366fe65f6Smrgchgrpcmd= 6466fe65f6Smrgstripcmd= 6566fe65f6Smrgrmcmd="$rmprog -f" 6666fe65f6Smrgmvcmd="$mvprog" 6766fe65f6Smrgsrc= 6866fe65f6Smrgdst= 6966fe65f6Smrgdir_arg= 7066fe65f6Smrgdstarg= 7166fe65f6Smrgno_target_directory= 7266fe65f6Smrg 7366fe65f6Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 7466fe65f6Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 7566fe65f6Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 7666fe65f6Smrg or: $0 [OPTION]... -d DIRECTORIES... 7766fe65f6Smrg 7866fe65f6SmrgIn the 1st form, copy SRCFILE to DSTFILE. 7966fe65f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 8066fe65f6SmrgIn the 4th, create DIRECTORIES. 8166fe65f6Smrg 8266fe65f6SmrgOptions: 8366fe65f6Smrg-c (ignored) 8466fe65f6Smrg-d create directories instead of installing files. 8566fe65f6Smrg-g GROUP $chgrpprog installed files to GROUP. 8666fe65f6Smrg-m MODE $chmodprog installed files to MODE. 8766fe65f6Smrg-o USER $chownprog installed files to USER. 8866fe65f6Smrg-s $stripprog installed files. 8966fe65f6Smrg-t DIRECTORY install into DIRECTORY. 9066fe65f6Smrg-T report an error if DSTFILE is a directory. 9166fe65f6Smrg--help display this help and exit. 9266fe65f6Smrg--version display version info and exit. 9366fe65f6Smrg 9466fe65f6SmrgEnvironment variables override the default commands: 9566fe65f6Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 9666fe65f6Smrg" 9766fe65f6Smrg 9866fe65f6Smrgwhile test -n "$1"; do 9966fe65f6Smrg case $1 in 10066fe65f6Smrg -c) shift 10166fe65f6Smrg continue;; 10266fe65f6Smrg 10366fe65f6Smrg -d) dir_arg=true 10466fe65f6Smrg shift 10566fe65f6Smrg continue;; 10666fe65f6Smrg 10766fe65f6Smrg -g) chgrpcmd="$chgrpprog $2" 10866fe65f6Smrg shift 10966fe65f6Smrg shift 11066fe65f6Smrg continue;; 11166fe65f6Smrg 11266fe65f6Smrg --help) echo "$usage"; exit $?;; 11366fe65f6Smrg 11466fe65f6Smrg -m) chmodcmd="$chmodprog $2" 11566fe65f6Smrg shift 11666fe65f6Smrg shift 11766fe65f6Smrg continue;; 11866fe65f6Smrg 11966fe65f6Smrg -o) chowncmd="$chownprog $2" 12066fe65f6Smrg shift 12166fe65f6Smrg shift 12266fe65f6Smrg continue;; 12366fe65f6Smrg 12466fe65f6Smrg -s) stripcmd=$stripprog 12566fe65f6Smrg shift 12666fe65f6Smrg continue;; 12766fe65f6Smrg 12866fe65f6Smrg -t) dstarg=$2 12966fe65f6Smrg shift 13066fe65f6Smrg shift 13166fe65f6Smrg continue;; 13266fe65f6Smrg 13366fe65f6Smrg -T) no_target_directory=true 13466fe65f6Smrg shift 13566fe65f6Smrg continue;; 13666fe65f6Smrg 13766fe65f6Smrg --version) echo "$0 $scriptversion"; exit $?;; 13866fe65f6Smrg 13966fe65f6Smrg *) # When -d is used, all remaining arguments are directories to create. 14066fe65f6Smrg # When -t is used, the destination is already specified. 14166fe65f6Smrg test -n "$dir_arg$dstarg" && break 14266fe65f6Smrg # Otherwise, the last argument is the destination. Remove it from $@. 14366fe65f6Smrg for arg 14466fe65f6Smrg do 14566fe65f6Smrg if test -n "$dstarg"; then 14666fe65f6Smrg # $@ is not empty: it contains at least $arg. 14766fe65f6Smrg set fnord "$@" "$dstarg" 14866fe65f6Smrg shift # fnord 14966fe65f6Smrg fi 15066fe65f6Smrg shift # arg 15166fe65f6Smrg dstarg=$arg 15266fe65f6Smrg done 15366fe65f6Smrg break;; 15466fe65f6Smrg esac 15566fe65f6Smrgdone 15666fe65f6Smrg 15766fe65f6Smrgif test -z "$1"; then 15866fe65f6Smrg if test -z "$dir_arg"; then 15966fe65f6Smrg echo "$0: no input file specified." >&2 16066fe65f6Smrg exit 1 16166fe65f6Smrg fi 16266fe65f6Smrg # It's OK to call `install-sh -d' without argument. 16366fe65f6Smrg # This can happen when creating conditional directories. 16466fe65f6Smrg exit 0 16566fe65f6Smrgfi 16666fe65f6Smrg 16766fe65f6Smrgfor src 16866fe65f6Smrgdo 16966fe65f6Smrg # Protect names starting with `-'. 17066fe65f6Smrg case $src in 17166fe65f6Smrg -*) src=./$src ;; 17266fe65f6Smrg esac 17366fe65f6Smrg 17466fe65f6Smrg if test -n "$dir_arg"; then 17566fe65f6Smrg dst=$src 17666fe65f6Smrg src= 17766fe65f6Smrg 17866fe65f6Smrg if test -d "$dst"; then 17966fe65f6Smrg mkdircmd=: 18066fe65f6Smrg chmodcmd= 18166fe65f6Smrg else 18266fe65f6Smrg mkdircmd=$mkdirprog 18366fe65f6Smrg fi 18466fe65f6Smrg else 18566fe65f6Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 18666fe65f6Smrg # might cause directories to be created, which would be especially bad 18766fe65f6Smrg # if $src (and thus $dsttmp) contains '*'. 18866fe65f6Smrg if test ! -f "$src" && test ! -d "$src"; then 18966fe65f6Smrg echo "$0: $src does not exist." >&2 19066fe65f6Smrg exit 1 19166fe65f6Smrg fi 19266fe65f6Smrg 19366fe65f6Smrg if test -z "$dstarg"; then 19466fe65f6Smrg echo "$0: no destination specified." >&2 19566fe65f6Smrg exit 1 19666fe65f6Smrg fi 19766fe65f6Smrg 19866fe65f6Smrg dst=$dstarg 19966fe65f6Smrg # Protect names starting with `-'. 20066fe65f6Smrg case $dst in 20166fe65f6Smrg -*) dst=./$dst ;; 20266fe65f6Smrg esac 20366fe65f6Smrg 20466fe65f6Smrg # If destination is a directory, append the input filename; won't work 20566fe65f6Smrg # if double slashes aren't ignored. 20666fe65f6Smrg if test -d "$dst"; then 20766fe65f6Smrg if test -n "$no_target_directory"; then 20866fe65f6Smrg echo "$0: $dstarg: Is a directory" >&2 20966fe65f6Smrg exit 1 21066fe65f6Smrg fi 21166fe65f6Smrg dst=$dst/`basename "$src"` 21266fe65f6Smrg fi 21366fe65f6Smrg fi 21466fe65f6Smrg 21566fe65f6Smrg # This sed command emulates the dirname command. 21666fe65f6Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 21766fe65f6Smrg 21866fe65f6Smrg # Make sure that the destination directory exists. 21966fe65f6Smrg 22066fe65f6Smrg # Skip lots of stat calls in the usual case. 22166fe65f6Smrg if test ! -d "$dstdir"; then 22266fe65f6Smrg defaultIFS=' 22366fe65f6Smrg ' 22466fe65f6Smrg IFS="${IFS-$defaultIFS}" 22566fe65f6Smrg 22666fe65f6Smrg oIFS=$IFS 22766fe65f6Smrg # Some sh's can't handle IFS=/ for some reason. 22866fe65f6Smrg IFS='%' 22966fe65f6Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 23066fe65f6Smrg shift 23166fe65f6Smrg IFS=$oIFS 23266fe65f6Smrg 23366fe65f6Smrg pathcomp= 23466fe65f6Smrg 23566fe65f6Smrg while test $# -ne 0 ; do 23666fe65f6Smrg pathcomp=$pathcomp$1 23766fe65f6Smrg shift 23866fe65f6Smrg if test ! -d "$pathcomp"; then 23966fe65f6Smrg $mkdirprog "$pathcomp" 24066fe65f6Smrg # mkdir can fail with a `File exist' error in case several 24166fe65f6Smrg # install-sh are creating the directory concurrently. This 24266fe65f6Smrg # is OK. 24366fe65f6Smrg test -d "$pathcomp" || exit 24466fe65f6Smrg fi 24566fe65f6Smrg pathcomp=$pathcomp/ 24666fe65f6Smrg done 24766fe65f6Smrg fi 24866fe65f6Smrg 24966fe65f6Smrg if test -n "$dir_arg"; then 25066fe65f6Smrg $doit $mkdircmd "$dst" \ 25166fe65f6Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 25266fe65f6Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 25366fe65f6Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 25466fe65f6Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 25566fe65f6Smrg 25666fe65f6Smrg else 25766fe65f6Smrg dstfile=`basename "$dst"` 25866fe65f6Smrg 25966fe65f6Smrg # Make a couple of temp file names in the proper directory. 26066fe65f6Smrg dsttmp=$dstdir/_inst.$$_ 26166fe65f6Smrg rmtmp=$dstdir/_rm.$$_ 26266fe65f6Smrg 26366fe65f6Smrg # Trap to clean up those temp files at exit. 26466fe65f6Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 26566fe65f6Smrg trap '(exit $?); exit' 1 2 13 15 26666fe65f6Smrg 26766fe65f6Smrg # Copy the file name to the temp name. 26866fe65f6Smrg $doit $cpprog "$src" "$dsttmp" && 26966fe65f6Smrg 27066fe65f6Smrg # and set any options; do chmod last to preserve setuid bits. 27166fe65f6Smrg # 27266fe65f6Smrg # If any of these fail, we abort the whole thing. If we want to 27366fe65f6Smrg # ignore errors from any of these, just make sure not to ignore 27466fe65f6Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 27566fe65f6Smrg # 27666fe65f6Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 27766fe65f6Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 27866fe65f6Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 27966fe65f6Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 28066fe65f6Smrg 28166fe65f6Smrg # Now rename the file to the real destination. 28266fe65f6Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 28366fe65f6Smrg || { 28466fe65f6Smrg # The rename failed, perhaps because mv can't rename something else 28566fe65f6Smrg # to itself, or perhaps because mv is so ancient that it does not 28666fe65f6Smrg # support -f. 28766fe65f6Smrg 28866fe65f6Smrg # Now remove or move aside any old file at destination location. 28966fe65f6Smrg # We try this two ways since rm can't unlink itself on some 29066fe65f6Smrg # systems and the destination file might be busy for other 29166fe65f6Smrg # reasons. In this case, the final cleanup might fail but the new 29266fe65f6Smrg # file should still install successfully. 29366fe65f6Smrg { 29466fe65f6Smrg if test -f "$dstdir/$dstfile"; then 29566fe65f6Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 29666fe65f6Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 29766fe65f6Smrg || { 29866fe65f6Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 29966fe65f6Smrg (exit 1); exit 1 30066fe65f6Smrg } 30166fe65f6Smrg else 30266fe65f6Smrg : 30366fe65f6Smrg fi 30466fe65f6Smrg } && 30566fe65f6Smrg 30666fe65f6Smrg # Now rename the file to the real destination. 30766fe65f6Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 30866fe65f6Smrg } 30966fe65f6Smrg } 31066fe65f6Smrg fi || { (exit 1); exit 1; } 31166fe65f6Smrgdone 31266fe65f6Smrg 31366fe65f6Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 31466fe65f6Smrg{ 31566fe65f6Smrg (exit 0); exit 0 31666fe65f6Smrg} 31766fe65f6Smrg 31866fe65f6Smrg# Local variables: 31966fe65f6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 32066fe65f6Smrg# time-stamp-start: "scriptversion=" 32166fe65f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 32266fe65f6Smrg# time-stamp-end: "$" 32366fe65f6Smrg# End: 324