1*4a17663cSThomas Veerman#! /bin/sh 2*4a17663cSThomas Veerman# 3*4a17663cSThomas Veerman# install - install a program, script, or datafile 4*4a17663cSThomas Veerman# 5*4a17663cSThomas Veerman# This originates from X11R5 (mit/util/scripts/install.sh), which was 6*4a17663cSThomas Veerman# later released in X11R6 (xc/config/util/install.sh) with the 7*4a17663cSThomas Veerman# following copyright and license. 8*4a17663cSThomas Veerman# 9*4a17663cSThomas Veerman# Copyright (C) 1994 X Consortium 10*4a17663cSThomas Veerman# 11*4a17663cSThomas Veerman# Permission is hereby granted, free of charge, to any person obtaining a copy 12*4a17663cSThomas Veerman# of this software and associated documentation files (the "Software"), to 13*4a17663cSThomas Veerman# deal in the Software without restriction, including without limitation the 14*4a17663cSThomas Veerman# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 15*4a17663cSThomas Veerman# sell copies of the Software, and to permit persons to whom the Software is 16*4a17663cSThomas Veerman# furnished to do so, subject to the following conditions: 17*4a17663cSThomas Veerman# 18*4a17663cSThomas Veerman# The above copyright notice and this permission notice shall be included in 19*4a17663cSThomas Veerman# all copies or substantial portions of the Software. 20*4a17663cSThomas Veerman# 21*4a17663cSThomas Veerman# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22*4a17663cSThomas Veerman# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23*4a17663cSThomas Veerman# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24*4a17663cSThomas Veerman# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 25*4a17663cSThomas Veerman# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 26*4a17663cSThomas Veerman# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27*4a17663cSThomas Veerman# 28*4a17663cSThomas Veerman# Except as contained in this notice, the name of the X Consortium shall not 29*4a17663cSThomas Veerman# be used in advertising or otherwise to promote the sale, use or other deal- 30*4a17663cSThomas Veerman# ings in this Software without prior written authorization from the X Consor- 31*4a17663cSThomas Veerman# tium. 32*4a17663cSThomas Veerman# 33*4a17663cSThomas Veerman# 34*4a17663cSThomas Veerman# FSF changes to this file are in the public domain. 35*4a17663cSThomas Veerman# 36*4a17663cSThomas Veerman# Calling this script install-sh is preferred over install.sh, to prevent 37*4a17663cSThomas Veerman# `make' implicit rules from creating a file called install from it 38*4a17663cSThomas Veerman# when there is no Makefile. 39*4a17663cSThomas Veerman# 40*4a17663cSThomas Veerman# This script is compatible with the BSD install script, but was written 41*4a17663cSThomas Veerman# from scratch. It can only install one file at a time, a restriction 42*4a17663cSThomas Veerman# shared with many OS's install programs. 43*4a17663cSThomas Veerman 44*4a17663cSThomas Veerman 45*4a17663cSThomas Veerman# set DOITPROG to echo to test this script 46*4a17663cSThomas Veerman 47*4a17663cSThomas Veerman# Don't use :- since 4.3BSD and earlier shells don't like it. 48*4a17663cSThomas Veermandoit="${DOITPROG-}" 49*4a17663cSThomas Veerman 50*4a17663cSThomas Veerman 51*4a17663cSThomas Veerman# put in absolute paths if you don't have them in your path; or use env. vars. 52*4a17663cSThomas Veerman 53*4a17663cSThomas Veermanmvprog="${MVPROG-mv}" 54*4a17663cSThomas Veermancpprog="${CPPROG-cp}" 55*4a17663cSThomas Veermanchmodprog="${CHMODPROG-chmod}" 56*4a17663cSThomas Veermanchownprog="${CHOWNPROG-chown}" 57*4a17663cSThomas Veermanchgrpprog="${CHGRPPROG-chgrp}" 58*4a17663cSThomas Veermanstripprog="${STRIPPROG-strip}" 59*4a17663cSThomas Veermanrmprog="${RMPROG-rm}" 60*4a17663cSThomas Veermanmkdirprog="${MKDIRPROG-mkdir}" 61*4a17663cSThomas Veerman 62*4a17663cSThomas Veermantransformbasename="" 63*4a17663cSThomas Veermantransform_arg="" 64*4a17663cSThomas Veermaninstcmd="$mvprog" 65*4a17663cSThomas Veermanchmodcmd="$chmodprog 0755" 66*4a17663cSThomas Veermanchowncmd="" 67*4a17663cSThomas Veermanchgrpcmd="" 68*4a17663cSThomas Veermanstripcmd="" 69*4a17663cSThomas Veermanrmcmd="$rmprog -f" 70*4a17663cSThomas Veermanmvcmd="$mvprog" 71*4a17663cSThomas Veermansrc="" 72*4a17663cSThomas Veermandst="" 73*4a17663cSThomas Veermandir_arg="" 74*4a17663cSThomas Veerman 75*4a17663cSThomas Veermanwhile [ x"$1" != x ]; do 76*4a17663cSThomas Veerman case $1 in 77*4a17663cSThomas Veerman -c) instcmd=$cpprog 78*4a17663cSThomas Veerman shift 79*4a17663cSThomas Veerman continue;; 80*4a17663cSThomas Veerman 81*4a17663cSThomas Veerman -d) dir_arg=true 82*4a17663cSThomas Veerman shift 83*4a17663cSThomas Veerman continue;; 84*4a17663cSThomas Veerman 85*4a17663cSThomas Veerman -m) chmodcmd="$chmodprog $2" 86*4a17663cSThomas Veerman shift 87*4a17663cSThomas Veerman shift 88*4a17663cSThomas Veerman continue;; 89*4a17663cSThomas Veerman 90*4a17663cSThomas Veerman -o) chowncmd="$chownprog $2" 91*4a17663cSThomas Veerman shift 92*4a17663cSThomas Veerman shift 93*4a17663cSThomas Veerman continue;; 94*4a17663cSThomas Veerman 95*4a17663cSThomas Veerman -g) chgrpcmd="$chgrpprog $2" 96*4a17663cSThomas Veerman shift 97*4a17663cSThomas Veerman shift 98*4a17663cSThomas Veerman continue;; 99*4a17663cSThomas Veerman 100*4a17663cSThomas Veerman -s) stripcmd=$stripprog 101*4a17663cSThomas Veerman shift 102*4a17663cSThomas Veerman continue;; 103*4a17663cSThomas Veerman 104*4a17663cSThomas Veerman -t=*) transformarg=`echo $1 | sed 's/-t=//'` 105*4a17663cSThomas Veerman shift 106*4a17663cSThomas Veerman continue;; 107*4a17663cSThomas Veerman 108*4a17663cSThomas Veerman -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 109*4a17663cSThomas Veerman shift 110*4a17663cSThomas Veerman continue;; 111*4a17663cSThomas Veerman 112*4a17663cSThomas Veerman *) if [ x"$src" = x ] 113*4a17663cSThomas Veerman then 114*4a17663cSThomas Veerman src=$1 115*4a17663cSThomas Veerman else 116*4a17663cSThomas Veerman # this colon is to work around a 386BSD /bin/sh bug 117*4a17663cSThomas Veerman : 118*4a17663cSThomas Veerman dst=$1 119*4a17663cSThomas Veerman fi 120*4a17663cSThomas Veerman shift 121*4a17663cSThomas Veerman continue;; 122*4a17663cSThomas Veerman esac 123*4a17663cSThomas Veermandone 124*4a17663cSThomas Veerman 125*4a17663cSThomas Veermanif [ x"$src" = x ] 126*4a17663cSThomas Veermanthen 127*4a17663cSThomas Veerman echo "$0: no input file specified" >&2 128*4a17663cSThomas Veerman exit 1 129*4a17663cSThomas Veermanelse 130*4a17663cSThomas Veerman : 131*4a17663cSThomas Veermanfi 132*4a17663cSThomas Veerman 133*4a17663cSThomas Veermanif [ x"$dir_arg" != x ]; then 134*4a17663cSThomas Veerman dst=$src 135*4a17663cSThomas Veerman src="" 136*4a17663cSThomas Veerman 137*4a17663cSThomas Veerman if [ -d "$dst" ]; then 138*4a17663cSThomas Veerman instcmd=: 139*4a17663cSThomas Veerman chmodcmd="" 140*4a17663cSThomas Veerman else 141*4a17663cSThomas Veerman instcmd=$mkdirprog 142*4a17663cSThomas Veerman fi 143*4a17663cSThomas Veermanelse 144*4a17663cSThomas Veerman 145*4a17663cSThomas Veerman# Waiting for this to be detected by the "$instcmd $src $dsttmp" command 146*4a17663cSThomas Veerman# might cause directories to be created, which would be especially bad 147*4a17663cSThomas Veerman# if $src (and thus $dsttmp) contains '*'. 148*4a17663cSThomas Veerman 149*4a17663cSThomas Veerman if [ -f "$src" ] || [ -d "$src" ] 150*4a17663cSThomas Veerman then 151*4a17663cSThomas Veerman : 152*4a17663cSThomas Veerman else 153*4a17663cSThomas Veerman echo "$0: $src does not exist" >&2 154*4a17663cSThomas Veerman exit 1 155*4a17663cSThomas Veerman fi 156*4a17663cSThomas Veerman 157*4a17663cSThomas Veerman if [ x"$dst" = x ] 158*4a17663cSThomas Veerman then 159*4a17663cSThomas Veerman echo "$0: no destination specified" >&2 160*4a17663cSThomas Veerman exit 1 161*4a17663cSThomas Veerman else 162*4a17663cSThomas Veerman : 163*4a17663cSThomas Veerman fi 164*4a17663cSThomas Veerman 165*4a17663cSThomas Veerman# If destination is a directory, append the input filename; if your system 166*4a17663cSThomas Veerman# does not like double slashes in filenames, you may need to add some logic 167*4a17663cSThomas Veerman 168*4a17663cSThomas Veerman if [ -d "$dst" ] 169*4a17663cSThomas Veerman then 170*4a17663cSThomas Veerman dst=$dst/`basename "$src"` 171*4a17663cSThomas Veerman else 172*4a17663cSThomas Veerman : 173*4a17663cSThomas Veerman fi 174*4a17663cSThomas Veermanfi 175*4a17663cSThomas Veerman 176*4a17663cSThomas Veerman## this sed command emulates the dirname command 177*4a17663cSThomas Veermandstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 178*4a17663cSThomas Veerman 179*4a17663cSThomas Veerman# Make sure that the destination directory exists. 180*4a17663cSThomas Veerman# this part is taken from Noah Friedman's mkinstalldirs script 181*4a17663cSThomas Veerman 182*4a17663cSThomas Veerman# Skip lots of stat calls in the usual case. 183*4a17663cSThomas Veermanif [ ! -d "$dstdir" ]; then 184*4a17663cSThomas VeermandefaultIFS=' 185*4a17663cSThomas Veerman ' 186*4a17663cSThomas VeermanIFS="${IFS-$defaultIFS}" 187*4a17663cSThomas Veerman 188*4a17663cSThomas VeermanoIFS=$IFS 189*4a17663cSThomas Veerman# Some sh's can't handle IFS=/ for some reason. 190*4a17663cSThomas VeermanIFS='%' 191*4a17663cSThomas Veermanset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 192*4a17663cSThomas VeermanIFS=$oIFS 193*4a17663cSThomas Veerman 194*4a17663cSThomas Veermanpathcomp='' 195*4a17663cSThomas Veerman 196*4a17663cSThomas Veermanwhile [ $# -ne 0 ] ; do 197*4a17663cSThomas Veerman pathcomp=$pathcomp$1 198*4a17663cSThomas Veerman shift 199*4a17663cSThomas Veerman 200*4a17663cSThomas Veerman if [ ! -d "$pathcomp" ] ; 201*4a17663cSThomas Veerman then 202*4a17663cSThomas Veerman $mkdirprog "$pathcomp" 203*4a17663cSThomas Veerman else 204*4a17663cSThomas Veerman : 205*4a17663cSThomas Veerman fi 206*4a17663cSThomas Veerman 207*4a17663cSThomas Veerman pathcomp=$pathcomp/ 208*4a17663cSThomas Veermandone 209*4a17663cSThomas Veermanfi 210*4a17663cSThomas Veerman 211*4a17663cSThomas Veermanif [ x"$dir_arg" != x ] 212*4a17663cSThomas Veermanthen 213*4a17663cSThomas Veerman $doit $instcmd "$dst" && 214*4a17663cSThomas Veerman 215*4a17663cSThomas Veerman if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi && 216*4a17663cSThomas Veerman if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi && 217*4a17663cSThomas Veerman if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi && 218*4a17663cSThomas Veerman if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi 219*4a17663cSThomas Veermanelse 220*4a17663cSThomas Veerman 221*4a17663cSThomas Veerman# If we're going to rename the final executable, determine the name now. 222*4a17663cSThomas Veerman 223*4a17663cSThomas Veerman if [ x"$transformarg" = x ] 224*4a17663cSThomas Veerman then 225*4a17663cSThomas Veerman dstfile=`basename "$dst"` 226*4a17663cSThomas Veerman else 227*4a17663cSThomas Veerman dstfile=`basename "$dst" $transformbasename | 228*4a17663cSThomas Veerman sed $transformarg`$transformbasename 229*4a17663cSThomas Veerman fi 230*4a17663cSThomas Veerman 231*4a17663cSThomas Veerman# don't allow the sed command to completely eliminate the filename 232*4a17663cSThomas Veerman 233*4a17663cSThomas Veerman if [ x"$dstfile" = x ] 234*4a17663cSThomas Veerman then 235*4a17663cSThomas Veerman dstfile=`basename "$dst"` 236*4a17663cSThomas Veerman else 237*4a17663cSThomas Veerman : 238*4a17663cSThomas Veerman fi 239*4a17663cSThomas Veerman 240*4a17663cSThomas Veerman# Make a couple of temp file names in the proper directory. 241*4a17663cSThomas Veerman 242*4a17663cSThomas Veerman dsttmp=$dstdir/#inst.$$# 243*4a17663cSThomas Veerman rmtmp=$dstdir/#rm.$$# 244*4a17663cSThomas Veerman 245*4a17663cSThomas Veerman# Trap to clean up temp files at exit. 246*4a17663cSThomas Veerman 247*4a17663cSThomas Veerman trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 248*4a17663cSThomas Veerman trap '(exit $?); exit' 1 2 13 15 249*4a17663cSThomas Veerman 250*4a17663cSThomas Veerman# Move or copy the file name to the temp name 251*4a17663cSThomas Veerman 252*4a17663cSThomas Veerman $doit $instcmd "$src" "$dsttmp" && 253*4a17663cSThomas Veerman 254*4a17663cSThomas Veerman# and set any options; do chmod last to preserve setuid bits 255*4a17663cSThomas Veerman 256*4a17663cSThomas Veerman# If any of these fail, we abort the whole thing. If we want to 257*4a17663cSThomas Veerman# ignore errors from any of these, just make sure not to ignore 258*4a17663cSThomas Veerman# errors from the above "$doit $instcmd $src $dsttmp" command. 259*4a17663cSThomas Veerman 260*4a17663cSThomas Veerman if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi && 261*4a17663cSThomas Veerman if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi && 262*4a17663cSThomas Veerman if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi && 263*4a17663cSThomas Veerman if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi && 264*4a17663cSThomas Veerman 265*4a17663cSThomas Veerman# Now remove or move aside any old file at destination location. We try this 266*4a17663cSThomas Veerman# two ways since rm can't unlink itself on some systems and the destination 267*4a17663cSThomas Veerman# file might be busy for other reasons. In this case, the final cleanup 268*4a17663cSThomas Veerman# might fail but the new file should still install successfully. 269*4a17663cSThomas Veerman 270*4a17663cSThomas Veerman{ 271*4a17663cSThomas Veerman if [ -f "$dstdir/$dstfile" ] 272*4a17663cSThomas Veerman then 273*4a17663cSThomas Veerman $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null || 274*4a17663cSThomas Veerman $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null || 275*4a17663cSThomas Veerman { 276*4a17663cSThomas Veerman echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 277*4a17663cSThomas Veerman (exit 1); exit 278*4a17663cSThomas Veerman } 279*4a17663cSThomas Veerman else 280*4a17663cSThomas Veerman : 281*4a17663cSThomas Veerman fi 282*4a17663cSThomas Veerman} && 283*4a17663cSThomas Veerman 284*4a17663cSThomas Veerman# Now rename the file to the real destination. 285*4a17663cSThomas Veerman 286*4a17663cSThomas Veerman $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 287*4a17663cSThomas Veerman 288*4a17663cSThomas Veermanfi && 289*4a17663cSThomas Veerman 290*4a17663cSThomas Veerman# The final little trick to "correctly" pass the exit status to the exit trap. 291*4a17663cSThomas Veerman 292*4a17663cSThomas Veerman{ 293*4a17663cSThomas Veerman (exit 0); exit 294*4a17663cSThomas Veerman} 295