1335640Shselasky#! /bin/sh
2335640Shselasky#
3335640Shselasky# install - install a program, script, or datafile
4335640Shselasky# This comes from X11R5 (mit/util/scripts/install.sh).
5335640Shselasky#
6335640Shselasky# Copyright 1991 by the Massachusetts Institute of Technology
7335640Shselasky#
8335640Shselasky# Permission to use, copy, modify, distribute, and sell this software and its
9335640Shselasky# documentation for any purpose is hereby granted without fee, provided that
10335640Shselasky# the above copyright notice appear in all copies and that both that
11335640Shselasky# copyright notice and this permission notice appear in supporting
12335640Shselasky# documentation, and that the name of M.I.T. not be used in advertising or
13335640Shselasky# publicity pertaining to distribution of the software without specific,
14335640Shselasky# written prior permission.  M.I.T. makes no representations about the
15335640Shselasky# suitability of this software for any purpose.  It is provided "as is"
16335640Shselasky# without express or implied warranty.
17335640Shselasky#
18335640Shselasky# Calling this script install-sh is preferred over install.sh, to prevent
19335640Shselasky# `make' implicit rules from creating a file called install from it
20335640Shselasky# when there is no Makefile.
21335640Shselasky#
22335640Shselasky# This script is compatible with the BSD install script, but was written
23335640Shselasky# from scratch.  It can only install one file at a time, a restriction
24335640Shselasky# shared with many OS's install programs.
25335640Shselasky
26335640Shselasky
27335640Shselasky# set DOITPROG to echo to test this script
28335640Shselasky
29335640Shselasky# Don't use :- since 4.3BSD and earlier shells don't like it.
30335640Shselaskydoit="${DOITPROG-}"
31335640Shselasky
32335640Shselasky
33335640Shselasky# put in absolute paths if you don't have them in your path; or use env. vars.
34335640Shselasky
35335640Shselaskymvprog="${MVPROG-mv}"
36335640Shselaskycpprog="${CPPROG-cp}"
37335640Shselaskychmodprog="${CHMODPROG-chmod}"
38335640Shselaskychownprog="${CHOWNPROG-chown}"
39335640Shselaskychgrpprog="${CHGRPPROG-chgrp}"
40335640Shselaskystripprog="${STRIPPROG-strip}"
41335640Shselaskyrmprog="${RMPROG-rm}"
42335640Shselaskymkdirprog="${MKDIRPROG-mkdir}"
43335640Shselasky
44335640Shselaskytransformbasename=""
45335640Shselaskytransform_arg=""
46335640Shselaskyinstcmd="$mvprog"
47335640Shselaskychmodcmd="$chmodprog 0755"
48335640Shselaskychowncmd=""
49335640Shselaskychgrpcmd=""
50335640Shselaskystripcmd=""
51335640Shselaskyrmcmd="$rmprog -f"
52335640Shselaskymvcmd="$mvprog"
53335640Shselaskysrc=""
54335640Shselaskydst=""
55335640Shselaskydir_arg=""
56335640Shselasky
57335640Shselaskywhile [ x"$1" != x ]; do
58335640Shselasky    case $1 in
59335640Shselasky	-c) instcmd="$cpprog"
60335640Shselasky	    shift
61335640Shselasky	    continue;;
62335640Shselasky
63335640Shselasky	-d) dir_arg=true
64335640Shselasky	    shift
65335640Shselasky	    continue;;
66335640Shselasky
67335640Shselasky	-m) chmodcmd="$chmodprog $2"
68335640Shselasky	    shift
69335640Shselasky	    shift
70335640Shselasky	    continue;;
71335640Shselasky
72335640Shselasky	-o) chowncmd="$chownprog $2"
73335640Shselasky	    shift
74335640Shselasky	    shift
75335640Shselasky	    continue;;
76335640Shselasky
77335640Shselasky	-g) chgrpcmd="$chgrpprog $2"
78335640Shselasky	    shift
79335640Shselasky	    shift
80335640Shselasky	    continue;;
81335640Shselasky
82335640Shselasky	-s) stripcmd="$stripprog"
83335640Shselasky	    shift
84335640Shselasky	    continue;;
85335640Shselasky
86335640Shselasky	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
87335640Shselasky	    shift
88335640Shselasky	    continue;;
89335640Shselasky
90335640Shselasky	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
91335640Shselasky	    shift
92335640Shselasky	    continue;;
93335640Shselasky
94335640Shselasky	*)  if [ x"$src" = x ]
95335640Shselasky	    then
96335640Shselasky		src=$1
97335640Shselasky	    else
98335640Shselasky		# this colon is to work around a 386BSD /bin/sh bug
99335640Shselasky		:
100335640Shselasky		dst=$1
101335640Shselasky	    fi
102335640Shselasky	    shift
103335640Shselasky	    continue;;
104335640Shselasky    esac
105335640Shselaskydone
106335640Shselasky
107335640Shselaskyif [ x"$src" = x ]
108335640Shselaskythen
109335640Shselasky	echo "install:	no input file specified"
110335640Shselasky	exit 1
111335640Shselaskyelse
112335640Shselasky	true
113335640Shselaskyfi
114335640Shselasky
115335640Shselaskyif [ x"$dir_arg" != x ]; then
116335640Shselasky	dst=$src
117335640Shselasky	src=""
118335640Shselasky
119335640Shselasky	if [ -d $dst ]; then
120335640Shselasky		instcmd=:
121335640Shselasky	else
122335640Shselasky		instcmd=mkdir
123335640Shselasky	fi
124335640Shselaskyelse
125335640Shselasky
126335640Shselasky# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
127335640Shselasky# might cause directories to be created, which would be especially bad
128335640Shselasky# if $src (and thus $dsttmp) contains '*'.
129335640Shselasky
130335640Shselasky	if [ -f $src -o -d $src ]
131335640Shselasky	then
132335640Shselasky		true
133335640Shselasky	else
134335640Shselasky		echo "install:  $src does not exist"
135335640Shselasky		exit 1
136335640Shselasky	fi
137335640Shselasky
138335640Shselasky	if [ x"$dst" = x ]
139335640Shselasky	then
140335640Shselasky		echo "install:	no destination specified"
141335640Shselasky		exit 1
142335640Shselasky	else
143335640Shselasky		true
144335640Shselasky	fi
145335640Shselasky
146335640Shselasky# If destination is a directory, append the input filename; if your system
147335640Shselasky# does not like double slashes in filenames, you may need to add some logic
148335640Shselasky
149335640Shselasky	if [ -d $dst ]
150335640Shselasky	then
151335640Shselasky		dst="$dst"/`basename $src`
152335640Shselasky	else
153335640Shselasky		true
154335640Shselasky	fi
155335640Shselaskyfi
156335640Shselasky
157335640Shselasky## this sed command emulates the dirname command
158335640Shselaskydstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
159335640Shselasky
160335640Shselasky# Make sure that the destination directory exists.
161335640Shselasky#  this part is taken from Noah Friedman's mkinstalldirs script
162335640Shselasky
163335640Shselasky# Skip lots of stat calls in the usual case.
164335640Shselaskyif [ ! -d "$dstdir" ]; then
165335640ShselaskydefaultIFS='	
166335640Shselasky'
167335640ShselaskyIFS="${IFS-${defaultIFS}}"
168335640Shselasky
169335640ShselaskyoIFS="${IFS}"
170335640Shselasky# Some sh's can't handle IFS=/ for some reason.
171335640ShselaskyIFS='%'
172335640Shselaskyset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
173335640ShselaskyIFS="${oIFS}"
174335640Shselasky
175335640Shselaskypathcomp=''
176335640Shselasky
177335640Shselaskywhile [ $# -ne 0 ] ; do
178335640Shselasky	pathcomp="${pathcomp}${1}"
179335640Shselasky	shift
180335640Shselasky
181335640Shselasky	if [ ! -d "${pathcomp}" ] ;
182335640Shselasky        then
183335640Shselasky		$mkdirprog "${pathcomp}"
184335640Shselasky	else
185335640Shselasky		true
186335640Shselasky	fi
187335640Shselasky
188335640Shselasky	pathcomp="${pathcomp}/"
189335640Shselaskydone
190335640Shselaskyfi
191335640Shselasky
192335640Shselaskyif [ x"$dir_arg" != x ]
193335640Shselaskythen
194335640Shselasky	$doit $instcmd $dst &&
195335640Shselasky
196335640Shselasky	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
197335640Shselasky	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
198335640Shselasky	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
199335640Shselasky	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
200335640Shselaskyelse
201335640Shselasky
202335640Shselasky# If we're going to rename the final executable, determine the name now.
203335640Shselasky
204335640Shselasky	if [ x"$transformarg" = x ]
205335640Shselasky	then
206335640Shselasky		dstfile=`basename $dst`
207335640Shselasky	else
208335640Shselasky		dstfile=`basename $dst $transformbasename |
209335640Shselasky			sed $transformarg`$transformbasename
210335640Shselasky	fi
211335640Shselasky
212335640Shselasky# don't allow the sed command to completely eliminate the filename
213335640Shselasky
214335640Shselasky	if [ x"$dstfile" = x ]
215335640Shselasky	then
216335640Shselasky		dstfile=`basename $dst`
217335640Shselasky	else
218335640Shselasky		true
219335640Shselasky	fi
220335640Shselasky
221335640Shselasky# Make a temp file name in the proper directory.
222335640Shselasky
223335640Shselasky	dsttmp=$dstdir/#inst.$$#
224335640Shselasky
225335640Shselasky# Move or copy the file name to the temp name
226335640Shselasky
227335640Shselasky	$doit $instcmd $src $dsttmp &&
228335640Shselasky
229335640Shselasky	trap "rm -f ${dsttmp}" 0 &&
230335640Shselasky
231335640Shselasky# and set any options; do chmod last to preserve setuid bits
232335640Shselasky
233335640Shselasky# If any of these fail, we abort the whole thing.  If we want to
234335640Shselasky# ignore errors from any of these, just make sure not to ignore
235335640Shselasky# errors from the above "$doit $instcmd $src $dsttmp" command.
236335640Shselasky
237335640Shselasky	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
238335640Shselasky	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
239335640Shselasky	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
240335640Shselasky	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
241335640Shselasky
242335640Shselasky# Now rename the file to the real destination.
243335640Shselasky
244335640Shselasky	$doit $rmcmd -f $dstdir/$dstfile &&
245335640Shselasky	$doit $mvcmd $dsttmp $dstdir/$dstfile
246335640Shselasky
247335640Shselaskyfi &&
248335640Shselasky
249335640Shselasky
250335640Shselaskyexit 0
251