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