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