xref: /illumos-gate/usr/src/cmd/install.d/install.sh (revision 7c478bd9)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
24#	  All Rights Reserved
25
26
27#ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.12.1.1	*/
28
29#	INSTALL COMMAND
30
31FLIST=$ROOT/etc/syslist
32DEFAULT="$ROOT/bin $ROOT/usr/bin $ROOT/etc $ROOT/lib $ROOT/usr/lib" FOUND="" MOVOLD=""
33ECHO=echo PATH=/usr/bin FLAG=off
34USAGE="eval echo 'usage: install [options] file [dir1 ...]'; exit 2"
35
36if [ $# -lt 2 ] ; then
37	$USAGE
38fi
39
40MFLAG=off
41UFLAG=off
42GFLAG=off
43
44MODE=755
45GRP=`expr "\`id\`" : ".*gid=[0-9]*(\(..*\)) .*"`
46GROUP=`echo $GRP | sed -e 's/) fsid.*//p'`
47OWN=`expr "\`id\`" : ".*uid=[0-9]*(\(..*\)) .*"`
48OWNER=`echo $OWN | sed -e 's/) .*//p'`
49if [ -z "$GROUP" ]
50then
51	GROUP=`expr "\`id\`" : ".*gid=\([0-9]*\).*"`
52fi
53if [ -z "$OWNER" ]
54then
55	OWNER=`expr "\`id\`" : ".*uid=\([0-9]*\).*"`
56fi
57if [ "$OWNER" = root ]
58then
59	ROOTFLAG=on
60	OWNER=bin
61	GROUP=bin
62else
63	ROOTFLAG=off
64fi
65
66
67for i in $*
68do
69	if [ $FLAG = on ]
70	then
71		case $i in
72		    -*) echo "install: The -c, -f, -n options each require a directory following!"
73			exit 2;;
74		     *) FLAG=off
75			continue;;
76		esac
77	fi
78	case $i in
79	    -c) if [ x$ARG = x-d -o x$ARG = x-f ]
80		then
81			echo "install: -c dir: illegal option with $ARG option!"
82			exit 2
83	        elif [ x$arg = x-i -o x$arg = x-o -o x$arg = x-n ]
84		then
85			echo "install: -c dir: illegal option with $arg option!"
86			exit 2
87		elif test $# -lt 3
88		then
89			echo "install: -c option must have at least 3 args!"
90			exit 2
91		else
92			direct=$2
93			FLAG=on
94			ARG=-c
95			shift; shift
96		fi;;
97	    -f) if [ x$ARG = x-d -o x$ARG = x-c ]
98		then
99			echo "install: -f dir: illegal option with $ARG option!"
100			exit 2
101		elif [ x$arg = x-i -o x$arg = x-n ]
102		then
103			echo "install: -f dir: illegal option with $arg option!"
104			exit 2
105		elif test $# -lt 3
106		then
107			echo "install: -f option must have at least 3 args!"
108			exit 2
109		else
110			direct=$2
111			FLAG=on
112			ARG=-f
113			shift; shift
114		fi;;
115	  -i) if [ x$ARG = x-d -o x$ARG  = x-c -o x$ARG = x-f ]
116		then
117			echo "install: -i: illegal option with $ARG option!"
118			exit 2
119		elif test $# -lt 3
120		then
121			echo "install: -i option requires at least 3 args!"
122			exit 2
123		else
124			DEFAULT=""
125			arg=-i
126			shift
127		fi;;
128	    -o) if  [ x$ARG = x-d -o x$ARG = x-c ]
129		then
130			echo "install: -o: illegal option with $ARG option!"
131			exit 2
132		elif test $# -lt 2
133		then
134			$USAGE
135		else
136			MOVOLD=yes
137			arg=-o
138			shift
139		fi;;
140	    -n) if [ x$ARG = x-d -o x$ARG = x-c -o x$ARG = x-f ]
141		then
142			echo "install: -n dir: illegal option with $ARG option!"
143			exit 2
144		elif test $# -lt 3
145		then
146			echo "install: -n option requires at least 3 args!"
147			exit 2
148		else
149			LASTRES=$2
150			FLAG=on
151			FOUND=n
152			arg=-n
153			shift; shift
154		fi;;
155	    -d) if [ x$ARG = x-c -o x$ARG = x-f ]
156		then
157			echo "install: -d: illegal option with $ARG option!"
158			exit 2
159		elif [ x$arg = x-i -o x$arg = x-o -o x$arg = x-n ]
160		then
161			echo "install: -d: illegal option with $arg option!"
162			exit 2
163		else
164			ARG=-d
165			shift
166		fi;;
167	    -s) if test $# -lt 2
168		then
169			$USAGE
170		else
171			ECHO=:
172			shift
173		fi;;
174	    -u) if [ $ROOTFLAG = off ]
175		then
176			echo "install: -u option available only to root -- ignored"
177		else
178			OWNER=$2
179			UFLAG=on
180			$ECHO new owner is $OWNER
181		fi
182		FLAG=on
183		shift; shift;;
184	    -g) if [ $ROOTFLAG = off ]
185		then
186			echo "install: -g option available only to root -- ignored"
187		else
188			GROUP=$2
189			GFLAG=on
190		fi
191		FLAG=on
192		shift; shift;;
193	    -m) MODE=$2
194		MFLAG=on
195		FLAG=on
196		shift; shift;;
197	     *) break;;
198	esac
199done
200
201
202if [ x$ARG = x-d ]
203then
204	if [ ! -d $i ]
205	then
206		mkdir -p $i
207		if [ $? = 0 ]
208		then
209			$ECHO "directory $i created"
210			chgrp $GROUP $i
211			chown $OWNER $i
212			chmod $MODE $i
213		else
214			echo "install: mkdir $i failed "
215		fi
216	else
217		chgrp $GROUP $i
218		chown $OWNER $i
219		chmod $MODE $i
220	fi
221	exit
222fi
223
224FILEP=$i FILE=`echo $i | sed -e "s/.*\///"`
225if [ x$ARG = x-c -o x$ARG = x-f ]
226then
227	case $2 in
228		-*) $USAGE ;;
229		"") :	;;
230	esac
231	if test -f $direct/$FILE -o -f $direct/$FILE/$FILE
232	then
233		case $ARG in
234			-c) echo "install: $FILE already exists in $direct"
235			    exit 2;;
236			-f) GRP=`ls -l $direct/$FILE | awk '{print $4}'`
237			    OWN=`ls -l $direct/$FILE | awk '{print $3}'`
238			    if [ "$MOVOLD" = yes ]
239			    then
240				mv -f $direct/$FILE $direct/OLD$FILE
241				cp $direct/OLD$FILE $direct/$FILE
242				if [ $? = 0 ]
243				then
244				   $ECHO "$FILE moved to $direct/OLD$FILE"
245				else
246				   echo "install: mv $direct/OLD$FILE $direct/$FILE failed"
247				   exit 2
248				fi
249			    fi
250			    if cp $FILEP $direct/$FILE
251			    then
252				chgrp $GRP $direct/$FILE
253				chown $OWN $direct/$FILE
254
255				if [ "$GFLAG" = on ]
256				then
257					chgrp $GROUP $direct/$FILE
258				fi
259				if [ "$MFLAG" = on ]
260				then
261					chmod $MODE $direct/$FILE
262				fi
263				if [ "$UFLAG" = on ]
264				then
265					chown $OWNER $direct/$FILE
266				fi
267
268				$ECHO "$FILEP installed as $direct/$FILE"
269			    else
270				echo "install: cp $FILEP $direct/$FILE failed "
271				exit 2
272			    fi
273			    exit;;
274		esac
275	else
276		cp $FILEP $direct/$FILE
277		if [ $? = 0 ]
278		then
279			$ECHO "$FILEP installed as $direct/$FILE"
280			chgrp $GROUP $direct/$FILE
281			chown $OWNER $direct/$FILE
282			chmod $MODE $direct/$FILE
283		else
284			echo "install: cp $FILEP $direct/$FILE failed "
285			exit 2
286		fi
287	fi
288	exit
289fi
290
291shift
292
293PUTHERE=""
294for i in $*
295do
296	case $i in
297		-*) $USAGE ;;
298	esac
299	PUTHOLD=`find $i -follow -name $FILE -type f -print`
300	PUTHERE=`expr "\`echo $PUTHOLD\`" : '\([^ ]*\)'`
301	if [ "$PUTHERE" != "" ]
302	then break
303	fi
304done
305if [ -r $FLIST -a "$PUTHERE" = "" ]
306then
307	PUTHERE=`grep "/${FILE}$" $FLIST | sed  -n -e '1p'`
308	if [ "$PUTHERE" != "" -a "$ROOT" != "" ]
309	then
310		PUTHERE=${ROOT}${PUTHERE}
311	fi
312fi
313if [ "$PUTHERE" = "" ]
314then
315	for i in $DEFAULT
316	do
317		PUTHOLD=`find $i -follow -name $FILE -type f -print`
318		PUTHERE=`expr "\`echo $PUTHOLD\`" : '\([^ ]*\)'`
319		if [ "$PUTHERE" != "" ]
320		then break
321		fi
322	done
323fi
324if [ "$PUTHERE" != "" ]
325then
326		    GRP=`ls -l $PUTHERE | awk '{print $4}'`
327		    OWN=`ls -l $PUTHERE | awk '{print $3}'`
328		    if [ "$MOVOLD" = yes ]
329		    then
330			old=`echo $PUTHERE | sed -e "s/\/[^\/]*$//"`
331			mv -f $PUTHERE $old/OLD$FILE
332			cp $old/OLD$FILE $PUTHERE
333			if [ $? = 0 ]
334			then
335				    $ECHO "old $FILE moved to $old/OLD$FILE"
336			else
337				   echo "install: cp $direct/OLD$FILE $direct/$FILE failed"
338				    exit 2
339			fi
340		    fi
341		    FOUND=y
342		    if cp $FILEP $PUTHERE
343		    then
344			chgrp $GRP $PUTHERE
345			chown $OWN $PUTHERE
346
347			if [ "$GFLAG" = on ]
348			then
349				chgrp $GROUP $PUTHERE
350			fi
351			if [ "$MFLAG" = on ]
352			then
353				chmod $MODE $PUTHERE
354			fi
355			if [ "$UFLAG" = on ]
356			then
357				chown $OWNER $PUTHERE
358			fi
359
360			$ECHO "$FILEP installed as $PUTHERE"
361			break
362		    else
363			exit 2
364		    fi
365fi
366
367case $FOUND in
368	"") echo "install: $FILE was not found anywhere!"
369	    exit 2;;
370	 y) :	;;
371	 n) cp $FILEP $LASTRES/$FILE
372	    if [ $? = 0 ]
373	    then
374		$ECHO "$FILEP installed as $LASTRES/$FILE by default!"
375		cd $LASTRES
376		chgrp $GROUP $FILE
377		chown $OWNER $FILE
378		chmod $MODE $FILE
379	    else
380		echo "install: cp $FILEP $LASTRES/$FILE failed"
381		exit 2
382	    fi;;
383esac
384