1#! /bin/sh
2#
3# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4# 
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of version 2 of the GNU General Public License as
7# published by the Free Software Foundation.
8# 
9# This program is distributed in the hope that it would be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12# 
13# Further, this software is distributed without any warranty that it is
14# free of the rightful claim of any third person regarding infringement
15# or the like.  Any license provided herein, whether implied or
16# otherwise, applies only to this software file.  Patent licenses, if
17# any, provided herein do not apply to combinations of this program with
18# other software, or any other product whatsoever.
19# 
20# You should have received a copy of the GNU General Public License along
21# with this program; if not, write the Free Software Foundation, Inc., 59
22# Temple Place - Suite 330, Boston MA 02111-1307, USA.
23# 
24# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25# Mountain View, CA  94043, or:
26# 
27# http://www.sgi.com 
28# 
29# For further information regarding this notice, see: 
30# 
31# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32#
33
34#
35# This script emulates bsd install and also recognises
36# two environment variables, with the following semantics :-
37#
38# $DIST_MANIFEST - if set, the name of the file to append manifest
39#                  information in the following format:
40#                  File     :  f mode owner group src target
41#                  Directory:  d mode owner group target
42#                  Symlink  :  l linkval target
43#
44# $DIST_ROOT     - if set, prepend to target
45#
46# The sematics of all combinations of these two variables
47# are as follows:
48#
49# $DIST_MANIFEST?  $DIST_ROOT? |   Copy?  Append Manifest?
50# -----------------------------+--------------------------
51#       not set       not set  |    yes        no
52#       not set       set      |    yes        no
53#       set           not set  |    no         yes
54#       set           set      |    yes        yes
55# 
56_usage() {
57    echo "Usage: $prog [-o owner] [-g group] [-m mode] -d directory"
58    echo "or     $prog [-D] [-o owner] [-g group] [-m mode] file directory/file"
59    echo "or     $prog [-o owner] [-g group] [-m mode] file [file ...] directory"
60    echo "or     $prog -S file target  (creates \"target\" symlink)"
61    echo "or     $prog -T lt_arg [-o owner] [-g group] [-m mode] libtool.lai directory"
62    echo ""
63    echo "The \$DIST_MANIFEST and \$DIST_ROOT environment variables affect the"
64    echo "behaviour of this command - see comments in the script."
65    echo "The -D flag is only available for the second usage, and causes"
66    echo "the target directory to be created before installing the file."
67    echo ""
68    exit 1
69}
70
71_chown ()
72{
73    _st=255
74    if [ $# -eq 3 ] ; then
75	chown $1:$2 $3
76	_st=$?
77	if [ $_st -ne 0 ] ; then
78	    if [ $REAL_UID != '0' ] ; then
79		if [ ! -f $DIST_ROOT/.chown.quiet ] ; then
80		    echo '==============================================='
81		    echo Ownership of files under ${DIST_ROOT:-/}
82		    echo cannot be changed
83		    echo '==============================================='
84		    if [ -n "$DIST_ROOT" ] ; then
85			touch $DIST_ROOT/.chown.quiet
86		    fi
87		fi
88	       _st=0
89	    fi     
90	fi
91    fi
92
93    return $_st
94}
95
96
97_manifest ()
98{ 
99    echo $* | sed -e 's/\/\//\//g' >>${DIST_MANIFEST:-/dev/null}
100}
101
102prog=`basename $0`
103HERE=`pwd`
104dflag=false
105Dflag=false
106Sflag=false
107Tflag=false
108DIRMODE=755
109FILEMODE=644
110OWNER=`id -u`
111GROUP=`id -g`
112REAL_UID=$OWNER
113
114# default is to install and don't append manifest
115INSTALL=true
116MANIFEST=:
117
118[ -n "$DIST_MANIFEST" -a -z "$DIST_ROOT" ] && INSTALL=false
119[ -n "$DIST_MANIFEST" ] && MANIFEST="_manifest"
120
121[ $# -eq 0 ] && _usage
122
123if $INSTALL
124then
125    CP=cp; LN=ln; MKDIR=mkdir; CHMOD=chmod; CHOWN=_chown
126else
127    CP=true; LN=true; MKDIR=true; CHMOD=true; CHOWN=true
128fi
129
130[ -n "$DIST_ROOT" -a $REAL_UID -ne 0 ] && CHOWN=true
131
132while getopts "Dcm:d:S:o:g:T:" c $*
133do
134   case $c in
135   c)
136	;;
137   g)
138	GROUP=$OPTARG
139	;;
140   o)
141	OWNER=$OPTARG
142	;;
143   m)
144	DIRMODE=`expr $OPTARG`
145	FILEMODE=$DIRMODE
146	;;
147   D) 
148	Dflag=true
149	;;
150   S) 
151	symlink=$OPTARG
152	Sflag=true
153	;;
154   d) 
155	dir=$DIST_ROOT/$OPTARG
156	dflag=true
157	;;
158   T)
159	lt_install=$OPTARG
160	Tflag=true
161	;;
162   *)
163   	_usage
164	;;
165   esac
166done
167
168shift `expr $OPTIND - 1`
169
170status=0
171if $dflag
172then
173    #
174    # first usage
175    #
176    $MKDIR -p $dir 
177    status=$?
178    if [ $status -eq 0 ]
179    then
180	$CHMOD $DIRMODE $dir
181	status=$?
182    fi
183    if [ $status -eq 0 ]
184    then
185	$CHOWN $OWNER $GROUP $dir
186	status=$?
187    fi
188    $MANIFEST d $DIRMODE $OWNER $GROUP ${dir#$DIST_ROOT}
189elif $Sflag
190then
191    #
192    # fourth usage (symlink)
193    #
194    if [ $# -ne 1 ]
195    then
196    	_usage
197    else
198    	target=$DIST_ROOT/$1
199    fi
200    $LN -s -f $symlink $target
201    status=$?
202    $MANIFEST l $symlink ${target#$DIST_ROOT} 
203elif $Tflag
204then
205    #
206    # -T (install libs built by libtool)
207    #
208    if [ $# -ne 2 ]
209    then
210	_usage
211    else
212	libtool_lai=$1
213	# source the libtool variables
214	if [ ! -f $libtool_lai ]
215	then
216		echo "$prog: Unable to find libtool library file $libtool_lai"
217		exit 2
218	fi
219	. ./$libtool_lai
220	target=$DIST_ROOT/$2
221    fi
222    case $lt_install in
223    so_dot_version)
224	# Loop until we find libfoo.so.x.y.z, then break out.
225	for solib in $library_names
226	do
227		# does it have enough parts?  libfoo.so.x.y.z == 5
228		cnt=`echo "$solib" | sed -e 's/\./ /g' | wc -w`
229		if [ $cnt -eq 5 ]
230		then
231			install_name=$target/$solib
232			$CP $solib $install_name
233			status=$?
234			$MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$solib ${install_name#$DIST_ROOT}
235			break
236		fi
237	done
238	;;
239
240    so_*)
241	case $lt_install in
242	so_dot_current)
243		# ln -s libfoo.so.x.y.z to libfoo.so.x
244		from_parts=5  # libfoo.so.x.y.z
245		to_parts=3    # libfoo.so.x
246		;;
247	so_base)
248		# ln -s libfoo.so.x to libfoo.so
249		from_parts=3  # libfoo.so.x
250		to_parts=2    # libfoo.so
251		;;
252	*)
253		echo "$prog: -T $lt_install invalid"
254		exit 2
255		;;
256	esac
257
258	# Loop until we find the names, then break out.
259	for solib in $library_names
260	do
261		# does it have enough parts?
262		cnt=`echo "$solib" | sed -e 's/\./ /g' | wc -w`
263		if [ $cnt -eq $from_parts ]
264		then
265			from_name=$solib
266		elif [ $cnt -eq $to_parts ]
267		then
268			to_name=$solib
269		fi
270
271		if [ -n "$from_name" ] && [ -n "$to_name" ]
272		then
273			install_name=$target/$to_name
274			$LN -s -f $from_name $install_name
275			status=$?
276			$MANIFEST l $from_name ${install_name#$DIST_ROOT}
277			break
278		fi
279	done
280	;;
281    old_lib)
282	install_name=$target/$old_library
283	$CP $old_library $install_name
284	status=$?
285	$MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$old_library ${install_name#$DIST_ROOT}
286	;;
287    *)
288	echo "$prog: -T $lt_install invalid"
289	exit 2
290	;;
291    esac
292
293    case $lt_install in
294    old_lib|so_dot_version)
295	if [ $status -eq 0 ]
296	then
297		$CHMOD $FILEMODE $install_name
298		$CHOWN $OWNER $GROUP $install_name
299	fi
300	;;
301    esac
302
303else
304    list=""
305    dir=""
306    if [ $# -eq 2 ]
307    then
308	#
309	# second usage
310	#
311	f=$1
312	dir=$DIST_ROOT/$2
313	if $Dflag
314	then
315	    mkdir -p `dirname $dir`
316	fi
317	$CP $f $dir
318	status=$?
319	if [ $status -eq 0 ]
320	then 
321	    if [ -f $dir/$f ]
322	    then
323		$CHMOD $FILEMODE $dir/$f
324		status=$?
325		if [ $status -eq 0 ]
326		then
327		    $CHOWN $OWNER $GROUP $dir/$f
328		    status=$?
329		fi
330		$MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
331	    else
332		$CHMOD $FILEMODE $dir
333		status=$?
334		if [ $status -eq 0 ]
335		then
336		    $CHOWN $OWNER $GROUP $dir
337		    status=$?
338		fi
339		$MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$dir ${dir#$DIST_ROOT}
340	    fi
341	fi
342    else
343	#
344	# third usage
345	#
346	n=1
347	while [ $# -gt 0 ]
348	do
349	    if [ $# -gt 1 ]
350	    then
351		list="$list $1"
352	    else
353		dir=$DIST_ROOT/$1
354	    fi
355	    shift
356	done
357
358	# echo DIR=$dir list=\"$list\"
359	for f in $list
360	do
361	    $CP $f $dir
362	    status=$?
363	    if [ $status -eq 0 ]
364	    then
365		$CHMOD $FILEMODE $dir/$f
366		status=$?
367		if [ $status -eq 0 ]
368		then
369		    $CHOWN $OWNER $GROUP $dir/$f
370		    status=$?
371		fi
372		$MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
373	    fi
374	    [ $status -ne 0 ] && break
375	done
376    fi
377fi
378
379exit $status
380