install-sh revision 29459361
129459361Smrg#!/bin/sh 229459361Smrg# install - install a program, script, or datafile 329459361Smrg 429459361Smrgscriptversion=2005-05-14.22 529459361Smrg 629459361Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 729459361Smrg# later released in X11R6 (xc/config/util/install.sh) with the 829459361Smrg# following copyright and license. 929459361Smrg# 1029459361Smrg# Copyright (C) 1994 X Consortium 1129459361Smrg# 1229459361Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 1329459361Smrg# of this software and associated documentation files (the "Software"), to 1429459361Smrg# deal in the Software without restriction, including without limitation the 1529459361Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1629459361Smrg# sell copies of the Software, and to permit persons to whom the Software is 1729459361Smrg# furnished to do so, subject to the following conditions: 1829459361Smrg# 1929459361Smrg# The above copyright notice and this permission notice shall be included in 2029459361Smrg# all copies or substantial portions of the Software. 2129459361Smrg# 2229459361Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2329459361Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2429459361Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2529459361Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2629459361Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2729459361Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2829459361Smrg# 2929459361Smrg# Except as contained in this notice, the name of the X Consortium shall not 3029459361Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 3129459361Smrg# ings in this Software without prior written authorization from the X Consor- 3229459361Smrg# tium. 3329459361Smrg# 3429459361Smrg# 3529459361Smrg# FSF changes to this file are in the public domain. 3629459361Smrg# 3729459361Smrg# Calling this script install-sh is preferred over install.sh, to prevent 3829459361Smrg# `make' implicit rules from creating a file called install from it 3929459361Smrg# when there is no Makefile. 4029459361Smrg# 4129459361Smrg# This script is compatible with the BSD install script, but was written 4229459361Smrg# from scratch. It can only install one file at a time, a restriction 4329459361Smrg# shared with many OS's install programs. 4429459361Smrg 4529459361Smrg# set DOITPROG to echo to test this script 4629459361Smrg 4729459361Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 4829459361Smrgdoit="${DOITPROG-}" 4929459361Smrg 5029459361Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 5129459361Smrg 5229459361Smrgmvprog="${MVPROG-mv}" 5329459361Smrgcpprog="${CPPROG-cp}" 5429459361Smrgchmodprog="${CHMODPROG-chmod}" 5529459361Smrgchownprog="${CHOWNPROG-chown}" 5629459361Smrgchgrpprog="${CHGRPPROG-chgrp}" 5729459361Smrgstripprog="${STRIPPROG-strip}" 5829459361Smrgrmprog="${RMPROG-rm}" 5929459361Smrgmkdirprog="${MKDIRPROG-mkdir}" 6029459361Smrg 6129459361Smrgchmodcmd="$chmodprog 0755" 6229459361Smrgchowncmd= 6329459361Smrgchgrpcmd= 6429459361Smrgstripcmd= 6529459361Smrgrmcmd="$rmprog -f" 6629459361Smrgmvcmd="$mvprog" 6729459361Smrgsrc= 6829459361Smrgdst= 6929459361Smrgdir_arg= 7029459361Smrgdstarg= 7129459361Smrgno_target_directory= 7229459361Smrg 7329459361Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 7429459361Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 7529459361Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 7629459361Smrg or: $0 [OPTION]... -d DIRECTORIES... 7729459361Smrg 7829459361SmrgIn the 1st form, copy SRCFILE to DSTFILE. 7929459361SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 8029459361SmrgIn the 4th, create DIRECTORIES. 8129459361Smrg 8229459361SmrgOptions: 8329459361Smrg-c (ignored) 8429459361Smrg-d create directories instead of installing files. 8529459361Smrg-g GROUP $chgrpprog installed files to GROUP. 8629459361Smrg-m MODE $chmodprog installed files to MODE. 8729459361Smrg-o USER $chownprog installed files to USER. 8829459361Smrg-s $stripprog installed files. 8929459361Smrg-t DIRECTORY install into DIRECTORY. 9029459361Smrg-T report an error if DSTFILE is a directory. 9129459361Smrg--help display this help and exit. 9229459361Smrg--version display version info and exit. 9329459361Smrg 9429459361SmrgEnvironment variables override the default commands: 9529459361Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 9629459361Smrg" 9729459361Smrg 9829459361Smrgwhile test -n "$1"; do 9929459361Smrg case $1 in 10029459361Smrg -c) shift 10129459361Smrg continue;; 10229459361Smrg 10329459361Smrg -d) dir_arg=true 10429459361Smrg shift 10529459361Smrg continue;; 10629459361Smrg 10729459361Smrg -g) chgrpcmd="$chgrpprog $2" 10829459361Smrg shift 10929459361Smrg shift 11029459361Smrg continue;; 11129459361Smrg 11229459361Smrg --help) echo "$usage"; exit $?;; 11329459361Smrg 11429459361Smrg -m) chmodcmd="$chmodprog $2" 11529459361Smrg shift 11629459361Smrg shift 11729459361Smrg continue;; 11829459361Smrg 11929459361Smrg -o) chowncmd="$chownprog $2" 12029459361Smrg shift 12129459361Smrg shift 12229459361Smrg continue;; 12329459361Smrg 12429459361Smrg -s) stripcmd=$stripprog 12529459361Smrg shift 12629459361Smrg continue;; 12729459361Smrg 12829459361Smrg -t) dstarg=$2 12929459361Smrg shift 13029459361Smrg shift 13129459361Smrg continue;; 13229459361Smrg 13329459361Smrg -T) no_target_directory=true 13429459361Smrg shift 13529459361Smrg continue;; 13629459361Smrg 13729459361Smrg --version) echo "$0 $scriptversion"; exit $?;; 13829459361Smrg 13929459361Smrg *) # When -d is used, all remaining arguments are directories to create. 14029459361Smrg # When -t is used, the destination is already specified. 14129459361Smrg test -n "$dir_arg$dstarg" && break 14229459361Smrg # Otherwise, the last argument is the destination. Remove it from $@. 14329459361Smrg for arg 14429459361Smrg do 14529459361Smrg if test -n "$dstarg"; then 14629459361Smrg # $@ is not empty: it contains at least $arg. 14729459361Smrg set fnord "$@" "$dstarg" 14829459361Smrg shift # fnord 14929459361Smrg fi 15029459361Smrg shift # arg 15129459361Smrg dstarg=$arg 15229459361Smrg done 15329459361Smrg break;; 15429459361Smrg esac 15529459361Smrgdone 15629459361Smrg 15729459361Smrgif test -z "$1"; then 15829459361Smrg if test -z "$dir_arg"; then 15929459361Smrg echo "$0: no input file specified." >&2 16029459361Smrg exit 1 16129459361Smrg fi 16229459361Smrg # It's OK to call `install-sh -d' without argument. 16329459361Smrg # This can happen when creating conditional directories. 16429459361Smrg exit 0 16529459361Smrgfi 16629459361Smrg 16729459361Smrgfor src 16829459361Smrgdo 16929459361Smrg # Protect names starting with `-'. 17029459361Smrg case $src in 17129459361Smrg -*) src=./$src ;; 17229459361Smrg esac 17329459361Smrg 17429459361Smrg if test -n "$dir_arg"; then 17529459361Smrg dst=$src 17629459361Smrg src= 17729459361Smrg 17829459361Smrg if test -d "$dst"; then 17929459361Smrg mkdircmd=: 18029459361Smrg chmodcmd= 18129459361Smrg else 18229459361Smrg mkdircmd=$mkdirprog 18329459361Smrg fi 18429459361Smrg else 18529459361Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 18629459361Smrg # might cause directories to be created, which would be especially bad 18729459361Smrg # if $src (and thus $dsttmp) contains '*'. 18829459361Smrg if test ! -f "$src" && test ! -d "$src"; then 18929459361Smrg echo "$0: $src does not exist." >&2 19029459361Smrg exit 1 19129459361Smrg fi 19229459361Smrg 19329459361Smrg if test -z "$dstarg"; then 19429459361Smrg echo "$0: no destination specified." >&2 19529459361Smrg exit 1 19629459361Smrg fi 19729459361Smrg 19829459361Smrg dst=$dstarg 19929459361Smrg # Protect names starting with `-'. 20029459361Smrg case $dst in 20129459361Smrg -*) dst=./$dst ;; 20229459361Smrg esac 20329459361Smrg 20429459361Smrg # If destination is a directory, append the input filename; won't work 20529459361Smrg # if double slashes aren't ignored. 20629459361Smrg if test -d "$dst"; then 20729459361Smrg if test -n "$no_target_directory"; then 20829459361Smrg echo "$0: $dstarg: Is a directory" >&2 20929459361Smrg exit 1 21029459361Smrg fi 21129459361Smrg dst=$dst/`basename "$src"` 21229459361Smrg fi 21329459361Smrg fi 21429459361Smrg 21529459361Smrg # This sed command emulates the dirname command. 21629459361Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 21729459361Smrg 21829459361Smrg # Make sure that the destination directory exists. 21929459361Smrg 22029459361Smrg # Skip lots of stat calls in the usual case. 22129459361Smrg if test ! -d "$dstdir"; then 22229459361Smrg defaultIFS=' 22329459361Smrg ' 22429459361Smrg IFS="${IFS-$defaultIFS}" 22529459361Smrg 22629459361Smrg oIFS=$IFS 22729459361Smrg # Some sh's can't handle IFS=/ for some reason. 22829459361Smrg IFS='%' 22929459361Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 23029459361Smrg shift 23129459361Smrg IFS=$oIFS 23229459361Smrg 23329459361Smrg pathcomp= 23429459361Smrg 23529459361Smrg while test $# -ne 0 ; do 23629459361Smrg pathcomp=$pathcomp$1 23729459361Smrg shift 23829459361Smrg if test ! -d "$pathcomp"; then 23929459361Smrg $mkdirprog "$pathcomp" 24029459361Smrg # mkdir can fail with a `File exist' error in case several 24129459361Smrg # install-sh are creating the directory concurrently. This 24229459361Smrg # is OK. 24329459361Smrg test -d "$pathcomp" || exit 24429459361Smrg fi 24529459361Smrg pathcomp=$pathcomp/ 24629459361Smrg done 24729459361Smrg fi 24829459361Smrg 24929459361Smrg if test -n "$dir_arg"; then 25029459361Smrg $doit $mkdircmd "$dst" \ 25129459361Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 25229459361Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 25329459361Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 25429459361Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 25529459361Smrg 25629459361Smrg else 25729459361Smrg dstfile=`basename "$dst"` 25829459361Smrg 25929459361Smrg # Make a couple of temp file names in the proper directory. 26029459361Smrg dsttmp=$dstdir/_inst.$$_ 26129459361Smrg rmtmp=$dstdir/_rm.$$_ 26229459361Smrg 26329459361Smrg # Trap to clean up those temp files at exit. 26429459361Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 26529459361Smrg trap '(exit $?); exit' 1 2 13 15 26629459361Smrg 26729459361Smrg # Copy the file name to the temp name. 26829459361Smrg $doit $cpprog "$src" "$dsttmp" && 26929459361Smrg 27029459361Smrg # and set any options; do chmod last to preserve setuid bits. 27129459361Smrg # 27229459361Smrg # If any of these fail, we abort the whole thing. If we want to 27329459361Smrg # ignore errors from any of these, just make sure not to ignore 27429459361Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 27529459361Smrg # 27629459361Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 27729459361Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 27829459361Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 27929459361Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 28029459361Smrg 28129459361Smrg # Now rename the file to the real destination. 28229459361Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 28329459361Smrg || { 28429459361Smrg # The rename failed, perhaps because mv can't rename something else 28529459361Smrg # to itself, or perhaps because mv is so ancient that it does not 28629459361Smrg # support -f. 28729459361Smrg 28829459361Smrg # Now remove or move aside any old file at destination location. 28929459361Smrg # We try this two ways since rm can't unlink itself on some 29029459361Smrg # systems and the destination file might be busy for other 29129459361Smrg # reasons. In this case, the final cleanup might fail but the new 29229459361Smrg # file should still install successfully. 29329459361Smrg { 29429459361Smrg if test -f "$dstdir/$dstfile"; then 29529459361Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 29629459361Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 29729459361Smrg || { 29829459361Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 29929459361Smrg (exit 1); exit 1 30029459361Smrg } 30129459361Smrg else 30229459361Smrg : 30329459361Smrg fi 30429459361Smrg } && 30529459361Smrg 30629459361Smrg # Now rename the file to the real destination. 30729459361Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 30829459361Smrg } 30929459361Smrg } 31029459361Smrg fi || { (exit 1); exit 1; } 31129459361Smrgdone 31229459361Smrg 31329459361Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 31429459361Smrg{ 31529459361Smrg (exit 0); exit 0 31629459361Smrg} 31729459361Smrg 31829459361Smrg# Local variables: 31929459361Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 32029459361Smrg# time-stamp-start: "scriptversion=" 32129459361Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 32229459361Smrg# time-stamp-end: "$" 32329459361Smrg# End: 324