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