install-sh revision 76910425
176910425Smrg#!/bin/sh 276910425Smrg# install - install a program, script, or datafile 376910425Smrg 476910425Smrgscriptversion=2005-05-14.22 576910425Smrg 676910425Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 776910425Smrg# later released in X11R6 (xc/config/util/install.sh) with the 876910425Smrg# following copyright and license. 976910425Smrg# 1076910425Smrg# Copyright (C) 1994 X Consortium 1176910425Smrg# 1276910425Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 1376910425Smrg# of this software and associated documentation files (the "Software"), to 1476910425Smrg# deal in the Software without restriction, including without limitation the 1576910425Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1676910425Smrg# sell copies of the Software, and to permit persons to whom the Software is 1776910425Smrg# furnished to do so, subject to the following conditions: 1876910425Smrg# 1976910425Smrg# The above copyright notice and this permission notice shall be included in 2076910425Smrg# all copies or substantial portions of the Software. 2176910425Smrg# 2276910425Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2376910425Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2476910425Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2576910425Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2676910425Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2776910425Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2876910425Smrg# 2976910425Smrg# Except as contained in this notice, the name of the X Consortium shall not 3076910425Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 3176910425Smrg# ings in this Software without prior written authorization from the X Consor- 3276910425Smrg# tium. 3376910425Smrg# 3476910425Smrg# 3576910425Smrg# FSF changes to this file are in the public domain. 3676910425Smrg# 3776910425Smrg# Calling this script install-sh is preferred over install.sh, to prevent 3876910425Smrg# `make' implicit rules from creating a file called install from it 3976910425Smrg# when there is no Makefile. 4076910425Smrg# 4176910425Smrg# This script is compatible with the BSD install script, but was written 4276910425Smrg# from scratch. It can only install one file at a time, a restriction 4376910425Smrg# shared with many OS's install programs. 4476910425Smrg 4576910425Smrg# set DOITPROG to echo to test this script 4676910425Smrg 4776910425Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 4876910425Smrgdoit="${DOITPROG-}" 4976910425Smrg 5076910425Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 5176910425Smrg 5276910425Smrgmvprog="${MVPROG-mv}" 5376910425Smrgcpprog="${CPPROG-cp}" 5476910425Smrgchmodprog="${CHMODPROG-chmod}" 5576910425Smrgchownprog="${CHOWNPROG-chown}" 5676910425Smrgchgrpprog="${CHGRPPROG-chgrp}" 5776910425Smrgstripprog="${STRIPPROG-strip}" 5876910425Smrgrmprog="${RMPROG-rm}" 5976910425Smrgmkdirprog="${MKDIRPROG-mkdir}" 6076910425Smrg 6176910425Smrgchmodcmd="$chmodprog 0755" 6276910425Smrgchowncmd= 6376910425Smrgchgrpcmd= 6476910425Smrgstripcmd= 6576910425Smrgrmcmd="$rmprog -f" 6676910425Smrgmvcmd="$mvprog" 6776910425Smrgsrc= 6876910425Smrgdst= 6976910425Smrgdir_arg= 7076910425Smrgdstarg= 7176910425Smrgno_target_directory= 7276910425Smrg 7376910425Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 7476910425Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 7576910425Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 7676910425Smrg or: $0 [OPTION]... -d DIRECTORIES... 7776910425Smrg 7876910425SmrgIn the 1st form, copy SRCFILE to DSTFILE. 7976910425SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 8076910425SmrgIn the 4th, create DIRECTORIES. 8176910425Smrg 8276910425SmrgOptions: 8376910425Smrg-c (ignored) 8476910425Smrg-d create directories instead of installing files. 8576910425Smrg-g GROUP $chgrpprog installed files to GROUP. 8676910425Smrg-m MODE $chmodprog installed files to MODE. 8776910425Smrg-o USER $chownprog installed files to USER. 8876910425Smrg-s $stripprog installed files. 8976910425Smrg-t DIRECTORY install into DIRECTORY. 9076910425Smrg-T report an error if DSTFILE is a directory. 9176910425Smrg--help display this help and exit. 9276910425Smrg--version display version info and exit. 9376910425Smrg 9476910425SmrgEnvironment variables override the default commands: 9576910425Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 9676910425Smrg" 9776910425Smrg 9876910425Smrgwhile test -n "$1"; do 9976910425Smrg case $1 in 10076910425Smrg -c) shift 10176910425Smrg continue;; 10276910425Smrg 10376910425Smrg -d) dir_arg=true 10476910425Smrg shift 10576910425Smrg continue;; 10676910425Smrg 10776910425Smrg -g) chgrpcmd="$chgrpprog $2" 10876910425Smrg shift 10976910425Smrg shift 11076910425Smrg continue;; 11176910425Smrg 11276910425Smrg --help) echo "$usage"; exit $?;; 11376910425Smrg 11476910425Smrg -m) chmodcmd="$chmodprog $2" 11576910425Smrg shift 11676910425Smrg shift 11776910425Smrg continue;; 11876910425Smrg 11976910425Smrg -o) chowncmd="$chownprog $2" 12076910425Smrg shift 12176910425Smrg shift 12276910425Smrg continue;; 12376910425Smrg 12476910425Smrg -s) stripcmd=$stripprog 12576910425Smrg shift 12676910425Smrg continue;; 12776910425Smrg 12876910425Smrg -t) dstarg=$2 12976910425Smrg shift 13076910425Smrg shift 13176910425Smrg continue;; 13276910425Smrg 13376910425Smrg -T) no_target_directory=true 13476910425Smrg shift 13576910425Smrg continue;; 13676910425Smrg 13776910425Smrg --version) echo "$0 $scriptversion"; exit $?;; 13876910425Smrg 13976910425Smrg *) # When -d is used, all remaining arguments are directories to create. 14076910425Smrg # When -t is used, the destination is already specified. 14176910425Smrg test -n "$dir_arg$dstarg" && break 14276910425Smrg # Otherwise, the last argument is the destination. Remove it from $@. 14376910425Smrg for arg 14476910425Smrg do 14576910425Smrg if test -n "$dstarg"; then 14676910425Smrg # $@ is not empty: it contains at least $arg. 14776910425Smrg set fnord "$@" "$dstarg" 14876910425Smrg shift # fnord 14976910425Smrg fi 15076910425Smrg shift # arg 15176910425Smrg dstarg=$arg 15276910425Smrg done 15376910425Smrg break;; 15476910425Smrg esac 15576910425Smrgdone 15676910425Smrg 15776910425Smrgif test -z "$1"; then 15876910425Smrg if test -z "$dir_arg"; then 15976910425Smrg echo "$0: no input file specified." >&2 16076910425Smrg exit 1 16176910425Smrg fi 16276910425Smrg # It's OK to call `install-sh -d' without argument. 16376910425Smrg # This can happen when creating conditional directories. 16476910425Smrg exit 0 16576910425Smrgfi 16676910425Smrg 16776910425Smrgfor src 16876910425Smrgdo 16976910425Smrg # Protect names starting with `-'. 17076910425Smrg case $src in 17176910425Smrg -*) src=./$src ;; 17276910425Smrg esac 17376910425Smrg 17476910425Smrg if test -n "$dir_arg"; then 17576910425Smrg dst=$src 17676910425Smrg src= 17776910425Smrg 17876910425Smrg if test -d "$dst"; then 17976910425Smrg mkdircmd=: 18076910425Smrg chmodcmd= 18176910425Smrg else 18276910425Smrg mkdircmd=$mkdirprog 18376910425Smrg fi 18476910425Smrg else 18576910425Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 18676910425Smrg # might cause directories to be created, which would be especially bad 18776910425Smrg # if $src (and thus $dsttmp) contains '*'. 18876910425Smrg if test ! -f "$src" && test ! -d "$src"; then 18976910425Smrg echo "$0: $src does not exist." >&2 19076910425Smrg exit 1 19176910425Smrg fi 19276910425Smrg 19376910425Smrg if test -z "$dstarg"; then 19476910425Smrg echo "$0: no destination specified." >&2 19576910425Smrg exit 1 19676910425Smrg fi 19776910425Smrg 19876910425Smrg dst=$dstarg 19976910425Smrg # Protect names starting with `-'. 20076910425Smrg case $dst in 20176910425Smrg -*) dst=./$dst ;; 20276910425Smrg esac 20376910425Smrg 20476910425Smrg # If destination is a directory, append the input filename; won't work 20576910425Smrg # if double slashes aren't ignored. 20676910425Smrg if test -d "$dst"; then 20776910425Smrg if test -n "$no_target_directory"; then 20876910425Smrg echo "$0: $dstarg: Is a directory" >&2 20976910425Smrg exit 1 21076910425Smrg fi 21176910425Smrg dst=$dst/`basename "$src"` 21276910425Smrg fi 21376910425Smrg fi 21476910425Smrg 21576910425Smrg # This sed command emulates the dirname command. 21676910425Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 21776910425Smrg 21876910425Smrg # Make sure that the destination directory exists. 21976910425Smrg 22076910425Smrg # Skip lots of stat calls in the usual case. 22176910425Smrg if test ! -d "$dstdir"; then 22276910425Smrg defaultIFS=' 22376910425Smrg ' 22476910425Smrg IFS="${IFS-$defaultIFS}" 22576910425Smrg 22676910425Smrg oIFS=$IFS 22776910425Smrg # Some sh's can't handle IFS=/ for some reason. 22876910425Smrg IFS='%' 22976910425Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 23076910425Smrg shift 23176910425Smrg IFS=$oIFS 23276910425Smrg 23376910425Smrg pathcomp= 23476910425Smrg 23576910425Smrg while test $# -ne 0 ; do 23676910425Smrg pathcomp=$pathcomp$1 23776910425Smrg shift 23876910425Smrg if test ! -d "$pathcomp"; then 23976910425Smrg $mkdirprog "$pathcomp" 24076910425Smrg # mkdir can fail with a `File exist' error in case several 24176910425Smrg # install-sh are creating the directory concurrently. This 24276910425Smrg # is OK. 24376910425Smrg test -d "$pathcomp" || exit 24476910425Smrg fi 24576910425Smrg pathcomp=$pathcomp/ 24676910425Smrg done 24776910425Smrg fi 24876910425Smrg 24976910425Smrg if test -n "$dir_arg"; then 25076910425Smrg $doit $mkdircmd "$dst" \ 25176910425Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 25276910425Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 25376910425Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 25476910425Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 25576910425Smrg 25676910425Smrg else 25776910425Smrg dstfile=`basename "$dst"` 25876910425Smrg 25976910425Smrg # Make a couple of temp file names in the proper directory. 26076910425Smrg dsttmp=$dstdir/_inst.$$_ 26176910425Smrg rmtmp=$dstdir/_rm.$$_ 26276910425Smrg 26376910425Smrg # Trap to clean up those temp files at exit. 26476910425Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 26576910425Smrg trap '(exit $?); exit' 1 2 13 15 26676910425Smrg 26776910425Smrg # Copy the file name to the temp name. 26876910425Smrg $doit $cpprog "$src" "$dsttmp" && 26976910425Smrg 27076910425Smrg # and set any options; do chmod last to preserve setuid bits. 27176910425Smrg # 27276910425Smrg # If any of these fail, we abort the whole thing. If we want to 27376910425Smrg # ignore errors from any of these, just make sure not to ignore 27476910425Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 27576910425Smrg # 27676910425Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 27776910425Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 27876910425Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 27976910425Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 28076910425Smrg 28176910425Smrg # Now rename the file to the real destination. 28276910425Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 28376910425Smrg || { 28476910425Smrg # The rename failed, perhaps because mv can't rename something else 28576910425Smrg # to itself, or perhaps because mv is so ancient that it does not 28676910425Smrg # support -f. 28776910425Smrg 28876910425Smrg # Now remove or move aside any old file at destination location. 28976910425Smrg # We try this two ways since rm can't unlink itself on some 29076910425Smrg # systems and the destination file might be busy for other 29176910425Smrg # reasons. In this case, the final cleanup might fail but the new 29276910425Smrg # file should still install successfully. 29376910425Smrg { 29476910425Smrg if test -f "$dstdir/$dstfile"; then 29576910425Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 29676910425Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 29776910425Smrg || { 29876910425Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 29976910425Smrg (exit 1); exit 1 30076910425Smrg } 30176910425Smrg else 30276910425Smrg : 30376910425Smrg fi 30476910425Smrg } && 30576910425Smrg 30676910425Smrg # Now rename the file to the real destination. 30776910425Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 30876910425Smrg } 30976910425Smrg } 31076910425Smrg fi || { (exit 1); exit 1; } 31176910425Smrgdone 31276910425Smrg 31376910425Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 31476910425Smrg{ 31576910425Smrg (exit 0); exit 0 31676910425Smrg} 31776910425Smrg 31876910425Smrg# Local variables: 31976910425Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 32076910425Smrg# time-stamp-start: "scriptversion=" 32176910425Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 32276910425Smrg# time-stamp-end: "$" 32376910425Smrg# End: 324