Debian Codegraph @

check-enhancements

raw
1 #!/bin/bash
2
3 # Copyright (C) 2012 George Danchev <[email protected]>
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, or (at your option)
8 # 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
18 # 02111-1307, USA.
19
20 SELF=$(basename "$0")
21 VER=0.3
22
23 OPT_CHECK_INSTALLED_PACKAGES=0
24 OPT_SHOW_INSTALLED_ENHANCEMENTS=0
25 OPT_VERBOSE=0
26
27 print_version() {
28 echo "$VER"
29 }
30
31 print_help() {
32 cat << HLP
33 ${SELF} ( -ip | -ie | -h | --verbose | --version )
34
35 Options:
36
37 Program options start with dash or double dash, otherwise they
38 are interpreted as package names. Only non-installed enhancement
39 packages are printed out by default, use -ie to show them all.
40
41 -ip, --ip, -installed-packages, --installed-packages
42 Show enhancements of all installed packages (could be slow)
43 -ie, --ie, -installed-enhancements, --installed-enhancements
44 Show installed enhancement packages too
45 -h, -help, --help
46 -version, --version
47 -verbose, --verbose
48
49 Examples:
50
51 # check enhancement packages of all installed packages
52 ${SELF} -ip
53 # check enhancement packages of selected packages
54 ${SELF} pkg1 pkgN
55
56 HLP
57 }
58
59 pkgs_enhancing_pkg_status() {
60 # Figure out which package list files are there
61 PACKAGEFILES=$(apt-get indextargets --format '$(FILENAME)' "Created-By: Packages")
62 # Figure out package Enhances:'ing a given package
63 EN=$(/usr/lib/apt/apt-helper cat-file $PACKAGEFILES | grep-dctrl -n -s Package -F Enhances -X "${1}")
64 case $? in
65 0) if test $OPT_VERBOSE != 0; then echo -e "Package <<$1>> could be Enhanced by:"; fi
66 ;;
67 1) return
68 ;;
69 *) echo "${SELF}: Internal Error!"
70 exit 1
71 ;;
72 esac
73 for e in `echo "${EN}" | sort | uniq | xargs` # now sort and unify
74 do
75 # Figure out whether those enhancements are installed or not.
76 # By default only output non-installed enhancements
77 OUT=""
78 case "${OPT_SHOW_INSTALLED_ENHANCEMENTS}" in
79 0) OUT=`apt-cache policy ${e} | head -3 | paste -s | grep "Installed: (none)"`
80 ;;
81 1) OUT=`apt-cache policy ${e} | head -3 | paste -s`
82 ;;
83 esac
84 if test x"$OUT" != x""; then printf "%2s => " "$1" && echo "${OUT}"; fi
85 done
86 }
87
88 # main
89 declare -a PKGS=()
90 for op in $@
91 do
92 case "${op}" in
93 -ip|--ip|-installed-packages|--installed-packages)
94 OPT_CHECK_INSTALLED_PACKAGES=1
95 ;;
96
97 -ie|--ie|-installed-enhancements|--installed-enhancements)
98 OPT_SHOW_INSTALLED_ENHANCEMENTS=1
99 ;;
100
101 -h|-help|--help)
102 print_help
103 exit 0
104 ;;
105
106 -version|--version)
107 print_version
108 exit 0
109 ;;
110
111 -verbose|--verbose)
112 OPT_VERBOSE=1
113 ;;
114
115 -*)
116 echo "invalid option ${op}"
117 print_help
118 exit 1
119 ;;
120
121 *)
122 # PKGS[$[${#PKGS[@]}+1]]="${op}"
123 PKGS+=(${op})
124 ;;
125 esac
126 done
127
128 # no options, just show help
129 if test x"$1" == x""; then print_help; exit 0; fi
130
131 # process package names given on the cmdline
132 for cmdline_pkg in "${PKGS[@]}"
133 do
134 pkgs_enhancing_pkg_status $cmdline_pkg
135 done
136
137 # process installed packages, as well
138 case $OPT_CHECK_INSTALLED_PACKAGES in
139 1)
140 for installed in $(grep-status -FStatus "install ok installed" -n -s Package | sort | uniq)
141 do
142 pkgs_enhancing_pkg_status $installed
143 done
144 ;;
145 esac
146
147 exit 0