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