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