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