xref: /openbsd-src/gnu/usr.bin/cvs/install-sh (revision 43c1707e6f6829177cb1974ee6615ce6c1307689)
11e72d8d2Sderaadt#!/bin/sh
21e72d8d2Sderaadt#
31e72d8d2Sderaadt# install - install a program, script, or datafile
4*43c1707eStholo# This comes from X11R5 (mit/util/scripts/install.sh).
5*43c1707eStholo#
6*43c1707eStholo# Copyright 1991 by the Massachusetts Institute of Technology
7*43c1707eStholo#
8*43c1707eStholo# Permission to use, copy, modify, distribute, and sell this software and its
9*43c1707eStholo# documentation for any purpose is hereby granted without fee, provided that
10*43c1707eStholo# the above copyright notice appear in all copies and that both that
11*43c1707eStholo# copyright notice and this permission notice appear in supporting
12*43c1707eStholo# documentation, and that the name of M.I.T. not be used in advertising or
13*43c1707eStholo# publicity pertaining to distribution of the software without specific,
14*43c1707eStholo# written prior permission.  M.I.T. makes no representations about the
15*43c1707eStholo# suitability of this software for any purpose.  It is provided "as is"
16*43c1707eStholo# without express or implied warranty.
171e72d8d2Sderaadt#
181e72d8d2Sderaadt# Calling this script install-sh is preferred over install.sh, to prevent
191e72d8d2Sderaadt# `make' implicit rules from creating a file called install from it
201e72d8d2Sderaadt# when there is no Makefile.
211e72d8d2Sderaadt#
221e72d8d2Sderaadt# This script is compatible with the BSD install script, but was written
23*43c1707eStholo# from scratch.  It can only install one file at a time, a restriction
24*43c1707eStholo# shared with many OS's install programs.
251e72d8d2Sderaadt
261e72d8d2Sderaadt
271e72d8d2Sderaadt# set DOITPROG to echo to test this script
281e72d8d2Sderaadt
291e72d8d2Sderaadt# Don't use :- since 4.3BSD and earlier shells don't like it.
301e72d8d2Sderaadtdoit="${DOITPROG-}"
311e72d8d2Sderaadt
321e72d8d2Sderaadt
331e72d8d2Sderaadt# put in absolute paths if you don't have them in your path; or use env. vars.
341e72d8d2Sderaadt
351e72d8d2Sderaadtmvprog="${MVPROG-mv}"
361e72d8d2Sderaadtcpprog="${CPPROG-cp}"
371e72d8d2Sderaadtchmodprog="${CHMODPROG-chmod}"
381e72d8d2Sderaadtchownprog="${CHOWNPROG-chown}"
391e72d8d2Sderaadtchgrpprog="${CHGRPPROG-chgrp}"
401e72d8d2Sderaadtstripprog="${STRIPPROG-strip}"
411e72d8d2Sderaadtrmprog="${RMPROG-rm}"
421e72d8d2Sderaadtmkdirprog="${MKDIRPROG-mkdir}"
431e72d8d2Sderaadt
44*43c1707eStholotransformbasename=""
451e72d8d2Sderaadttransform_arg=""
461e72d8d2Sderaadtinstcmd="$mvprog"
471e72d8d2Sderaadtchmodcmd="$chmodprog 0755"
481e72d8d2Sderaadtchowncmd=""
491e72d8d2Sderaadtchgrpcmd=""
501e72d8d2Sderaadtstripcmd=""
511e72d8d2Sderaadtrmcmd="$rmprog -f"
521e72d8d2Sderaadtmvcmd="$mvprog"
531e72d8d2Sderaadtsrc=""
541e72d8d2Sderaadtdst=""
551e72d8d2Sderaadtdir_arg=""
561e72d8d2Sderaadt
571e72d8d2Sderaadtwhile [ x"$1" != x ]; do
581e72d8d2Sderaadt    case $1 in
591e72d8d2Sderaadt	-c) instcmd="$cpprog"
601e72d8d2Sderaadt	    shift
611e72d8d2Sderaadt	    continue;;
621e72d8d2Sderaadt
631e72d8d2Sderaadt	-d) dir_arg=true
641e72d8d2Sderaadt	    shift
651e72d8d2Sderaadt	    continue;;
661e72d8d2Sderaadt
671e72d8d2Sderaadt	-m) chmodcmd="$chmodprog $2"
681e72d8d2Sderaadt	    shift
691e72d8d2Sderaadt	    shift
701e72d8d2Sderaadt	    continue;;
711e72d8d2Sderaadt
721e72d8d2Sderaadt	-o) chowncmd="$chownprog $2"
731e72d8d2Sderaadt	    shift
741e72d8d2Sderaadt	    shift
751e72d8d2Sderaadt	    continue;;
761e72d8d2Sderaadt
771e72d8d2Sderaadt	-g) chgrpcmd="$chgrpprog $2"
781e72d8d2Sderaadt	    shift
791e72d8d2Sderaadt	    shift
801e72d8d2Sderaadt	    continue;;
811e72d8d2Sderaadt
821e72d8d2Sderaadt	-s) stripcmd="$stripprog"
831e72d8d2Sderaadt	    shift
841e72d8d2Sderaadt	    continue;;
851e72d8d2Sderaadt
861e72d8d2Sderaadt	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
871e72d8d2Sderaadt	    shift
881e72d8d2Sderaadt	    continue;;
891e72d8d2Sderaadt
901e72d8d2Sderaadt	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
911e72d8d2Sderaadt	    shift
921e72d8d2Sderaadt	    continue;;
931e72d8d2Sderaadt
941e72d8d2Sderaadt	*)  if [ x"$src" = x ]
951e72d8d2Sderaadt	    then
961e72d8d2Sderaadt		src=$1
971e72d8d2Sderaadt	    else
981e72d8d2Sderaadt		# this colon is to work around a 386BSD /bin/sh bug
991e72d8d2Sderaadt		:
1001e72d8d2Sderaadt		dst=$1
1011e72d8d2Sderaadt	    fi
1021e72d8d2Sderaadt	    shift
1031e72d8d2Sderaadt	    continue;;
1041e72d8d2Sderaadt    esac
1051e72d8d2Sderaadtdone
1061e72d8d2Sderaadt
1071e72d8d2Sderaadtif [ x"$src" = x ]
1081e72d8d2Sderaadtthen
1091e72d8d2Sderaadt	echo "install:	no input file specified"
1101e72d8d2Sderaadt	exit 1
1111e72d8d2Sderaadtelse
1121e72d8d2Sderaadt	true
1131e72d8d2Sderaadtfi
1141e72d8d2Sderaadt
1151e72d8d2Sderaadtif [ x"$dir_arg" != x ]; then
1161e72d8d2Sderaadt	dst=$src
1171e72d8d2Sderaadt	src=""
1181e72d8d2Sderaadt
1191e72d8d2Sderaadt	if [ -d $dst ]; then
1201e72d8d2Sderaadt		instcmd=:
121*43c1707eStholo		chmodcmd=""
1221e72d8d2Sderaadt	else
1231e72d8d2Sderaadt		instcmd=mkdir
1241e72d8d2Sderaadt	fi
1251e72d8d2Sderaadtelse
1261e72d8d2Sderaadt
1271e72d8d2Sderaadt# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
1281e72d8d2Sderaadt# might cause directories to be created, which would be especially bad
1291e72d8d2Sderaadt# if $src (and thus $dsttmp) contains '*'.
1301e72d8d2Sderaadt
1311e72d8d2Sderaadt	if [ -f $src -o -d $src ]
1321e72d8d2Sderaadt	then
1331e72d8d2Sderaadt		true
1341e72d8d2Sderaadt	else
1351e72d8d2Sderaadt		echo "install:  $src does not exist"
1361e72d8d2Sderaadt		exit 1
1371e72d8d2Sderaadt	fi
1381e72d8d2Sderaadt
1391e72d8d2Sderaadt	if [ x"$dst" = x ]
1401e72d8d2Sderaadt	then
1411e72d8d2Sderaadt		echo "install:	no destination specified"
1421e72d8d2Sderaadt		exit 1
1431e72d8d2Sderaadt	else
1441e72d8d2Sderaadt		true
1451e72d8d2Sderaadt	fi
1461e72d8d2Sderaadt
1471e72d8d2Sderaadt# If destination is a directory, append the input filename; if your system
1481e72d8d2Sderaadt# does not like double slashes in filenames, you may need to add some logic
1491e72d8d2Sderaadt
1501e72d8d2Sderaadt	if [ -d $dst ]
1511e72d8d2Sderaadt	then
1521e72d8d2Sderaadt		dst="$dst"/`basename $src`
1531e72d8d2Sderaadt	else
1541e72d8d2Sderaadt		true
1551e72d8d2Sderaadt	fi
1561e72d8d2Sderaadtfi
1571e72d8d2Sderaadt
1581e72d8d2Sderaadt## this sed command emulates the dirname command
1591e72d8d2Sderaadtdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
1601e72d8d2Sderaadt
1611e72d8d2Sderaadt# Make sure that the destination directory exists.
1621e72d8d2Sderaadt#  this part is taken from Noah Friedman's mkinstalldirs script
1631e72d8d2Sderaadt
1641e72d8d2Sderaadt# Skip lots of stat calls in the usual case.
1651e72d8d2Sderaadtif [ ! -d "$dstdir" ]; then
1661e72d8d2SderaadtdefaultIFS='
1671e72d8d2Sderaadt'
1681e72d8d2SderaadtIFS="${IFS-${defaultIFS}}"
1691e72d8d2Sderaadt
1701e72d8d2SderaadtoIFS="${IFS}"
1711e72d8d2Sderaadt# Some sh's can't handle IFS=/ for some reason.
1721e72d8d2SderaadtIFS='%'
1731e72d8d2Sderaadtset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
1741e72d8d2SderaadtIFS="${oIFS}"
1751e72d8d2Sderaadt
1761e72d8d2Sderaadtpathcomp=''
1771e72d8d2Sderaadt
1781e72d8d2Sderaadtwhile [ $# -ne 0 ] ; do
1791e72d8d2Sderaadt	pathcomp="${pathcomp}${1}"
1801e72d8d2Sderaadt	shift
1811e72d8d2Sderaadt
1821e72d8d2Sderaadt	if [ ! -d "${pathcomp}" ] ;
1831e72d8d2Sderaadt        then
1841e72d8d2Sderaadt		$mkdirprog "${pathcomp}"
1851e72d8d2Sderaadt	else
1861e72d8d2Sderaadt		true
1871e72d8d2Sderaadt	fi
1881e72d8d2Sderaadt
1891e72d8d2Sderaadt	pathcomp="${pathcomp}/"
1901e72d8d2Sderaadtdone
1911e72d8d2Sderaadtfi
1921e72d8d2Sderaadt
1931e72d8d2Sderaadtif [ x"$dir_arg" != x ]
1941e72d8d2Sderaadtthen
1951e72d8d2Sderaadt	$doit $instcmd $dst &&
1961e72d8d2Sderaadt
1971e72d8d2Sderaadt	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
1981e72d8d2Sderaadt	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
1991e72d8d2Sderaadt	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
2001e72d8d2Sderaadt	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
2011e72d8d2Sderaadtelse
2021e72d8d2Sderaadt
2031e72d8d2Sderaadt# If we're going to rename the final executable, determine the name now.
2041e72d8d2Sderaadt
2051e72d8d2Sderaadt	if [ x"$transformarg" = x ]
2061e72d8d2Sderaadt	then
2071e72d8d2Sderaadt		dstfile=`basename $dst`
2081e72d8d2Sderaadt	else
2091e72d8d2Sderaadt		dstfile=`basename $dst $transformbasename |
2101e72d8d2Sderaadt			sed $transformarg`$transformbasename
2111e72d8d2Sderaadt	fi
2121e72d8d2Sderaadt
2131e72d8d2Sderaadt# don't allow the sed command to completely eliminate the filename
2141e72d8d2Sderaadt
2151e72d8d2Sderaadt	if [ x"$dstfile" = x ]
2161e72d8d2Sderaadt	then
2171e72d8d2Sderaadt		dstfile=`basename $dst`
2181e72d8d2Sderaadt	else
2191e72d8d2Sderaadt		true
2201e72d8d2Sderaadt	fi
2211e72d8d2Sderaadt
2221e72d8d2Sderaadt# Make a temp file name in the proper directory.
2231e72d8d2Sderaadt
2241e72d8d2Sderaadt	dsttmp=$dstdir/#inst.$$#
2251e72d8d2Sderaadt
2261e72d8d2Sderaadt# Move or copy the file name to the temp name
2271e72d8d2Sderaadt
2281e72d8d2Sderaadt	$doit $instcmd $src $dsttmp &&
2291e72d8d2Sderaadt
2301e72d8d2Sderaadt	trap "rm -f ${dsttmp}" 0 &&
2311e72d8d2Sderaadt
2321e72d8d2Sderaadt# and set any options; do chmod last to preserve setuid bits
2331e72d8d2Sderaadt
2341e72d8d2Sderaadt# If any of these fail, we abort the whole thing.  If we want to
2351e72d8d2Sderaadt# ignore errors from any of these, just make sure not to ignore
2361e72d8d2Sderaadt# errors from the above "$doit $instcmd $src $dsttmp" command.
2371e72d8d2Sderaadt
2381e72d8d2Sderaadt	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
2391e72d8d2Sderaadt	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
2401e72d8d2Sderaadt	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
2411e72d8d2Sderaadt	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
2421e72d8d2Sderaadt
2431e72d8d2Sderaadt# Now rename the file to the real destination.
2441e72d8d2Sderaadt
2451e72d8d2Sderaadt	$doit $rmcmd -f $dstdir/$dstfile &&
2461e72d8d2Sderaadt	$doit $mvcmd $dsttmp $dstdir/$dstfile
2471e72d8d2Sderaadt
2481e72d8d2Sderaadtfi &&
2491e72d8d2Sderaadt
2501e72d8d2Sderaadt
2511e72d8d2Sderaadtexit 0
252