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