install-sh revision 966bf024
1966bf024Smrg#!/bin/sh 2966bf024Smrg# 3966bf024Smrg# install - install a program, script, or datafile 4966bf024Smrg# 5966bf024Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 6966bf024Smrg# later released in X11R6 (xc/config/util/install.sh) with the 7966bf024Smrg# following copyright and license. 8966bf024Smrg# 9966bf024Smrg# Copyright (C) 1994 X Consortium 10966bf024Smrg# 11966bf024Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 12966bf024Smrg# of this software and associated documentation files (the "Software"), to 13966bf024Smrg# deal in the Software without restriction, including without limitation the 14966bf024Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 15966bf024Smrg# sell copies of the Software, and to permit persons to whom the Software is 16966bf024Smrg# furnished to do so, subject to the following conditions: 17966bf024Smrg# 18966bf024Smrg# The above copyright notice and this permission notice shall be included in 19966bf024Smrg# all copies or substantial portions of the Software. 20966bf024Smrg# 21966bf024Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22966bf024Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23966bf024Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24966bf024Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 25966bf024Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 26966bf024Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27966bf024Smrg# 28966bf024Smrg# Except as contained in this notice, the name of the X Consortium shall not 29966bf024Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 30966bf024Smrg# ings in this Software without prior written authorization from the X Consor- 31966bf024Smrg# tium. 32966bf024Smrg# 33966bf024Smrg# 34966bf024Smrg# FSF changes to this file are in the public domain. 35966bf024Smrg# 36966bf024Smrg# Calling this script install-sh is preferred over install.sh, to prevent 37966bf024Smrg# `make' implicit rules from creating a file called install from it 38966bf024Smrg# when there is no Makefile. 39966bf024Smrg# 40966bf024Smrg# This script is compatible with the BSD install script, but was written 41966bf024Smrg# from scratch. It can only install one file at a time, a restriction 42966bf024Smrg# shared with many OS's install programs. 43966bf024Smrg 44966bf024Smrg 45966bf024Smrg# set DOITPROG to echo to test this script 46966bf024Smrg 47966bf024Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 48966bf024Smrgdoit="${DOITPROG-}" 49966bf024Smrg 50966bf024Smrg 51966bf024Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 52966bf024Smrg 53966bf024Smrgmvprog="${MVPROG-mv}" 54966bf024Smrgcpprog="${CPPROG-cp}" 55966bf024Smrgchmodprog="${CHMODPROG-chmod}" 56966bf024Smrgchownprog="${CHOWNPROG-chown}" 57966bf024Smrgchgrpprog="${CHGRPPROG-chgrp}" 58966bf024Smrgstripprog="${STRIPPROG-strip}" 59966bf024Smrgrmprog="${RMPROG-rm}" 60966bf024Smrgmkdirprog="${MKDIRPROG-mkdir}" 61966bf024Smrg 62966bf024Smrgtransformbasename="" 63966bf024Smrgtransform_arg="" 64966bf024Smrginstcmd="$mvprog" 65966bf024Smrgchmodcmd="$chmodprog 0755" 66966bf024Smrgchowncmd="" 67966bf024Smrgchgrpcmd="" 68966bf024Smrgstripcmd="" 69966bf024Smrgrmcmd="$rmprog -f" 70966bf024Smrgmvcmd="$mvprog" 71966bf024Smrgsrc="" 72966bf024Smrgdst="" 73966bf024Smrgdir_arg="" 74966bf024Smrg 75966bf024Smrgwhile [ x"$1" != x ]; do 76966bf024Smrg case $1 in 77966bf024Smrg -c) instcmd=$cpprog 78966bf024Smrg shift 79966bf024Smrg continue;; 80966bf024Smrg 81966bf024Smrg -d) dir_arg=true 82966bf024Smrg shift 83966bf024Smrg continue;; 84966bf024Smrg 85966bf024Smrg -m) chmodcmd="$chmodprog $2" 86966bf024Smrg shift 87966bf024Smrg shift 88966bf024Smrg continue;; 89966bf024Smrg 90966bf024Smrg -o) chowncmd="$chownprog $2" 91966bf024Smrg shift 92966bf024Smrg shift 93966bf024Smrg continue;; 94966bf024Smrg 95966bf024Smrg -g) chgrpcmd="$chgrpprog $2" 96966bf024Smrg shift 97966bf024Smrg shift 98966bf024Smrg continue;; 99966bf024Smrg 100966bf024Smrg -s) stripcmd=$stripprog 101966bf024Smrg shift 102966bf024Smrg continue;; 103966bf024Smrg 104966bf024Smrg -t=*) transformarg=`echo $1 | sed 's/-t=//'` 105966bf024Smrg shift 106966bf024Smrg continue;; 107966bf024Smrg 108966bf024Smrg -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 109966bf024Smrg shift 110966bf024Smrg continue;; 111966bf024Smrg 112966bf024Smrg *) if [ x"$src" = x ] 113966bf024Smrg then 114966bf024Smrg src=$1 115966bf024Smrg else 116966bf024Smrg # this colon is to work around a 386BSD /bin/sh bug 117966bf024Smrg : 118966bf024Smrg dst=$1 119966bf024Smrg fi 120966bf024Smrg shift 121966bf024Smrg continue;; 122966bf024Smrg esac 123966bf024Smrgdone 124966bf024Smrg 125966bf024Smrgif [ x"$src" = x ] 126966bf024Smrgthen 127966bf024Smrg echo "$0: no input file specified" >&2 128966bf024Smrg exit 1 129966bf024Smrgelse 130966bf024Smrg : 131966bf024Smrgfi 132966bf024Smrg 133966bf024Smrgif [ x"$dir_arg" != x ]; then 134966bf024Smrg dst=$src 135966bf024Smrg src="" 136966bf024Smrg 137966bf024Smrg if [ -d "$dst" ]; then 138966bf024Smrg instcmd=: 139966bf024Smrg chmodcmd="" 140966bf024Smrg else 141966bf024Smrg instcmd=$mkdirprog 142966bf024Smrg fi 143966bf024Smrgelse 144966bf024Smrg 145966bf024Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command 146966bf024Smrg# might cause directories to be created, which would be especially bad 147966bf024Smrg# if $src (and thus $dsttmp) contains '*'. 148966bf024Smrg 149966bf024Smrg if [ -f "$src" ] || [ -d "$src" ] 150966bf024Smrg then 151966bf024Smrg : 152966bf024Smrg else 153966bf024Smrg echo "$0: $src does not exist" >&2 154966bf024Smrg exit 1 155966bf024Smrg fi 156966bf024Smrg 157966bf024Smrg if [ x"$dst" = x ] 158966bf024Smrg then 159966bf024Smrg echo "$0: no destination specified" >&2 160966bf024Smrg exit 1 161966bf024Smrg else 162966bf024Smrg : 163966bf024Smrg fi 164966bf024Smrg 165966bf024Smrg# If destination is a directory, append the input filename; if your system 166966bf024Smrg# does not like double slashes in filenames, you may need to add some logic 167966bf024Smrg 168966bf024Smrg if [ -d "$dst" ] 169966bf024Smrg then 170966bf024Smrg dst=$dst/`basename "$src"` 171966bf024Smrg else 172966bf024Smrg : 173966bf024Smrg fi 174966bf024Smrgfi 175966bf024Smrg 176966bf024Smrg## this sed command emulates the dirname command 177966bf024Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 178966bf024Smrg 179966bf024Smrg# Make sure that the destination directory exists. 180966bf024Smrg# this part is taken from Noah Friedman's mkinstalldirs script 181966bf024Smrg 182966bf024Smrg# Skip lots of stat calls in the usual case. 183966bf024Smrgif [ ! -d "$dstdir" ]; then 184966bf024SmrgdefaultIFS=' 185966bf024Smrg ' 186966bf024SmrgIFS="${IFS-$defaultIFS}" 187966bf024Smrg 188966bf024SmrgoIFS=$IFS 189966bf024Smrg# Some sh's can't handle IFS=/ for some reason. 190966bf024SmrgIFS='%' 191966bf024Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 192966bf024SmrgIFS=$oIFS 193966bf024Smrg 194966bf024Smrgpathcomp='' 195966bf024Smrg 196966bf024Smrgwhile [ $# -ne 0 ] ; do 197966bf024Smrg pathcomp=$pathcomp$1 198966bf024Smrg shift 199966bf024Smrg 200966bf024Smrg if [ ! -d "$pathcomp" ] ; 201966bf024Smrg then 202966bf024Smrg $mkdirprog "$pathcomp" 203966bf024Smrg else 204966bf024Smrg : 205966bf024Smrg fi 206966bf024Smrg 207966bf024Smrg pathcomp=$pathcomp/ 208966bf024Smrgdone 209966bf024Smrgfi 210966bf024Smrg 211966bf024Smrgif [ x"$dir_arg" != x ] 212966bf024Smrgthen 213966bf024Smrg $doit $instcmd "$dst" && 214966bf024Smrg 215966bf024Smrg if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi && 216966bf024Smrg if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi && 217966bf024Smrg if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi && 218966bf024Smrg if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi 219966bf024Smrgelse 220966bf024Smrg 221966bf024Smrg# If we're going to rename the final executable, determine the name now. 222966bf024Smrg 223966bf024Smrg if [ x"$transformarg" = x ] 224966bf024Smrg then 225966bf024Smrg dstfile=`basename "$dst"` 226966bf024Smrg else 227966bf024Smrg dstfile=`basename "$dst" $transformbasename | 228966bf024Smrg sed $transformarg`$transformbasename 229966bf024Smrg fi 230966bf024Smrg 231966bf024Smrg# don't allow the sed command to completely eliminate the filename 232966bf024Smrg 233966bf024Smrg if [ x"$dstfile" = x ] 234966bf024Smrg then 235966bf024Smrg dstfile=`basename "$dst"` 236966bf024Smrg else 237966bf024Smrg : 238966bf024Smrg fi 239966bf024Smrg 240966bf024Smrg# Make a couple of temp file names in the proper directory. 241966bf024Smrg 242966bf024Smrg dsttmp=$dstdir/_inst.$$_ 243966bf024Smrg rmtmp=$dstdir/_rm.$$_ 244966bf024Smrg 245966bf024Smrg# Trap to clean up temp files at exit. 246966bf024Smrg 247966bf024Smrg trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 248966bf024Smrg trap '(exit $?); exit' 1 2 13 15 249966bf024Smrg 250966bf024Smrg# Move or copy the file name to the temp name 251966bf024Smrg 252966bf024Smrg $doit $instcmd "$src" "$dsttmp" && 253966bf024Smrg 254966bf024Smrg# and set any options; do chmod last to preserve setuid bits 255966bf024Smrg 256966bf024Smrg# If any of these fail, we abort the whole thing. If we want to 257966bf024Smrg# ignore errors from any of these, just make sure not to ignore 258966bf024Smrg# errors from the above "$doit $instcmd $src $dsttmp" command. 259966bf024Smrg 260966bf024Smrg if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi && 261966bf024Smrg if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi && 262966bf024Smrg if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi && 263966bf024Smrg if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi && 264966bf024Smrg 265966bf024Smrg# Now remove or move aside any old file at destination location. We try this 266966bf024Smrg# two ways since rm can't unlink itself on some systems and the destination 267966bf024Smrg# file might be busy for other reasons. In this case, the final cleanup 268966bf024Smrg# might fail but the new file should still install successfully. 269966bf024Smrg 270966bf024Smrg{ 271966bf024Smrg if [ -f "$dstdir/$dstfile" ] 272966bf024Smrg then 273966bf024Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null || 274966bf024Smrg $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null || 275966bf024Smrg { 276966bf024Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 277966bf024Smrg (exit 1); exit 278966bf024Smrg } 279966bf024Smrg else 280966bf024Smrg : 281966bf024Smrg fi 282966bf024Smrg} && 283966bf024Smrg 284966bf024Smrg# Now rename the file to the real destination. 285966bf024Smrg 286966bf024Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 287966bf024Smrg 288966bf024Smrgfi && 289966bf024Smrg 290966bf024Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 291966bf024Smrg 292966bf024Smrg{ 293966bf024Smrg (exit 0); exit 294966bf024Smrg} 295