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