Home | History | Annotate | Line # | Download | only in dist
install-sh revision 1.1.1.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  christos 	
    119  1.1  christos 	if [ -d $dst ]; then
    120  1.1  christos 		instcmd=:
    121  1.1  christos 		chmodcmd=""
    122  1.1  christos 	else
    123  1.1  christos 		instcmd=mkdir
    124  1.1  christos 	fi
    125  1.1  christos else
    126  1.1  christos 
    127  1.1  christos # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
    128  1.1  christos # might cause directories to be created, which would be especially bad 
    129  1.1  christos # if $src (and thus $dsttmp) contains '*'.
    130  1.1  christos 
    131  1.1  christos 	if [ -f $src -o -d $src ]
    132  1.1  christos 	then
    133  1.1  christos 		true
    134  1.1  christos 	else
    135  1.1  christos 		echo "install:  $src does not exist"
    136  1.1  christos 		exit 1
    137  1.1  christos 	fi
    138  1.1  christos 	
    139  1.1  christos 	if [ x"$dst" = x ]
    140  1.1  christos 	then
    141  1.1  christos 		echo "install:	no destination specified"
    142  1.1  christos 		exit 1
    143  1.1  christos 	else
    144  1.1  christos 		true
    145  1.1  christos 	fi
    146  1.1  christos 
    147  1.1  christos # If destination is a directory, append the input filename; if your system
    148  1.1  christos # does not like double slashes in filenames, you may need to add some logic
    149  1.1  christos 
    150  1.1  christos 	if [ -d $dst ]
    151  1.1  christos 	then
    152  1.1  christos 		dst="$dst"/`basename $src`
    153  1.1  christos 	else
    154  1.1  christos 		true
    155  1.1  christos 	fi
    156  1.1  christos fi
    157  1.1  christos 
    158  1.1  christos ## this sed command emulates the dirname command
    159  1.1  christos dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
    160  1.1  christos 
    161  1.1  christos # Make sure that the destination directory exists.
    162  1.1  christos #  this part is taken from Noah Friedman's mkinstalldirs script
    163  1.1  christos 
    164  1.1  christos # Skip lots of stat calls in the usual case.
    165  1.1  christos if [ ! -d "$dstdir" ]; then
    166  1.1  christos defaultIFS='	
    167  1.1  christos '
    168  1.1  christos IFS="${IFS-${defaultIFS}}"
    169  1.1  christos 
    170  1.1  christos oIFS="${IFS}"
    171  1.1  christos # Some sh's can't handle IFS=/ for some reason.
    172  1.1  christos IFS='%'
    173  1.1  christos set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
    174  1.1  christos IFS="${oIFS}"
    175  1.1  christos 
    176  1.1  christos pathcomp=''
    177  1.1  christos 
    178  1.1  christos while [ $# -ne 0 ] ; do
    179  1.1  christos 	pathcomp="${pathcomp}${1}"
    180  1.1  christos 	shift
    181  1.1  christos 
    182  1.1  christos 	if [ ! -d "${pathcomp}" ] ;
    183  1.1  christos         then
    184  1.1  christos 		$mkdirprog "${pathcomp}"
    185  1.1  christos 	else
    186  1.1  christos 		true
    187  1.1  christos 	fi
    188  1.1  christos 
    189  1.1  christos 	pathcomp="${pathcomp}/"
    190  1.1  christos done
    191  1.1  christos fi
    192  1.1  christos 
    193  1.1  christos if [ x"$dir_arg" != x ]
    194  1.1  christos then
    195  1.1  christos 	$doit $instcmd $dst &&
    196  1.1  christos 
    197  1.1  christos 	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
    198  1.1  christos 	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
    199  1.1  christos 	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
    200  1.1  christos 	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
    201  1.1  christos else
    202  1.1  christos 
    203  1.1  christos # If we're going to rename the final executable, determine the name now.
    204  1.1  christos 
    205  1.1  christos 	if [ x"$transformarg" = x ] 
    206  1.1  christos 	then
    207  1.1  christos 		dstfile=`basename $dst`
    208  1.1  christos 	else
    209  1.1  christos 		dstfile=`basename $dst $transformbasename | 
    210  1.1  christos 			sed $transformarg`$transformbasename
    211  1.1  christos 	fi
    212  1.1  christos 
    213  1.1  christos # don't allow the sed command to completely eliminate the filename
    214  1.1  christos 
    215  1.1  christos 	if [ x"$dstfile" = x ] 
    216  1.1  christos 	then
    217  1.1  christos 		dstfile=`basename $dst`
    218  1.1  christos 	else
    219  1.1  christos 		true
    220  1.1  christos 	fi
    221  1.1  christos 
    222  1.1  christos # Make a temp file name in the proper directory.
    223  1.1  christos 
    224  1.1  christos 	dsttmp=$dstdir/#inst.$$#
    225  1.1  christos 
    226  1.1  christos # Move or copy the file name to the temp name
    227  1.1  christos 
    228  1.1  christos 	$doit $instcmd $src $dsttmp &&
    229  1.1  christos 
    230  1.1  christos 	trap "rm -f ${dsttmp}" 0 &&
    231  1.1  christos 
    232  1.1  christos # and set any options; do chmod last to preserve setuid bits
    233  1.1  christos 
    234  1.1  christos # If any of these fail, we abort the whole thing.  If we want to
    235  1.1  christos # ignore errors from any of these, just make sure not to ignore
    236  1.1  christos # errors from the above "$doit $instcmd $src $dsttmp" command.
    237  1.1  christos 
    238  1.1  christos 	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
    239  1.1  christos 	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
    240  1.1  christos 	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
    241  1.1  christos 	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
    242  1.1  christos 
    243  1.1  christos # Now rename the file to the real destination.
    244  1.1  christos 
    245  1.1  christos 	$doit $rmcmd -f $dstdir/$dstfile &&
    246  1.1  christos 	$doit $mvcmd $dsttmp $dstdir/$dstfile 
    247  1.1  christos 
    248  1.1  christos fi &&
    249  1.1  christos 
    250  1.1  christos 
    251  1.1  christos exit 0
    252