Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / mk / pkginstall / dirs
blobc783cbfca88043d31c93aea95a6883fe6683c9c9
1 # $NetBSD: dirs,v 1.3 2006/12/15 12:46:24 martti Exp $
3 # Generate a +DIRS script that reference counts directories that are
4 # required for the proper functioning of the package.
6 case "${STAGE},$1" in
7 UNPACK,|UNPACK,+DIRS)
8         ${CAT} > ./+DIRS << 'EOF'
9 #!@SH@
11 # +DIRS - reference-counted directory management script
13 # Usage: ./+DIRS ADD|REMOVE|PERMS [metadatadir]
14 #        ./+DIRS CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]
16 # This script supports two actions, ADD and REMOVE, that will add or
17 # remove the directories needed by the package associated with
18 # <metadatadir>.  The CHECK-ADD action will check whether any directories
19 # needed by the package are missing, and print an informative message
20 # noting those directories.  The CHECK-REMOVE action will check whether
21 # any directories needed by the package still exist, and print an
22 # informative message noting those directories.  The CHECK-ADD and
23 # CHECK-REMOVE actions return non-zero if they detect either missing
24 # or existing directories, respectively.  The PERMS action will correct
25 # any ownership or permission discrepancies between the existing
26 # directories and the data in this script, and the CHECK-PERMS action
27 # will check whether any directories have the wrong ownership or
28 # permission and print an informative message noting those directories.
29 # The CHECK-PERMS action will return non-zero if it detects directories
30 # with wrong ownership or permissions.
32 # Lines starting with "# DIR: " are data read by this script that
33 # name the directories that this package requires to exist to function
34 # correctly, e.g.
36 #       # DIR: /etc/foo m
37 #       # DIR: /var/log/foo/tmp mo 0700 foo-user foo-group
38 #       # DIR: share/foo-plugins fm
40 # For each DIR entry, if the directory path is relative, then it is taken
41 # to be relative to ${PKG_PREFIX}.
43 # The second field in each DIR entry is a set of flags with the following
44 # meanings:
46 #       f       ignore ${PKG_CONFIG}
47 #       m       create (make) the directory when ADDing
48 #       o       directory is owned by the package
50 AWK="@AWK@"
51 CAT="@CAT@"
52 CHGRP="@CHGRP@"
53 CHMOD="@CHMOD@"
54 CHOWN="@CHOWN@"
55 ECHO="@ECHO@"
56 GREP="@GREP@"
57 LS="@LS@"
58 MKDIR="@MKDIR@"
59 MV="@MV@"
60 PWD_CMD="@PWD_CMD@"
61 RM="@RM@"
62 RMDIR="@RMDIR@"
63 SED="@SED@"
64 SORT="@SORT@"
65 TEST="@TEST@"
66 TRUE="@TRUE@"
68 SELF=$0
69 ACTION=$1
71 CURDIR=`${PWD_CMD}`
72 PKG_METADATA_DIR="${2-${CURDIR}}"
73 : ${PKGNAME=${PKG_METADATA_DIR##*/}}
74 : ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
75 : ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}
76 : ${PKG_PREFIX=@PREFIX@}
78 PKG_REFCOUNT_DIRS_DBDIR="${PKG_REFCOUNT_DBDIR}/dirs"
80 case "${PKG_CONFIG:-@PKG_CONFIG@}" in
81 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
82         _PKG_CONFIG=yes
83         ;;
84 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
85         _PKG_CONFIG=no
86         ;;
87 esac
88 case "${PKG_CONFIG_PERMS:-@PKG_CONFIG_PERMS@}" in
89 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
90         _PKG_CONFIG_PERMS=yes
91         ;;
92 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
93         _PKG_CONFIG_PERMS=no
94         ;;
95 esac
97 exitcode=0
98 case $ACTION in
99 ADD)
100         ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
101         while read dir d_flags d_mode d_user d_group; do
102                 case $dir in
103                 "")     continue ;;
104                 [!/]*)  dir="${PKG_PREFIX}/$dir" ;;
105                 esac
106                 case $d_flags in
107                 *m*)    ;;
108                 *)      continue ;;
109                 esac
110                 shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
111                 perms="$shadow_dir/+PERMISSIONS"
112                 preexist="$shadow_dir/+PREEXISTING"
113                 token="$shadow_dir/${PKGNAME}"
114                 if ${TEST} ! -d "$shadow_dir"; then
115                         ${MKDIR} $shadow_dir
116                         ${TEST} ! -d "$dir" ||
117                                 ${ECHO} "${PKGNAME}" > $preexist
118                 fi
119                 if ${TEST} -f "$token" && \
120                    ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
121                         :
122                 else
123                         ${ECHO} "${PKG_METADATA_DIR}" >> $token
124                 fi
125                 case $d_mode$d_user$d_group in
126                 "")     ;;
127                 *)      ${ECHO} "$d_user $d_group $d_mode" > $perms ;;
128                 esac
129                 case $d_flags:$_PKG_CONFIG in
130                 *f*:*|*:yes)
131                         ${MKDIR} $dir
132                         case $d_user in
133                         "")     ;;
134                         *)      ${CHOWN} $d_user $dir ;;
135                         esac
136                         case $d_group in
137                         "")     ;;
138                         *)      ${CHGRP} $d_group $dir ;;
139                         esac
140                         case $d_mode in
141                         "")     ;;
142                         *)      ${CHMOD} $d_mode $dir ;;
143                         esac
144                         ;;
145                 esac
146         done
147         ;;
149 REMOVE)
150         ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
151         while read dir d_flags d_mode d_user d_group; do
152                 case $dir in
153                 "")     continue ;;
154                 [!/]*)  dir="${PKG_PREFIX}/$dir" ;;
155                 esac
156                 case $d_flags in
157                 *m*)    ;;
158                 *)      continue ;;
159                 esac
160                 shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
161                 perms="$shadow_dir/+PERMISSIONS"
162                 preexist="$shadow_dir/+PREEXISTING"
163                 token="$shadow_dir/${PKGNAME}"
164                 tokentmp="$token.tmp.$$"
165                 if ${TEST} -f "$token" && \
166                    ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
167                         ${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
168                         case `${CAT} $tokentmp | ${SED} -n "$="` in
169                         "")
170                                 ${TEST} -f "$preexist" ||
171                                         { case $d_flags:$_PKG_CONFIG in
172                                           *f*:*|*:yes)
173                                                 ${RMDIR} -p $dir 2>/dev/null || ${TRUE};
174                                                 ;;
175                                           esac; }
176                                 ${RM} -f $perms $preexist $token $token.tmp.*
177                                 ${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
178                                 ;;
179                         *)
180                                 ${MV} -f $tokentmp $token
181                                 ;;
182                         esac
183                 fi
184         done
185         ;;
187 PERMS)
188         ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
189         while read dir d_flags d_mode d_user d_group; do
190                 case $_PKG_CONFIG:$_PKG_CONFIG_PERMS in
191                 yes:yes)        ;;
192                 *)              continue ;;
193                 esac
194                 case $dir in
195                 "")     continue ;;
196                 [!/]*)  dir="${PKG_PREFIX}/$dir" ;;
197                 esac
198                 case $d_user in
199                 "")     ;;
200                 *)      ${CHOWN} $d_user $dir ;;
201                 esac
202                 case $d_group in
203                 "")     ;;
204                 *)      ${CHGRP} $d_group $dir ;;
205                 esac
206                 case $d_mode in
207                 "")     ;;
208                 *)      ${CHMOD} $d_mode $dir ;;
209                 esac
210         done
211         ;;
213 CHECK-ADD)
214         ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
215         { while read dir d_flags d_mode d_user d_group; do
216                 case $dir in
217                 "")     continue ;;
218                 [!/]*)  dir="${PKG_PREFIX}/$dir" ;;
219                 esac
220                 ${TEST} ! -d "$dir" || continue
221                 case $d_flags in
222                 *m*)    ;;
223                 *)      continue ;;
224                 esac
225                 case "$printed_header" in
226                 yes)    ;;
227                 *)      printed_header=yes
228                         ${ECHO} "==========================================================================="
229                         ${ECHO} "The following directories should be created for ${PKGNAME}:"
230                         ${ECHO} ""
231                         ;;
232                 esac
233                 case $d_user:$d_group:$d_mode in
234                 [!:]*:[!:]*:[!:]*)
235                         ${ECHO} "       $dir (m=$d_mode, o=$d_user, g=$d_group)"
236                         ;;
237                 *)
238                         ${ECHO} "       $dir"
239                         ;;
240                 esac
241         done
242         case "$printed_header" in
243         yes)    ${ECHO} ""
244                 ${ECHO} "==========================================================================="
245                 exit 1
246                 ;;
247         esac; }
248         ${TEST} $? -eq 0 || exitcode=1
249         ;;
251 CHECK-REMOVE)
252         ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
253         { while read dir d_flags d_mode d_user d_group; do
254                 case $dir in
255                 "")     continue ;;
256                 [!/]*)  dir="${PKG_PREFIX}/$dir" ;;
257                 esac
258                 ${TEST} -d "$dir" || continue
259                 case $d_flags in
260                 *o*)    ;;
261                 *)      continue ;;
262                 esac
263                 shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
264                 ${TEST} ! -d "$shadow_dir" || continue  # refcount isn't zero
265                 case "$printed_header" in
266                 yes)    ;;
267                 *)      printed_header=yes
268                         ${ECHO} "==========================================================================="
269                         ${ECHO} "The following directories are no longer being used by ${PKGNAME},"
270                         ${ECHO} "and they can be removed if no other packages are using them:"
271                         ${ECHO} ""
272                         ;;
273                 esac
274                 ${ECHO} "       $dir"
275         done
276         case "$printed_header" in
277         yes)    ${ECHO} ""
278                 ${ECHO} "==========================================================================="
279                 exit 1
280                 ;;
281         esac; }
282         ${TEST} $? -eq 0 || exitcode=1
283         ;;
285 CHECK-PERMS)
286         tmpdir="./.pkginstall.$$"
287         ${MKDIR} -p $tmpdir 2>/dev/null || exit 1
288         ${CHMOD} 0700 $tmpdir
289         ${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
290         { while read dir d_flags d_mode d_user d_group; do
291                 case $dir in
292                 "")     continue ;;
293                 [!/]*)  dir="${PKG_PREFIX}/$dir" ;;
294                 esac
295                 ${TEST} -d "$dir" || continue
296                 case $d_user:$d_group:$d_mode in
297                 ::)     continue ;;
298                 esac
300                 perms=`${LS} -ld $dir | ${AWK} '{ print $1":"$3":"$4 }'`
301                 testpath="$tmpdir/dir_perms"
302                 ${MKDIR} -p $testpath
303                 ${CHMOD} $d_mode $testpath 2>/dev/null
304                 longmode=`${LS} -ld $testpath | ${AWK} '{ print $1 }'`
305                 case $d_mode:$d_user:$d_group in
306                 :[!:]*:)
307                         case "$perms" in
308                         *:$d_user:*)    continue ;;
309                         esac
310                         ;;
311                 :[!:]*:[!:]*)
312                         case "$perms" in
313                         *:$d_user:$d_group)     continue ;;
314                         esac
315                         ;;
316                 [!:]*::)
317                         case "$perms" in
318                         $longmode:*:*)  continue ;;
319                         esac
320                         ;;
321                 [!:]*:[!:]*:)
322                         case "$perms" in
323                         $longmode:$d_user:*)    continue ;;
324                         esac
325                         ;;
326                 [!:]*:[!:]*:[!:]*)
327                         case "$perms" in
328                         $longmode:$d_user:$d_group)     continue ;;
329                         esac
330                         ;;
331                 esac
333                 case "$printed_header" in
334                 yes)    ;;
335                 *)      printed_header=yes
336                         ${ECHO} "==========================================================================="
337                         ${ECHO} "The following directories are used by ${PKGNAME} and"
338                         ${ECHO} "have the wrong ownership and/or permissions:"
339                         ${ECHO} ""
340                         ;;
341                 esac
342                 case $d_mode:$d_user:$d_group in
343                 [!:]*::)
344                         ${ECHO} "       $dir (m=$d_mode)"
345                         ;;
346                 [!:]*:[!:]*:)
347                         ${ECHO} "       $dir (m=$d_mode, o=$d_user)"
348                         ;;
349                 [!:]*:[!:]*:[!:]*)
350                         ${ECHO} "       $dir (m=$d_mode, o=$d_user, g=$d_group)"
351                         ;;
352                 esac
353         done
354         case "$printed_header" in
355         yes)    ${ECHO} ""
356                 ${ECHO} "==========================================================================="
357                 exit 1
358                 ;;
359         esac; }
360         ${TEST} $? -eq 0 || exitcode=1
361         ${RM} -fr $tmpdir
362         ;;
365         ${ECHO} "Usage: ./+DIRS ADD|REMOVE|PERMS [metadatadir]"
366         ${ECHO} "       ./+DIRS CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]"
367         ;;
368 esac
369 exit $exitcode
372         ${SED} -n "/^\# DIR: /p" ${SELF} >> ./+DIRS
373         ${CHMOD} +x ./+DIRS
374         ;;
375 esac