1*ebfedea0SLionel Sambuc#!/bin/sh 2*ebfedea0SLionel Sambuc# 3*ebfedea0SLionel Sambuc# install - install a program, script, or datafile 4*ebfedea0SLionel Sambuc# This comes from X11R5 (mit/util/scripts/install.sh). 5*ebfedea0SLionel Sambuc# 6*ebfedea0SLionel Sambuc# Copyright 1991 by the Massachusetts Institute of Technology 7*ebfedea0SLionel Sambuc# 8*ebfedea0SLionel Sambuc# Permission to use, copy, modify, distribute, and sell this software and its 9*ebfedea0SLionel Sambuc# documentation for any purpose is hereby granted without fee, provided that 10*ebfedea0SLionel Sambuc# the above copyright notice appear in all copies and that both that 11*ebfedea0SLionel Sambuc# copyright notice and this permission notice appear in supporting 12*ebfedea0SLionel Sambuc# documentation, and that the name of M.I.T. not be used in advertising or 13*ebfedea0SLionel Sambuc# publicity pertaining to distribution of the software without specific, 14*ebfedea0SLionel Sambuc# written prior permission. M.I.T. makes no representations about the 15*ebfedea0SLionel Sambuc# suitability of this software for any purpose. It is provided "as is" 16*ebfedea0SLionel Sambuc# without express or implied warranty. 17*ebfedea0SLionel Sambuc# 18*ebfedea0SLionel Sambuc# Calling this script install-sh is preferred over install.sh, to prevent 19*ebfedea0SLionel Sambuc# `make' implicit rules from creating a file called install from it 20*ebfedea0SLionel Sambuc# when there is no Makefile. 21*ebfedea0SLionel Sambuc# 22*ebfedea0SLionel Sambuc# This script is compatible with the BSD install script, but was written 23*ebfedea0SLionel Sambuc# from scratch. It can only install one file at a time, a restriction 24*ebfedea0SLionel Sambuc# shared with many OS's install programs. 25*ebfedea0SLionel Sambuc 26*ebfedea0SLionel Sambuc 27*ebfedea0SLionel Sambuc# set DOITPROG to echo to test this script 28*ebfedea0SLionel Sambuc 29*ebfedea0SLionel Sambuc# Don't use :- since 4.3BSD and earlier shells don't like it. 30*ebfedea0SLionel Sambucdoit="${DOITPROG-}" 31*ebfedea0SLionel Sambuc 32*ebfedea0SLionel Sambuc 33*ebfedea0SLionel Sambuc# put in absolute paths if you don't have them in your path; or use env. vars. 34*ebfedea0SLionel Sambuc 35*ebfedea0SLionel Sambucmvprog="${MVPROG-mv}" 36*ebfedea0SLionel Sambuccpprog="${CPPROG-cp}" 37*ebfedea0SLionel Sambucchmodprog="${CHMODPROG-chmod}" 38*ebfedea0SLionel Sambucchownprog="${CHOWNPROG-chown}" 39*ebfedea0SLionel Sambucchgrpprog="${CHGRPPROG-chgrp}" 40*ebfedea0SLionel Sambucstripprog="${STRIPPROG-strip}" 41*ebfedea0SLionel Sambucrmprog="${RMPROG-rm}" 42*ebfedea0SLionel Sambucmkdirprog="${MKDIRPROG-mkdir}" 43*ebfedea0SLionel Sambuc 44*ebfedea0SLionel Sambuctransformbasename="" 45*ebfedea0SLionel Sambuctransform_arg="" 46*ebfedea0SLionel Sambucinstcmd="$mvprog" 47*ebfedea0SLionel Sambucchmodcmd="$chmodprog 0755" 48*ebfedea0SLionel Sambucchowncmd="" 49*ebfedea0SLionel Sambucchgrpcmd="" 50*ebfedea0SLionel Sambucstripcmd="" 51*ebfedea0SLionel Sambucrmcmd="$rmprog -f" 52*ebfedea0SLionel Sambucmvcmd="$mvprog" 53*ebfedea0SLionel Sambucsrc="" 54*ebfedea0SLionel Sambucdst="" 55*ebfedea0SLionel Sambucdir_arg="" 56*ebfedea0SLionel Sambuc 57*ebfedea0SLionel Sambucwhile [ x"$1" != x ]; do 58*ebfedea0SLionel Sambuc case $1 in 59*ebfedea0SLionel Sambuc -c) instcmd="$cpprog" 60*ebfedea0SLionel Sambuc shift 61*ebfedea0SLionel Sambuc continue;; 62*ebfedea0SLionel Sambuc 63*ebfedea0SLionel Sambuc -d) dir_arg=true 64*ebfedea0SLionel Sambuc shift 65*ebfedea0SLionel Sambuc continue;; 66*ebfedea0SLionel Sambuc 67*ebfedea0SLionel Sambuc -m) chmodcmd="$chmodprog $2" 68*ebfedea0SLionel Sambuc shift 69*ebfedea0SLionel Sambuc shift 70*ebfedea0SLionel Sambuc continue;; 71*ebfedea0SLionel Sambuc 72*ebfedea0SLionel Sambuc -o) chowncmd="$chownprog $2" 73*ebfedea0SLionel Sambuc shift 74*ebfedea0SLionel Sambuc shift 75*ebfedea0SLionel Sambuc continue;; 76*ebfedea0SLionel Sambuc 77*ebfedea0SLionel Sambuc -g) chgrpcmd="$chgrpprog $2" 78*ebfedea0SLionel Sambuc shift 79*ebfedea0SLionel Sambuc shift 80*ebfedea0SLionel Sambuc continue;; 81*ebfedea0SLionel Sambuc 82*ebfedea0SLionel Sambuc -s) stripcmd="$stripprog" 83*ebfedea0SLionel Sambuc shift 84*ebfedea0SLionel Sambuc continue;; 85*ebfedea0SLionel Sambuc 86*ebfedea0SLionel Sambuc -t=*) transformarg=`echo $1 | sed 's/-t=//'` 87*ebfedea0SLionel Sambuc shift 88*ebfedea0SLionel Sambuc continue;; 89*ebfedea0SLionel Sambuc 90*ebfedea0SLionel Sambuc -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 91*ebfedea0SLionel Sambuc shift 92*ebfedea0SLionel Sambuc continue;; 93*ebfedea0SLionel Sambuc 94*ebfedea0SLionel Sambuc *) if [ x"$src" = x ] 95*ebfedea0SLionel Sambuc then 96*ebfedea0SLionel Sambuc src=$1 97*ebfedea0SLionel Sambuc else 98*ebfedea0SLionel Sambuc # this colon is to work around a 386BSD /bin/sh bug 99*ebfedea0SLionel Sambuc : 100*ebfedea0SLionel Sambuc dst=$1 101*ebfedea0SLionel Sambuc fi 102*ebfedea0SLionel Sambuc shift 103*ebfedea0SLionel Sambuc continue;; 104*ebfedea0SLionel Sambuc esac 105*ebfedea0SLionel Sambucdone 106*ebfedea0SLionel Sambuc 107*ebfedea0SLionel Sambucif [ x"$src" = x ] 108*ebfedea0SLionel Sambucthen 109*ebfedea0SLionel Sambuc echo "install: no input file specified" 110*ebfedea0SLionel Sambuc exit 1 111*ebfedea0SLionel Sambucelse 112*ebfedea0SLionel Sambuc true 113*ebfedea0SLionel Sambucfi 114*ebfedea0SLionel Sambuc 115*ebfedea0SLionel Sambucif [ x"$dir_arg" != x ]; then 116*ebfedea0SLionel Sambuc dst=$src 117*ebfedea0SLionel Sambuc src="" 118*ebfedea0SLionel Sambuc 119*ebfedea0SLionel Sambuc if [ -d $dst ]; then 120*ebfedea0SLionel Sambuc instcmd=: 121*ebfedea0SLionel Sambuc chmodcmd="" 122*ebfedea0SLionel Sambuc else 123*ebfedea0SLionel Sambuc instcmd=mkdir 124*ebfedea0SLionel Sambuc fi 125*ebfedea0SLionel Sambucelse 126*ebfedea0SLionel Sambuc 127*ebfedea0SLionel Sambuc# Waiting for this to be detected by the "$instcmd $src $dsttmp" command 128*ebfedea0SLionel Sambuc# might cause directories to be created, which would be especially bad 129*ebfedea0SLionel Sambuc# if $src (and thus $dsttmp) contains '*'. 130*ebfedea0SLionel Sambuc 131*ebfedea0SLionel Sambuc if [ -f $src -o -d $src ] 132*ebfedea0SLionel Sambuc then 133*ebfedea0SLionel Sambuc true 134*ebfedea0SLionel Sambuc else 135*ebfedea0SLionel Sambuc echo "install: $src does not exist" 136*ebfedea0SLionel Sambuc exit 1 137*ebfedea0SLionel Sambuc fi 138*ebfedea0SLionel Sambuc 139*ebfedea0SLionel Sambuc if [ x"$dst" = x ] 140*ebfedea0SLionel Sambuc then 141*ebfedea0SLionel Sambuc echo "install: no destination specified" 142*ebfedea0SLionel Sambuc exit 1 143*ebfedea0SLionel Sambuc else 144*ebfedea0SLionel Sambuc true 145*ebfedea0SLionel Sambuc fi 146*ebfedea0SLionel Sambuc 147*ebfedea0SLionel Sambuc# If destination is a directory, append the input filename; if your system 148*ebfedea0SLionel Sambuc# does not like double slashes in filenames, you may need to add some logic 149*ebfedea0SLionel Sambuc 150*ebfedea0SLionel Sambuc if [ -d $dst ] 151*ebfedea0SLionel Sambuc then 152*ebfedea0SLionel Sambuc dst="$dst"/`basename $src` 153*ebfedea0SLionel Sambuc else 154*ebfedea0SLionel Sambuc true 155*ebfedea0SLionel Sambuc fi 156*ebfedea0SLionel Sambucfi 157*ebfedea0SLionel Sambuc 158*ebfedea0SLionel Sambuc## this sed command emulates the dirname command 159*ebfedea0SLionel Sambucdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` 160*ebfedea0SLionel Sambuc 161*ebfedea0SLionel Sambuc# Make sure that the destination directory exists. 162*ebfedea0SLionel Sambuc# this part is taken from Noah Friedman's mkinstalldirs script 163*ebfedea0SLionel Sambuc 164*ebfedea0SLionel Sambuc# Skip lots of stat calls in the usual case. 165*ebfedea0SLionel Sambucif [ ! -d "$dstdir" ]; then 166*ebfedea0SLionel SambucdefaultIFS=' 167*ebfedea0SLionel Sambuc' 168*ebfedea0SLionel SambucIFS="${IFS-${defaultIFS}}" 169*ebfedea0SLionel Sambuc 170*ebfedea0SLionel SambucoIFS="${IFS}" 171*ebfedea0SLionel Sambuc# Some sh's can't handle IFS=/ for some reason. 172*ebfedea0SLionel SambucIFS='%' 173*ebfedea0SLionel Sambucset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'` 174*ebfedea0SLionel SambucIFS="${oIFS}" 175*ebfedea0SLionel Sambuc 176*ebfedea0SLionel Sambucpathcomp='' 177*ebfedea0SLionel Sambuc 178*ebfedea0SLionel Sambucwhile [ $# -ne 0 ] ; do 179*ebfedea0SLionel Sambuc pathcomp="${pathcomp}${1}" 180*ebfedea0SLionel Sambuc shift 181*ebfedea0SLionel Sambuc 182*ebfedea0SLionel Sambuc if [ ! -d "${pathcomp}" ] ; 183*ebfedea0SLionel Sambuc then 184*ebfedea0SLionel Sambuc $mkdirprog "${pathcomp}" 185*ebfedea0SLionel Sambuc else 186*ebfedea0SLionel Sambuc true 187*ebfedea0SLionel Sambuc fi 188*ebfedea0SLionel Sambuc 189*ebfedea0SLionel Sambuc pathcomp="${pathcomp}/" 190*ebfedea0SLionel Sambucdone 191*ebfedea0SLionel Sambucfi 192*ebfedea0SLionel Sambuc 193*ebfedea0SLionel Sambucif [ x"$dir_arg" != x ] 194*ebfedea0SLionel Sambucthen 195*ebfedea0SLionel Sambuc $doit $instcmd $dst && 196*ebfedea0SLionel Sambuc 197*ebfedea0SLionel Sambuc if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi && 198*ebfedea0SLionel Sambuc if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi && 199*ebfedea0SLionel Sambuc if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi && 200*ebfedea0SLionel Sambuc if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi 201*ebfedea0SLionel Sambucelse 202*ebfedea0SLionel Sambuc 203*ebfedea0SLionel Sambuc# If we're going to rename the final executable, determine the name now. 204*ebfedea0SLionel Sambuc 205*ebfedea0SLionel Sambuc if [ x"$transformarg" = x ] 206*ebfedea0SLionel Sambuc then 207*ebfedea0SLionel Sambuc dstfile=`basename $dst` 208*ebfedea0SLionel Sambuc else 209*ebfedea0SLionel Sambuc dstfile=`basename $dst $transformbasename | 210*ebfedea0SLionel Sambuc sed $transformarg`$transformbasename 211*ebfedea0SLionel Sambuc fi 212*ebfedea0SLionel Sambuc 213*ebfedea0SLionel Sambuc# don't allow the sed command to completely eliminate the filename 214*ebfedea0SLionel Sambuc 215*ebfedea0SLionel Sambuc if [ x"$dstfile" = x ] 216*ebfedea0SLionel Sambuc then 217*ebfedea0SLionel Sambuc dstfile=`basename $dst` 218*ebfedea0SLionel Sambuc else 219*ebfedea0SLionel Sambuc true 220*ebfedea0SLionel Sambuc fi 221*ebfedea0SLionel Sambuc 222*ebfedea0SLionel Sambuc# Make a temp file name in the proper directory. 223*ebfedea0SLionel Sambuc 224*ebfedea0SLionel Sambuc dsttmp=$dstdir/#inst.$$# 225*ebfedea0SLionel Sambuc 226*ebfedea0SLionel Sambuc# Move or copy the file name to the temp name 227*ebfedea0SLionel Sambuc 228*ebfedea0SLionel Sambuc $doit $instcmd $src $dsttmp && 229*ebfedea0SLionel Sambuc 230*ebfedea0SLionel Sambuc trap "rm -f ${dsttmp}" 0 && 231*ebfedea0SLionel Sambuc 232*ebfedea0SLionel Sambuc# and set any options; do chmod last to preserve setuid bits 233*ebfedea0SLionel Sambuc 234*ebfedea0SLionel Sambuc# If any of these fail, we abort the whole thing. If we want to 235*ebfedea0SLionel Sambuc# ignore errors from any of these, just make sure not to ignore 236*ebfedea0SLionel Sambuc# errors from the above "$doit $instcmd $src $dsttmp" command. 237*ebfedea0SLionel Sambuc 238*ebfedea0SLionel Sambuc if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi && 239*ebfedea0SLionel Sambuc if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi && 240*ebfedea0SLionel Sambuc if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi && 241*ebfedea0SLionel Sambuc if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi && 242*ebfedea0SLionel Sambuc 243*ebfedea0SLionel Sambuc# Now rename the file to the real destination. 244*ebfedea0SLionel Sambuc 245*ebfedea0SLionel Sambuc $doit $rmcmd -f $dstdir/$dstfile && 246*ebfedea0SLionel Sambuc $doit $mvcmd $dsttmp $dstdir/$dstfile 247*ebfedea0SLionel Sambuc 248*ebfedea0SLionel Sambucfi && 249*ebfedea0SLionel Sambuc 250*ebfedea0SLionel Sambuc 251*ebfedea0SLionel Sambucexit 0 252