New Script -- run-parts
[pkgusr] / usr / lib / pkgusr / update-pkg-project
1 #!/bin/bash
2 # Copyright © 2010 - 2021 Steve Youngs. All rights reserved.
3 # SPDX-License-Identifier: BSD-3-Clause
4
5 # Author:     Steve Youngs <steve@sxemacs.org>
6 # Maintainer: Steve Youngs <steve@sxemacs.org>
7 # Created:    <2010-12-01>
8 # Time-stamp: <Sunday 16 May 2021 20:05:12 (steve)>
9 # Homepage:   https://git.sxemacs.org/pkgusr
10 # Keywords:   pkgusr package-management tools
11
12 ## This file is part of pkgusr
13
14 ### Commentary:
15 #
16 # Updates the timestamp and contents in a package's .project
17 # $1 -- package to update, if null current pkgusr
18 # $2 -- '--subgroups' optional.  Use if the package is split into
19 # subgroups.
20 #
21 # All the work is done in list_package (see: list_package --help)
22
23 ### Code
24 pkg="$1"
25 subgrps="$2"
26
27 # Allow for 'update-pkg-project --subgroups' invocation
28 # Hint: if it is not a pkgusr doing this the resulting error message
29 # from list_package will set you straight. :)
30 if [[ ${pkg} =~ ^--?subgroups$ ]]; then
31     pkg=$(whoami)
32     subgrps='--subgroups'
33 fi
34
35 # Allow for no cmdline opts
36 if [ -z "${pkg}" ]; then
37     pkg=$(whoami)
38 fi
39
40 pkgdir=/usr/src/${pkg}
41 pkgawk=/usr/lib/pkgusr/pkgdeps.awk
42
43 # Clear out old file list
44 sed -i -n 1,/CONTENTS:/p ${pkgdir}/.project
45 echo "--------" >> ${pkgdir}/.project
46
47 # Update the mandb so we get up to date results
48 # See /usr/lib/pkgusr/mandb and /etc/sudoers.d/99-pkgusr
49 mandb
50
51 # Re-populate .project (source'd to get at $AEXEC)
52 source list_package ${pkg} ${subgrps} >> ${pkgdir}/.project
53
54 # Find deps
55 upd_pkg_deps()
56 {
57     # $AEXEC comes from list_package
58     for file in ${AEXEC[@]}; do
59         if readelf -d $file &>/dev/null && ldd $file &>/dev/null; then
60             (readelf -d $file ; ldd $file ) |
61             awk -f ${pkgawk} | xargs -r stat --printf "%U:%G\n"
62         fi
63     done | sort -u | tr -s '\n' ' '
64 }
65
66
67 # Update Timestamp
68 TIMESTAMP=$(date +%c)
69 sed -i "s/\(Last_Updated: \).*$/\1${TIMESTAMP}/" ${pkgdir}/.project
70
71 # Update Deps (a wee bit ugly in my eyes, sorry)
72 DEPS=$(upd_pkg_deps)
73 DEPS="Deps: ${DEPS% }"                  # trailing whitespace begone!
74 sed -i -e /Deps:/d -e "/Web_Site/a ${DEPS}" ${pkgdir}/.project
75 fmt -p Deps: ${pkgdir}/.project > ${pkgdir}/.project.fmt
76 sed -i 's/^Deps:/         &/' ${pkgdir}/.project.fmt
77 mv ${pkgdir}/.project{.fmt,}
78
79 # If we're root, chown the .project file
80 [[ $(id -u) -eq 0 ]] && chown -v ${pkg}:${pkg} ${pkgdir}/.project
81 exit 0