Add files from parent branch HEAD:
[pkgsrc.git] / mk / pkginstall / files
1 # $NetBSD: files,v 1.5 2008/01/04 21:50:27 heinz Exp $
2 #
3 # Generate a +FILES script that reference counts config files that are
4 # required for the proper functioning of the package.
5 #
6 case "${STAGE},$1" in
7 UNPACK,|UNPACK,+FILES)
8         ${CAT} > ./+FILES << 'EOF'
9 #!@SH@
10 #
11 # +FILES - reference-counted configuration file management script
12 #
13 # Usage: ./+FILES ADD|REMOVE|PERMS [metadatadir]
14 #        ./+FILES VIEW-REMOVE depotdir viewdir
15 #        ./+FILES CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]
16 #
17 # This script supports two actions, ADD and REMOVE, that will add or
18 # remove the configuration files needed by the package associated with
19 # <metadatadir>.  The CHECK-ADD action will check whether any files
20 # needed by the package are missing, and print an informative message
21 # noting those files.  The CHECK-REMOVE action will check whether
22 # any files needed by the package still exist, and print an informative
23 # message noting those files.  The CHECK-ADD and CHECK-REMOVE actions
24 # return non-zero if they detect either missing or existing files,
25 # respectively.  The VIEW-REMOVE action will remove from <viewdir> the
26 # links to the configuration files in <depotdir>.  The PERMS action
27 # will correct any ownership or permission discrepancies between the
28 # existing files and the data in this script, and the CHECK-PERMS
29 # action will check whether any files have the wrong ownership or
30 # permission and print an informative message noting those files.  The
31 # CHECK-PERMS action will return non-zero if it detects files with
32 # wrong ownership or permissions.
33 #
34 # Lines starting with "# FILE: " are data read by this script that
35 # name the files that this package requires to exist to function
36 # correctly, along with the locations of the example files, e.g.
37 #
38 #       # FILE: /etc/bar.conf c /example/bar.conf
39 #       # FILE: /etc/baz/conf c /example/baz.conf 0600 foo-user foo-group
40 #
41 # For each FILE entry, if the file path is relative, then it is taken to
42 # be relative to ${PKG_PREFIX}.
43 #
44 # The second field in each FILE entry is a set of flags with the following
45 # meaning:
46 #
47 #       c       file is copied into place
48 #       f       ignore ${PKG_CONFIG}
49 #       r       file is an rc.d script (consider ${PKG_RCD_SCRIPTS})
50 #
51 AWK="@AWK@"
52 CAT="@CAT@"
53 CP="@CP@"
54 CHGRP="@CHGRP@"
55 CHMOD="@CHMOD@"
56 CHOWN="@CHOWN@"
57 CMP="@CMP@"
58 ECHO="@ECHO@"
59 GREP="@GREP@"
60 LS="@LS@"
61 MKDIR="@MKDIR@"
62 MV="@MV@"
63 PWD_CMD="@PWD_CMD@"
64 RM="@RM@"
65 RMDIR="@RMDIR@"
66 SED="@SED@"
67 SORT="@SORT@"
68 TEST="@TEST@"
69 TRUE="@TRUE@"
70
71 SELF=$0
72 ACTION=$1
73
74 : ${PKG_PREFIX=@PREFIX@}
75
76 case "${PKG_CONFIG:-@PKG_CONFIG@}" in
77 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
78         _PKG_CONFIG=yes
79         ;;
80 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
81         _PKG_CONFIG=no
82         ;;
83 esac
84 case "${PKG_CONFIG_PERMS:-@PKG_CONFIG_PERMS@}" in
85 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
86         _PKG_CONFIG_PERMS=yes
87         ;;
88 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
89         _PKG_CONFIG_PERMS=no
90         ;;
91 esac
92 case "${PKG_RCD_SCRIPTS:-@PKG_RCD_SCRIPTS@}" in
93 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
94         _PKG_RCD_SCRIPTS=yes
95         ;;
96 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
97         _PKG_RCD_SCRIPTS=no
98         ;;
99 esac
100
101 case $ACTION in
102 VIEW-REMOVE)
103         DEPOTDIR="$2"
104         VIEWDIR="$3"
105         ${TEST} -n "${DEPOTDIR}" -a -n "${VIEWDIR}" || exit 0
106         ;;
107 *)
108         CURDIR=`${PWD_CMD}`
109         PKG_METADATA_DIR="${2-${CURDIR}}"
110         : ${PKGNAME=${PKG_METADATA_DIR##*/}}
111         : ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
112         : ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}
113         PKG_REFCOUNT_FILES_DBDIR="${PKG_REFCOUNT_DBDIR}/files"
114         ;;
115 esac
116
117 exitcode=0
118 case $ACTION in
119 ADD)
120         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -u |
121         while read file f_flags f_eg f_mode f_user f_group; do
122                 case $file in
123                 "")     continue ;;
124                 [!/]*)  file="${PKG_PREFIX}/$file" ;;
125                 esac
126                 case $f_flags in
127                 *c*)    ;;
128                 *)      continue ;;
129                 esac
130                 case $f_eg in
131                 "")     continue ;;
132                 [!/]*)  f_eg="${PKG_PREFIX}/$f_eg" ;;
133                 esac
134
135                 shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
136                 perms="$shadow_dir/+PERMISSIONS"
137                 preexist="$shadow_dir/+PREEXISTING"
138                 token="$shadow_dir/${PKGNAME}"
139                 if ${TEST} ! -d "$shadow_dir"; then
140                         ${MKDIR} $shadow_dir
141                         ${TEST} ! -f "$file" ||
142                                 ${ECHO} "${PKGNAME}" > $preexist
143                 fi
144                 if ${TEST} -f "$token" && \
145                    ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
146                         :
147                 else
148                         ${ECHO} "${PKG_METADATA_DIR}" >> $token
149                 fi
150
151                 case $f_mode$f_user$f_group in
152                 "")     ;;
153                 *)      ${ECHO} "$f_mode $f_user $f_group" > $perms ;;
154                 esac
155                 if ${TEST} ! -f "$file" -a ! -f "$f_eg" -a ! -c "$f_eg"; then
156                         :
157                 else
158                         case "$f_flags:$_PKG_CONFIG:$_PKG_RCD_SCRIPTS" in
159                         *f*:*:*|[!r]:yes:*|[!r][!r]:yes:*|[!r][!r][!r]:yes:*|*r*:yes:yes)
160                                 if ${TEST} -f "$file"; then
161                                         ${ECHO} "${PKGNAME}: $file already exists"
162                                 elif ${TEST} -f "$f_eg" -o -c "$f_eg"; then
163                                         ${ECHO} "${PKGNAME}: copying $f_eg to $file"
164                                         ${CP} $f_eg $file
165                                         case $f_user in
166                                         "")     ;;
167                                         *)      ${CHOWN} $f_user $file ;;
168                                         esac
169                                         case $f_group in
170                                         "")     ;;
171                                         *)      ${CHGRP} $f_group $file ;;
172                                         esac
173                                         case $f_mode in
174                                         "")     ;;
175                                         *)      ${CHMOD} $f_mode $file ;;
176                                         esac
177                                 fi
178                                 ;;
179                         esac
180                 fi
181         done
182         ;;
183
184 REMOVE)
185         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
186         while read file f_flags f_eg f_mode f_user f_group; do
187                 case $file in
188                 "")     continue ;;
189                 [!/]*)  file="${PKG_PREFIX}/$file" ;;
190                 esac
191                 case $f_flags in
192                 *c*)    ;;
193                 *)      continue ;;
194                 esac
195                 case $f_eg in
196                 "")     continue ;;
197                 [!/]*)  f_eg="${PKG_PREFIX}/$f_eg" ;;
198                 esac
199
200                 shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
201                 perms="$shadow_dir/+PERMISSIONS"
202                 preexist="$shadow_dir/+PREEXISTING"
203                 token="$shadow_dir/${PKGNAME}"
204                 tokentmp="$token.tmp.$$"
205                 if ${TEST} -f "$token" && \
206                    ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
207                         ${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
208                         case `${CAT} $tokentmp | ${SED} -n "$="` in
209                         "")
210                                 if ${TEST} -f "$preexist"; then
211                                         :
212                                 elif ${TEST} -f "$file" -a \( -f "$f_eg" -o -c "$f_eg" \) && \
213                                      ${CMP} -s "$file" "$f_eg"; then
214                                         case "$f_flags:$_PKG_CONFIG:$_PKG_RCD_SCRIPTS" in
215                                         *f*:*:*|[!r]:yes:*|[!r][!r]:yes:*|[!r][!r][!r]:yes:*|*r*:yes:yes)
216                                                 ${RM} -f "$file"
217                                                 ;;
218                                         esac
219                                 fi
220                                 ${RM} -f $perms $preexist $token $token.tmp.*
221                                 ${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
222                                 ;;
223                         *)
224                                 ${MV} -f $tokentmp $token
225                                 ;;
226                         esac
227                 fi
228         done
229         ;;
230
231 PERMS)
232         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
233         while read file f_flags f_eg f_mode f_user f_group; do
234                 case $_PKG_CONFIG:$_PKG_CONFIG_PERMS in
235                 yes:yes)        ;;
236                 *)              continue ;;
237                 esac
238                 case $file in
239                 "")     continue ;;
240                 [!/]*)  file="${PKG_PREFIX}/$file" ;;
241                 esac
242                 case $f_user in
243                 "")     ;;
244                 *)      ${CHOWN} $f_user $file ;;
245                 esac
246                 case $f_group in
247                 "")     ;;
248                 *)      ${CHGRP} $f_group $file ;;
249                 esac
250                 case $f_mode in
251                 "")     ;;
252                 *)      ${CHMOD} $f_mode $file ;;
253                 esac
254         done
255         ;;
256
257 VIEW-REMOVE)
258         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
259         while read file f_flags f_eg f_mode f_user f_group; do
260                 case $file in
261                 ${DEPOTDIR}/*)  link="${VIEWDIR}/${file#${DEPOTDIR}/}" ;;
262                 [!/]*)          link="${VIEWDIR}/$file" ;;
263                 *)              continue ;;
264                 esac
265                 dir="${link%[^/]*}"
266                 if ${TEST} -h "$link"; then
267                         ${RM} -f $link
268                         ${RMDIR} -p $dir 2>/dev/null || ${TRUE}
269                 fi
270         done
271         ;;
272
273 CHECK-ADD)
274         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
275         { while read file f_flags f_eg f_mode f_user f_group; do
276                 case $file in
277                 "")     continue ;;
278                 [!/]*)  file="${PKG_PREFIX}/$file" ;;
279                 esac
280                 ${TEST} ! -f "$file" || continue
281                 case $f_flags in
282                 *c*)    ;;
283                 *)      continue ;;
284                 esac
285                 case $f_eg in
286                 "")     continue ;;
287                 [!/]*)  f_eg="${PKG_PREFIX}/$f_eg" ;;
288                 esac
289
290                 case "$printed_header" in
291                 yes)    ;;
292                 *)      printed_header=yes
293                         ${ECHO} "==========================================================================="
294                         ${ECHO} "The following files should be created for ${PKGNAME}:"
295                         ;;
296                 esac
297                 ${ECHO} ""
298                 case $f_mode:$f_user:$f_group in
299                 ::)
300                         ${ECHO} "       $file"
301                         ;;
302                 [!:]*::)
303                         ${ECHO} "       $file (m=$f_mode)"
304                         ;;
305                 [!:]*:[!:]*:)
306                         ${ECHO} "       $file (m=$f_mode, o=$f_user)"
307                         ;;
308                 [!:]*:[!:]*:[!:]*)
309                         ${ECHO} "       $file (m=$f_mode, o=$f_user, g=$f_group)"
310                         ;;
311                 esac
312                 ${TEST} ! -f "$f_eg" || ${ECHO} "           [$f_eg]"
313         done
314         case "$printed_header" in
315         yes)    ${ECHO} ""
316                 ${ECHO} "==========================================================================="
317                 exit 1
318                 ;;
319         esac; }
320         ${TEST} $? -eq 0 || exitcode=1
321         ;;
322
323 CHECK-REMOVE)
324         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
325         { while read file f_flags f_eg f_mode f_user f_group; do
326                 case $file in
327                 "")     continue ;;
328                 [!/]*)  file="${PKG_PREFIX}/$file" ;;
329                 esac
330                 ${TEST} -f "$file" || continue
331                 shadow_dir="${PKG_REFCOUNT_FILES_DBDIR}$file"
332                 ${TEST} ! -d "$shadow_dir" || continue  # refcount isn't zero
333                 case "$printed_header" in
334                 yes)    ;;
335                 *)      printed_header=yes
336                         ${ECHO} "==========================================================================="
337                         ${ECHO} "The following files are no longer being used by ${PKGNAME},"
338                         ${ECHO} "and they can be removed if no other packages are using them:"
339                         ${ECHO} ""
340                         ;;
341                 esac
342                 ${ECHO} "       $file"
343         done
344         case "$printed_header" in
345         yes)    ${ECHO} ""
346                 ${ECHO} "==========================================================================="
347                 exit 1
348                 ;;
349         esac; }
350         ${TEST} $? -eq 0 || exitcode=1
351         ;;
352
353 CHECK-PERMS)
354         tmpdir="./.pkginstall.$$"
355         ${MKDIR} -p $tmpdir 2>/dev/null || exit 1
356         ${CHMOD} 0700 $tmpdir
357         ${SED} -n "/^\# FILE: /{s/^\# FILE: //;p;}" ${SELF} | ${SORT} -ru |
358         { while read file f_flags f_eg f_mode f_user f_group; do
359                 case $file in
360                 "")     continue ;;
361                 [!/]*)  file="${PKG_PREFIX}/$file" ;;
362                 esac
363                 ${TEST} -f "$file" || continue
364                 case $f_mode:$f_user:$f_group in
365                 ::)     continue ;;
366                 esac
367
368                 perms=`${LS} -l $file | ${AWK} '{ print $1":"$3":"$4 }'`
369                 testpath="$tmpdir/file_perms"
370                 ${ECHO} > $testpath
371                 ${CHMOD} $f_mode $testpath 2>/dev/null
372                 longmode=`${LS} -l $testpath | ${AWK} '{ print $1 }'`
373                 case $f_mode:$f_user:$f_group in
374                 :[!:]*:)
375                         case "$perms" in
376                         *:$f_user:*)    continue ;;
377                         esac
378                         ;;
379                 :[!:]*:[!:]*)
380                         case "$perms" in
381                         *:$f_user:$f_group)     continue ;;
382                         esac
383                         ;;
384                 [!:]*::)
385                         case "$perms" in
386                         $longmode:*:*)  continue ;;
387                         esac
388                         ;;
389                 [!:]*:[!:]*:)
390                         case "$perms" in
391                         $longmode:$f_user:*)    continue ;;
392                         esac
393                         ;;
394                 [!:]*:[!:]*:[!:]*)
395                         case "$perms" in
396                         $longmode:$f_user:$f_group)     continue ;;
397                         esac
398                         ;;
399                 esac
400
401                 case "$printed_header" in
402                 yes)    ;;
403                 *)      printed_header=yes
404                         ${ECHO} "==========================================================================="
405                         ${ECHO} "The following files are used by ${PKGNAME} and have"
406                         ${ECHO} "the wrong ownership and/or permissions:"
407                         ${ECHO} ""
408                         ;;
409                 esac
410                 case $f_mode:$f_user:$f_group in
411                 [!:]*::)
412                         ${ECHO} "       $file (m=$f_mode)"
413                         ;;
414                 [!:]*:[!:]*:)
415                         ${ECHO} "       $file (m=$f_mode, o=$f_user)"
416                         ;;
417                 [!:]*:[!:]*:[!:]*)
418                         ${ECHO} "       $file (m=$f_mode, o=$f_user, g=$f_group)"
419                         ;;
420                 esac
421         done
422         case "$printed_header" in
423         yes)    ${ECHO} ""
424                 ${ECHO} "==========================================================================="
425                 exit 1
426                 ;;
427         esac; }
428         ${TEST} $? -eq 0 || exitcode=1
429         ${RM} -fr $tmpdir
430         ;;
431
432 *)
433         ${ECHO} "Usage: ./+FILES ADD|REMOVE|PERMS [metadatadir]"
434         ${ECHO} "       ./+FILES VIEW-REMOVE depotdir viewdir"
435         ${ECHO} "       ./+FILES CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]"
436         ;;
437 esac
438 exit $exitcode
439
440 EOF
441         ${SED} -n "/^\# FILE: /p" ${SELF} >> ./+FILES
442         ${CHMOD} +x ./+FILES
443         ;;
444 esac
445