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