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