install-sh revision 234449
16081Sphk#!/bin/sh
26081Sphk#
36081Sphk# $NetBSD: install-sh.in,v 1.5 2010/10/08 19:57:05 tez Exp $
46081Sphk# This script now also installs multiple files, but might choke on installing
56081Sphk# multiple files with spaces in the file names.
66081Sphk#
76081Sphk# install - install a program, script, or datafile
86081Sphk# This comes from X11R5 (mit/util/scripts/install.sh).
96081Sphk#
106081Sphk# Copyright 1991 by the Massachusetts Institute of Technology
116081Sphk#
126081Sphk# Permission to use, copy, modify, distribute, and sell this software and its
136081Sphk# documentation for any purpose is hereby granted without fee, provided that
146081Sphk# the above copyright notice appear in all copies and that both that
156081Sphk# copyright notice and this permission notice appear in supporting
166081Sphk# documentation, and that the name of M.I.T. not be used in advertising or
176081Sphk# publicity pertaining to distribution of the software without specific,
186081Sphk# written prior permission.  M.I.T. makes no representations about the
196081Sphk# suitability of this software for any purpose.  It is provided "as is"
206081Sphk# without express or implied warranty.
216081Sphk#
226081Sphk# Calling this script install-sh is preferred over install.sh, to prevent
236081Sphk# `make' implicit rules from creating a file called install from it
246081Sphk# when there is no Makefile.
256081Sphk#
266081Sphk# This script is compatible with the BSD install script, but was written
276081Sphk# from scratch.
286081Sphk
296081Sphk# set DOITPROG to echo to test this script
306081Sphk
316081Sphk# Don't use :- since 4.3BSD and earlier shells don't like it.
326081Sphkdoit="${DOITPROG-}"
336081Sphk
346081Sphk
356081Sphk# put in absolute paths if you don't have them in your path; or use env. vars.
366081Sphk
376081Sphkawkprog="${AWKPROG-awk}"
386081Sphkmvprog="${MVPROG-mv}"
396081Sphkcpprog="${CPPROG-cp}"
406081Sphkchmodprog="${CHMODPROG-chmod}"
416081Sphkchownprog="${CHOWNPROG-chown}"
426081Sphkchgrpprog="${CHGRPPROG-chgrp}"
436081Sphkstripprog="${STRIPPROG-strip}"
446081Sphkrmprog="${RMPROG-rm}"
456081Sphkmkdirprog="${MKDIRPROG-mkdir}"
466081Sphk
476081Sphkinstcmd="$cpprog"
486081Sphkpathcompchmodcmd="$chmodprog 755"
496290Sphkchmodcmd="$chmodprog 755"
506081Sphkchowncmd=""
516081Sphkchgrpcmd=""
526081Sphkstripcmd=""
536081Sphkstripflags=""
546081Sphkrmcmd="$rmprog -f"
556081Sphkmvcmd="$mvprog"
566081Sphksrc=""
576081Sphkmsrc=""
586081Sphkdst=""
596081Sphkdir_arg=""
606081Sphksuffix=""
616081Sphksuffixfmt=""
626081Sphk
636081Sphkwhile [ x"$1" != x ]; do
646081Sphk    case $1 in
656290Sphk	-b) suffix=".old"
666081Sphk	    shift
676081Sphk	    continue;;
686457Sphk
696290Sphk	-B) suffixfmt="$2"
706290Sphk	    shift
716081Sphk	    shift
726081Sphk	    continue;;
736081Sphk
746081Sphk	-c) instcmd="$cpprog"
756081Sphk	    shift
766081Sphk	    continue;;
776081Sphk
786081Sphk	-d) dir_arg=true
796081Sphk	    shift
806081Sphk	    continue;;
816081Sphk
826081Sphk	-m) chmodcmd="$chmodprog $2"
836081Sphk	    shift
846081Sphk	    shift
856081Sphk	    continue;;
866081Sphk
876081Sphk	-o) chowncmd="$chownprog $2"
886081Sphk	    shift
896081Sphk	    shift
906081Sphk	    continue;;
916081Sphk
926081Sphk	-g) chgrpcmd="$chgrpprog $2"
936081Sphk	    shift
946081Sphk	    shift
956081Sphk	    continue;;
966081Sphk
976081Sphk	-s) stripcmd="$stripprog"
986081Sphk	    shift
996081Sphk	    continue;;
1006081Sphk
1016081Sphk	-S) stripcmd="$stripprog"
1026081Sphk	    stripflags="-S $2 $stripflags"
1036081Sphk	    shift
1046081Sphk	    shift
1056081Sphk	    continue;;
1066081Sphk
1076081Sphk	*)  if [ x"$msrc" = x ]
1086081Sphk	    then
1096081Sphk		msrc="$dst"
1106081Sphk	    else
1116081Sphk		msrc="$msrc $dst"
1126081Sphk	    fi
1136081Sphk	    src="$dst"
1146081Sphk	    dst="$1"
1156081Sphk	    shift
1166081Sphk	    continue;;
1176081Sphk    esac
1186081Sphkdone
1196081Sphk
1206081Sphkif [ x"$dir_arg" = x ]
1216081Sphkthen
1226081Sphk	dstisfile=""
1236081Sphk	if [ ! -d "$dst" ]
1246081Sphk	then
1256081Sphk		if [ x"$msrc" = x"$src" ]
1266081Sphk		then
1276081Sphk			dstisfile=true
1286081Sphk		else
1296081Sphk			echo "install: destination is not a directory"
1306081Sphk			exit 1
1316081Sphk		fi
1326081Sphk	fi
1336081Sphkelse
1346081Sphk	msrc="$msrc $dst"
1356081Sphkfi
1366081Sphk
1376081Sphkif [ x"$msrc" = x ]
1386081Sphkthen
1396081Sphk	echo "install: no destination specified"
1406081Sphk	exit 1
1416081Sphkfi      
1426081Sphk
1436081Sphkfor srcarg in $msrc; do
1446081Sphk
1456081Sphkif [ x"$dir_arg" != x ]; then
1466081Sphk
1476081Sphk	dstarg="$srcarg"
1486081Sphkelse
1496081Sphk	dstarg="$dst"
1506081Sphk
1516081Sphk# Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
1526081Sphk# might cause directories to be created, which would be especially bad 
1536081Sphk# if $src (and thus $dsttmp) contains '*'.
1546081Sphk
1556081Sphk	if [ -f "$srcarg" ]
1566081Sphk	then
1576081Sphk		doinst="$instcmd"
1586081Sphk	elif [ -d "$srcarg" ]
1596081Sphk	then
1606081Sphk		echo "install: $srcarg: not a regular file"
1616081Sphk		exit 1
1626081Sphk	elif [ "$srcarg" = "/dev/null" ]
1636081Sphk	then
1646081Sphk		doinst="$cpprog"
1656081Sphk	else
1666081Sphk		echo "install:  $srcarg does not exist"
1676081Sphk		exit 1
1686081Sphk	fi
1696081Sphk	
1706081Sphk# If destination is a directory, append the input filename; if your system
1716081Sphk# does not like double slashes in filenames, you may need to add some logic
1726081Sphk
1736081Sphk	if [ -d "$dstarg" ]
1746081Sphk	then
1756081Sphk		dstarg="$dstarg"/`basename "$srcarg"`
1766081Sphk	fi
1776081Sphkfi
1786081Sphk
1796081Sphk## this sed command emulates the dirname command
1806081Sphkdstdir=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
1816081Sphk
1826081Sphk# Make sure that the destination directory exists.
1836081Sphk#  this part is taken from Noah Friedman's mkinstalldirs script
1846081Sphk
1856081Sphk# Skip lots of stat calls in the usual case.
1866081Sphkif [ ! -d "$dstdir" ]; then
1876081SphkdefaultIFS='	
1886081Sphk'
1896081SphkIFS="${IFS-${defaultIFS}}"
1906081Sphk
1916081SphkoIFS="${IFS}"
1926081Sphk# Some sh's can't handle IFS=/ for some reason.
1936081SphkIFS='%'
1946081Sphkset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
1956081SphkIFS="${oIFS}"
1966081Sphk
1976081Sphkpathcomp=''
1986081Sphk
1996081Sphkwhile [ $# -ne 0 ] ; do
2006081Sphk	pathcomp="${pathcomp}${1}"
2016081Sphk	shift
2026081Sphk
2036081Sphk	if [ ! -d "${pathcomp}" ] ;
2046290Sphk        then
2056081Sphk		$doit $mkdirprog "${pathcomp}"
2066081Sphk        	if [ x"$chowncmd" != x ]; then $doit $chowncmd "${pathcomp}"; else true ; fi &&
2076081Sphk        	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "${pathcomp}"; else true ; fi &&
2086081Sphk        	if [ x"$pathcompchmodcmd" != x ]; then $doit $pathcompchmodcmd "${pathcomp}"; else true ; fi
2096081Sphk
2106290Sphk	else
2116081Sphk		true
2126081Sphk	fi
2136081Sphk
2146081Sphk	pathcomp="${pathcomp}/"
2156081Sphkdone
2166081Sphkfi
2176081Sphk
2186081Sphk	if [ x"$dir_arg" != x ]
2196081Sphk	then
2206081Sphk		if [ -d "$dstarg" ]; then
2216081Sphk			true
2226081Sphk		else
2236081Sphk			$doit $mkdirprog "$dstarg" &&
2246081Sphk
2256081Sphk			if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dstarg"; else true ; fi &&
2266081Sphk			if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dstarg"; else true ; fi &&
2276081Sphk			if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dstarg"; else true ; fi
2286081Sphk		fi
2296081Sphk	else
2306081Sphk
2316081Sphk		if [ x"$dstisfile" = x ]
2326081Sphk		then
2336290Sphk			file=$srcarg
2346081Sphk		else
2356457Sphk			file=$dst
2366290Sphk		fi
2376290Sphk
2386081Sphk		dstfile=`basename "$file"`
2396290Sphk		dstfinal="$dstdir/$dstfile"
2406290Sphk
2416081Sphk# Make a temp file name in the proper directory.
2426290Sphk
2436290Sphk		dsttmp=$dstdir/#inst.$$#
2446290Sphk
2456290Sphk# Make a backup file name in the proper directory.
2466290Sphk		case x$suffixfmt in
2476290Sphk		*%*)	suffix=`echo x |
2486081Sphk			$awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
2496290Sphk			{ cnt = 0;
2506290Sphk			  do {
2516290Sphk				sfx = sprintf(fmt, cnt++);
2526081Sphk				name = bname sfx;
2536081Sphk			  } while (system("test -f " name) == 0);
2546081Sphk			  print sfx; }' -`;;
2556081Sphk		x)	;;
2566081Sphk		*)	suffix="$suffixfmt";;
2576081Sphk		esac
2586081Sphk		dstbackup="$dstfinal$suffix"
2596081Sphk
2606081Sphk# Move or copy the file name to the temp name
2616081Sphk
2626081Sphk		$doit $doinst $srcarg "$dsttmp" &&
2636081Sphk
2646081Sphk		trap "rm -f ${dsttmp}" 0 &&
2656081Sphk
2666081Sphk# and set any options; do chmod last to preserve setuid bits
2676081Sphk
2686081Sphk# If any of these fail, we abort the whole thing.  If we want to
2696081Sphk# ignore errors from any of these, just make sure not to ignore
2706081Sphk# errors from the above "$doit $instcmd $src $dsttmp" command.
2716081Sphk
2726081Sphk		if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else true;fi &&
2736081Sphk		if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else true;fi &&
2746081Sphk		if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripflags "$dsttmp"; else true;fi &&
2756081Sphk		if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else true;fi &&
2766081Sphk
2776081Sphk# Now rename the file to the real destination.
2786081Sphk
2796081Sphk		if [ x"$suffix" != x ] && [ -f "$dstfinal" ]
2806081Sphk		then
2816081Sphk			$doit $mvcmd "$dstfinal" "$dstbackup"
2826081Sphk		else
2836081Sphk			$doit $rmcmd -f "$dstfinal"
2846081Sphk		fi &&
2856081Sphk		$doit $mvcmd "$dsttmp" "$dstfinal"
2866081Sphk	fi
2876081Sphk
2886081Sphkdone &&
2896081Sphk
2906081Sphk
2916081Sphkexit 0
2926081Sphk