install-sh revision 52f9793f
152f9793fSmrg#!/bin/sh 252f9793fSmrg# install - install a program, script, or datafile 352f9793fSmrg 452f9793fSmrgscriptversion=2005-05-14.22 552f9793fSmrg 652f9793fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 752f9793fSmrg# later released in X11R6 (xc/config/util/install.sh) with the 852f9793fSmrg# following copyright and license. 952f9793fSmrg# 1052f9793fSmrg# Copyright (C) 1994 X Consortium 1152f9793fSmrg# 1252f9793fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy 1352f9793fSmrg# of this software and associated documentation files (the "Software"), to 1452f9793fSmrg# deal in the Software without restriction, including without limitation the 1552f9793fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1652f9793fSmrg# sell copies of the Software, and to permit persons to whom the Software is 1752f9793fSmrg# furnished to do so, subject to the following conditions: 1852f9793fSmrg# 1952f9793fSmrg# The above copyright notice and this permission notice shall be included in 2052f9793fSmrg# all copies or substantial portions of the Software. 2152f9793fSmrg# 2252f9793fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2352f9793fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2452f9793fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2552f9793fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2652f9793fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2752f9793fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2852f9793fSmrg# 2952f9793fSmrg# Except as contained in this notice, the name of the X Consortium shall not 3052f9793fSmrg# be used in advertising or otherwise to promote the sale, use or other deal- 3152f9793fSmrg# ings in this Software without prior written authorization from the X Consor- 3252f9793fSmrg# tium. 3352f9793fSmrg# 3452f9793fSmrg# 3552f9793fSmrg# FSF changes to this file are in the public domain. 3652f9793fSmrg# 3752f9793fSmrg# Calling this script install-sh is preferred over install.sh, to prevent 3852f9793fSmrg# `make' implicit rules from creating a file called install from it 3952f9793fSmrg# when there is no Makefile. 4052f9793fSmrg# 4152f9793fSmrg# This script is compatible with the BSD install script, but was written 4252f9793fSmrg# from scratch. It can only install one file at a time, a restriction 4352f9793fSmrg# shared with many OS's install programs. 4452f9793fSmrg 4552f9793fSmrg# set DOITPROG to echo to test this script 4652f9793fSmrg 4752f9793fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it. 4852f9793fSmrgdoit="${DOITPROG-}" 4952f9793fSmrg 5052f9793fSmrg# put in absolute paths if you don't have them in your path; or use env. vars. 5152f9793fSmrg 5252f9793fSmrgmvprog="${MVPROG-mv}" 5352f9793fSmrgcpprog="${CPPROG-cp}" 5452f9793fSmrgchmodprog="${CHMODPROG-chmod}" 5552f9793fSmrgchownprog="${CHOWNPROG-chown}" 5652f9793fSmrgchgrpprog="${CHGRPPROG-chgrp}" 5752f9793fSmrgstripprog="${STRIPPROG-strip}" 5852f9793fSmrgrmprog="${RMPROG-rm}" 5952f9793fSmrgmkdirprog="${MKDIRPROG-mkdir}" 6052f9793fSmrg 6152f9793fSmrgchmodcmd="$chmodprog 0755" 6252f9793fSmrgchowncmd= 6352f9793fSmrgchgrpcmd= 6452f9793fSmrgstripcmd= 6552f9793fSmrgrmcmd="$rmprog -f" 6652f9793fSmrgmvcmd="$mvprog" 6752f9793fSmrgsrc= 6852f9793fSmrgdst= 6952f9793fSmrgdir_arg= 7052f9793fSmrgdstarg= 7152f9793fSmrgno_target_directory= 7252f9793fSmrg 7352f9793fSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 7452f9793fSmrg or: $0 [OPTION]... SRCFILES... DIRECTORY 7552f9793fSmrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 7652f9793fSmrg or: $0 [OPTION]... -d DIRECTORIES... 7752f9793fSmrg 7852f9793fSmrgIn the 1st form, copy SRCFILE to DSTFILE. 7952f9793fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 8052f9793fSmrgIn the 4th, create DIRECTORIES. 8152f9793fSmrg 8252f9793fSmrgOptions: 8352f9793fSmrg-c (ignored) 8452f9793fSmrg-d create directories instead of installing files. 8552f9793fSmrg-g GROUP $chgrpprog installed files to GROUP. 8652f9793fSmrg-m MODE $chmodprog installed files to MODE. 8752f9793fSmrg-o USER $chownprog installed files to USER. 8852f9793fSmrg-s $stripprog installed files. 8952f9793fSmrg-t DIRECTORY install into DIRECTORY. 9052f9793fSmrg-T report an error if DSTFILE is a directory. 9152f9793fSmrg--help display this help and exit. 9252f9793fSmrg--version display version info and exit. 9352f9793fSmrg 9452f9793fSmrgEnvironment variables override the default commands: 9552f9793fSmrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 9652f9793fSmrg" 9752f9793fSmrg 9852f9793fSmrgwhile test -n "$1"; do 9952f9793fSmrg case $1 in 10052f9793fSmrg -c) shift 10152f9793fSmrg continue;; 10252f9793fSmrg 10352f9793fSmrg -d) dir_arg=true 10452f9793fSmrg shift 10552f9793fSmrg continue;; 10652f9793fSmrg 10752f9793fSmrg -g) chgrpcmd="$chgrpprog $2" 10852f9793fSmrg shift 10952f9793fSmrg shift 11052f9793fSmrg continue;; 11152f9793fSmrg 11252f9793fSmrg --help) echo "$usage"; exit $?;; 11352f9793fSmrg 11452f9793fSmrg -m) chmodcmd="$chmodprog $2" 11552f9793fSmrg shift 11652f9793fSmrg shift 11752f9793fSmrg continue;; 11852f9793fSmrg 11952f9793fSmrg -o) chowncmd="$chownprog $2" 12052f9793fSmrg shift 12152f9793fSmrg shift 12252f9793fSmrg continue;; 12352f9793fSmrg 12452f9793fSmrg -s) stripcmd=$stripprog 12552f9793fSmrg shift 12652f9793fSmrg continue;; 12752f9793fSmrg 12852f9793fSmrg -t) dstarg=$2 12952f9793fSmrg shift 13052f9793fSmrg shift 13152f9793fSmrg continue;; 13252f9793fSmrg 13352f9793fSmrg -T) no_target_directory=true 13452f9793fSmrg shift 13552f9793fSmrg continue;; 13652f9793fSmrg 13752f9793fSmrg --version) echo "$0 $scriptversion"; exit $?;; 13852f9793fSmrg 13952f9793fSmrg *) # When -d is used, all remaining arguments are directories to create. 14052f9793fSmrg # When -t is used, the destination is already specified. 14152f9793fSmrg test -n "$dir_arg$dstarg" && break 14252f9793fSmrg # Otherwise, the last argument is the destination. Remove it from $@. 14352f9793fSmrg for arg 14452f9793fSmrg do 14552f9793fSmrg if test -n "$dstarg"; then 14652f9793fSmrg # $@ is not empty: it contains at least $arg. 14752f9793fSmrg set fnord "$@" "$dstarg" 14852f9793fSmrg shift # fnord 14952f9793fSmrg fi 15052f9793fSmrg shift # arg 15152f9793fSmrg dstarg=$arg 15252f9793fSmrg done 15352f9793fSmrg break;; 15452f9793fSmrg esac 15552f9793fSmrgdone 15652f9793fSmrg 15752f9793fSmrgif test -z "$1"; then 15852f9793fSmrg if test -z "$dir_arg"; then 15952f9793fSmrg echo "$0: no input file specified." >&2 16052f9793fSmrg exit 1 16152f9793fSmrg fi 16252f9793fSmrg # It's OK to call `install-sh -d' without argument. 16352f9793fSmrg # This can happen when creating conditional directories. 16452f9793fSmrg exit 0 16552f9793fSmrgfi 16652f9793fSmrg 16752f9793fSmrgfor src 16852f9793fSmrgdo 16952f9793fSmrg # Protect names starting with `-'. 17052f9793fSmrg case $src in 17152f9793fSmrg -*) src=./$src ;; 17252f9793fSmrg esac 17352f9793fSmrg 17452f9793fSmrg if test -n "$dir_arg"; then 17552f9793fSmrg dst=$src 17652f9793fSmrg src= 17752f9793fSmrg 17852f9793fSmrg if test -d "$dst"; then 17952f9793fSmrg mkdircmd=: 18052f9793fSmrg chmodcmd= 18152f9793fSmrg else 18252f9793fSmrg mkdircmd=$mkdirprog 18352f9793fSmrg fi 18452f9793fSmrg else 18552f9793fSmrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 18652f9793fSmrg # might cause directories to be created, which would be especially bad 18752f9793fSmrg # if $src (and thus $dsttmp) contains '*'. 18852f9793fSmrg if test ! -f "$src" && test ! -d "$src"; then 18952f9793fSmrg echo "$0: $src does not exist." >&2 19052f9793fSmrg exit 1 19152f9793fSmrg fi 19252f9793fSmrg 19352f9793fSmrg if test -z "$dstarg"; then 19452f9793fSmrg echo "$0: no destination specified." >&2 19552f9793fSmrg exit 1 19652f9793fSmrg fi 19752f9793fSmrg 19852f9793fSmrg dst=$dstarg 19952f9793fSmrg # Protect names starting with `-'. 20052f9793fSmrg case $dst in 20152f9793fSmrg -*) dst=./$dst ;; 20252f9793fSmrg esac 20352f9793fSmrg 20452f9793fSmrg # If destination is a directory, append the input filename; won't work 20552f9793fSmrg # if double slashes aren't ignored. 20652f9793fSmrg if test -d "$dst"; then 20752f9793fSmrg if test -n "$no_target_directory"; then 20852f9793fSmrg echo "$0: $dstarg: Is a directory" >&2 20952f9793fSmrg exit 1 21052f9793fSmrg fi 21152f9793fSmrg dst=$dst/`basename "$src"` 21252f9793fSmrg fi 21352f9793fSmrg fi 21452f9793fSmrg 21552f9793fSmrg # This sed command emulates the dirname command. 21652f9793fSmrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 21752f9793fSmrg 21852f9793fSmrg # Make sure that the destination directory exists. 21952f9793fSmrg 22052f9793fSmrg # Skip lots of stat calls in the usual case. 22152f9793fSmrg if test ! -d "$dstdir"; then 22252f9793fSmrg defaultIFS=' 22352f9793fSmrg ' 22452f9793fSmrg IFS="${IFS-$defaultIFS}" 22552f9793fSmrg 22652f9793fSmrg oIFS=$IFS 22752f9793fSmrg # Some sh's can't handle IFS=/ for some reason. 22852f9793fSmrg IFS='%' 22952f9793fSmrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 23052f9793fSmrg shift 23152f9793fSmrg IFS=$oIFS 23252f9793fSmrg 23352f9793fSmrg pathcomp= 23452f9793fSmrg 23552f9793fSmrg while test $# -ne 0 ; do 23652f9793fSmrg pathcomp=$pathcomp$1 23752f9793fSmrg shift 23852f9793fSmrg if test ! -d "$pathcomp"; then 23952f9793fSmrg $mkdirprog "$pathcomp" 24052f9793fSmrg # mkdir can fail with a `File exist' error in case several 24152f9793fSmrg # install-sh are creating the directory concurrently. This 24252f9793fSmrg # is OK. 24352f9793fSmrg test -d "$pathcomp" || exit 24452f9793fSmrg fi 24552f9793fSmrg pathcomp=$pathcomp/ 24652f9793fSmrg done 24752f9793fSmrg fi 24852f9793fSmrg 24952f9793fSmrg if test -n "$dir_arg"; then 25052f9793fSmrg $doit $mkdircmd "$dst" \ 25152f9793fSmrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 25252f9793fSmrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 25352f9793fSmrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 25452f9793fSmrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 25552f9793fSmrg 25652f9793fSmrg else 25752f9793fSmrg dstfile=`basename "$dst"` 25852f9793fSmrg 25952f9793fSmrg # Make a couple of temp file names in the proper directory. 26052f9793fSmrg dsttmp=$dstdir/_inst.$$_ 26152f9793fSmrg rmtmp=$dstdir/_rm.$$_ 26252f9793fSmrg 26352f9793fSmrg # Trap to clean up those temp files at exit. 26452f9793fSmrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 26552f9793fSmrg trap '(exit $?); exit' 1 2 13 15 26652f9793fSmrg 26752f9793fSmrg # Copy the file name to the temp name. 26852f9793fSmrg $doit $cpprog "$src" "$dsttmp" && 26952f9793fSmrg 27052f9793fSmrg # and set any options; do chmod last to preserve setuid bits. 27152f9793fSmrg # 27252f9793fSmrg # If any of these fail, we abort the whole thing. If we want to 27352f9793fSmrg # ignore errors from any of these, just make sure not to ignore 27452f9793fSmrg # errors from the above "$doit $cpprog $src $dsttmp" command. 27552f9793fSmrg # 27652f9793fSmrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 27752f9793fSmrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 27852f9793fSmrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 27952f9793fSmrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 28052f9793fSmrg 28152f9793fSmrg # Now rename the file to the real destination. 28252f9793fSmrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 28352f9793fSmrg || { 28452f9793fSmrg # The rename failed, perhaps because mv can't rename something else 28552f9793fSmrg # to itself, or perhaps because mv is so ancient that it does not 28652f9793fSmrg # support -f. 28752f9793fSmrg 28852f9793fSmrg # Now remove or move aside any old file at destination location. 28952f9793fSmrg # We try this two ways since rm can't unlink itself on some 29052f9793fSmrg # systems and the destination file might be busy for other 29152f9793fSmrg # reasons. In this case, the final cleanup might fail but the new 29252f9793fSmrg # file should still install successfully. 29352f9793fSmrg { 29452f9793fSmrg if test -f "$dstdir/$dstfile"; then 29552f9793fSmrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 29652f9793fSmrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 29752f9793fSmrg || { 29852f9793fSmrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 29952f9793fSmrg (exit 1); exit 1 30052f9793fSmrg } 30152f9793fSmrg else 30252f9793fSmrg : 30352f9793fSmrg fi 30452f9793fSmrg } && 30552f9793fSmrg 30652f9793fSmrg # Now rename the file to the real destination. 30752f9793fSmrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 30852f9793fSmrg } 30952f9793fSmrg } 31052f9793fSmrg fi || { (exit 1); exit 1; } 31152f9793fSmrgdone 31252f9793fSmrg 31352f9793fSmrg# The final little trick to "correctly" pass the exit status to the exit trap. 31452f9793fSmrg{ 31552f9793fSmrg (exit 0); exit 0 31652f9793fSmrg} 31752f9793fSmrg 31852f9793fSmrg# Local variables: 31952f9793fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp) 32052f9793fSmrg# time-stamp-start: "scriptversion=" 32152f9793fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H" 32252f9793fSmrg# time-stamp-end: "$" 32352f9793fSmrg# End: 324