xfstests: make 258 more forgiving of timestamp rounding
[xfstests-dev.git] / install-sh
1 #! /bin/bash
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
6 # modify it under the terms 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,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
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 ""
46     echo "The \$DIST_MANIFEST and \$DIST_ROOT environment variables affect the"
47     echo "behaviour of this command - see comments in the script."
48     echo "The -D flag is only available for the second usage, and causes"
49     echo "the target directory to be created before installing the file."
50     echo ""
51     exit 1
52 }
53
54 _chown ()
55 {
56     _st=255
57     if [ $# -eq 3 ] ; then
58         chown $1:$2 $3
59         _st=$?
60         if [ $_st -ne 0 ] ; then
61             if [ $REAL_UID != '0' ] ; then
62                 if [ ! -f $DIST_ROOT/.chown.quite ] ; then
63                     echo '==============================================='
64                     echo Ownership of files under ${DIST_ROOT:-/}
65                     echo cannot be changed
66                     echo '==============================================='
67                     if [ -n "$DIST_ROOT" ] ; then
68                         touch $DIST_ROOT/.chown.quite
69                     fi
70                 fi
71                _st=0
72             fi     
73         fi
74     fi
75
76     return $_st
77 }
78
79
80 _manifest ()
81
82     echo $* | sed -e 's/\/\//\//g' >>${DIST_MANIFEST:-/dev/null}
83 }
84
85 prog=`basename $0`
86 HERE=`pwd`
87 dflag=false
88 Dflag=false
89 Sflag=false
90 DIRMODE=755
91 FILEMODE=644
92 OWNER=`id -u`
93 GROUP=`id -g`
94 REAL_UID=$OWNER
95
96 # default is to install and don't append manifest
97 INSTALL=true
98 MANIFEST=:
99
100 [ -n "$DIST_MANIFEST" -a -z "$DIST_ROOT" ] && INSTALL=false
101 [ -n "$DIST_MANIFEST" ] && MANIFEST="_manifest"
102
103 [ $# -eq 0 ] && _usage
104
105 if $INSTALL
106 then
107     CP=cp; LN=ln; MKDIR=mkdir; CHMOD=chmod; CHOWN=_chown
108 else
109     CP=true; LN=true; MKDIR=true; CHMOD=true; CHOWN=true
110 fi
111
112 [ -n "$DIST_ROOT" -a $REAL_UID -ne 0 ] && CHOWN=true
113
114 while getopts "Dcm:d:S:o:g:" c $*
115 do
116    case $c in
117    c)
118         ;;
119    g)
120         GROUP=$OPTARG
121         ;;
122    o)
123         OWNER=$OPTARG
124         ;;
125    m)
126         DIRMODE=`expr $OPTARG`
127         FILEMODE=$DIRMODE
128         ;;
129    D) 
130         Dflag=true
131         ;;
132    S) 
133         symlink=$OPTARG
134         Sflag=true
135         ;;
136    d) 
137         dir=$DIST_ROOT/$OPTARG
138         dflag=true
139         ;;
140    *)
141         _usage
142         ;;
143    esac
144 done
145
146 shift `expr $OPTIND - 1`
147
148 status=0
149 if $dflag
150 then
151     #
152     # first usage
153     #
154     $MKDIR -p $dir 
155     status=$?
156     if [ $status -eq 0 ]
157     then
158         $CHMOD $DIRMODE $dir
159         status=$?
160     fi
161     if [ $status -eq 0 ]
162     then
163         $CHOWN $OWNER $GROUP $dir
164         status=$?
165     fi
166     $MANIFEST d $DIRMODE $OWNER $GROUP ${dir#$DIST_ROOT}
167 elif $Sflag
168 then
169     #
170     # fourth usage (symlink)
171     #
172     if [ $# -ne 1 ]
173     then
174         _usage
175     else
176         target=$DIST_ROOT/$1
177     fi
178     $LN -s -f $symlink $target
179     status=$?
180     $MANIFEST l $symlink ${target#$DIST_ROOT} 
181 else
182     list=""
183     dir=""
184     if [ $# -eq 2 ]
185     then
186         #
187         # second usage
188         #
189         f=$1
190         dir=$DIST_ROOT/$2
191         if $Dflag
192         then
193             mkdir -p `dirname $dir`
194         fi
195         $CP $f $dir
196         status=$?
197         if [ $status -eq 0 ]
198         then 
199             if [ -f $dir/$f ]
200             then
201                 $CHMOD $FILEMODE $dir/$f
202                 status=$?
203                 if [ $status -eq 0 ]
204                 then
205                     $CHOWN $OWNER $GROUP $dir/$f
206                     status=$?
207                 fi
208                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
209             else
210                 $CHMOD $FILEMODE $dir
211                 status=$?
212                 if [ $status -eq 0 ]
213                 then
214                     $CHOWN $OWNER $GROUP $dir
215                     status=$?
216                 fi
217                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$dir ${dir#$DIST_ROOT}
218             fi
219         fi
220     else
221         #
222         # third usage
223         #
224         n=1
225         while [ $# -gt 0 ]
226         do
227             if [ $# -gt 1 ]
228             then
229                 list="$list $1"
230             else
231                 dir=$DIST_ROOT/$1
232             fi
233             shift
234         done
235
236         # echo DIR=$dir list=\"$list\"
237         for f in $list
238         do
239             $CP $f $dir
240             status=$?
241             if [ $status -eq 0 ]
242             then
243                 $CHMOD $FILEMODE $dir/$f
244                 status=$?
245                 if [ $status -eq 0 ]
246                 then
247                     $CHOWN $OWNER $GROUP $dir/$f
248                     status=$?
249                 fi
250                 $MANIFEST f $FILEMODE $OWNER $GROUP $HERE/$f ${dir#$DIST_ROOT}/$f
251             fi
252             [ $status -ne 0 ] && break
253         done
254     fi
255 fi
256
257 exit $status