1 |
#!/bin/bash |
#!/bin/bash |
2 |
|
|
3 |
#query mage database for installed packages |
# query mage database for installed packages |
4 |
# $Header: /home/cvsd/magellan-cvs/magellan-src/mage/usr/lib/mage/magequery.sh,v 1.10 2005-09-09 16:51:51 niro Exp $ |
# $Id$ |
5 |
|
|
6 |
|
# always export C locale to fix utf-8 issues |
7 |
|
export LC_ALL=C |
8 |
|
|
9 |
print_usage() |
print_usage() |
10 |
{ |
{ |
13 |
echo " -n NAME searches for name NAME" |
echo " -n NAME searches for name NAME" |
14 |
echo " -v VER searches for version VER, needs -n" |
echo " -v VER searches for version VER, needs -n" |
15 |
echo " -b BUILD searches for build number BUILD, needs -n -v" |
echo " -b BUILD searches for build number BUILD, needs -n -v" |
16 |
|
echo " -c CAT shows all packages of given categorie" |
17 |
echo " -i shows an inventory of all installed packages" |
echo " -i shows an inventory of all installed packages" |
18 |
|
echo " -f PATH searches packages which own given file" |
19 |
|
echo " -e PATH searches the package which owns the exact given filename" |
20 |
echo |
echo |
21 |
echo " Examples:" |
echo " Examples:" |
22 |
echo |
echo |
27 |
|
|
28 |
# default args: |
# default args: |
29 |
GET_INVENTORY=false |
GET_INVENTORY=false |
30 |
|
SEARCH_ONLY_PATH=false |
31 |
|
SEARCH_ONLY_CAT=false |
32 |
|
SEARCH_EXACT_FILENAME=false |
33 |
|
|
34 |
|
# no params given, or is only an arg not ar param, exit |
35 |
|
if [[ $# -lt 1 ]] || [[ ${1:0:1} != - ]] |
36 |
|
then |
37 |
|
print_usage |
38 |
|
exit 1 |
39 |
|
fi |
40 |
|
|
41 |
while getopts "n:v:b:hi-" opt ; do |
# very basic getops |
42 |
case "${opt}" in |
for i in $* |
43 |
n) |
do |
44 |
S_PNAME="${OPTARG}" |
case $1 in |
45 |
;; |
-n) shift; S_PNAME="$1" ;; |
46 |
|
-v) shift; S_PVER="$1" ;; |
47 |
v) |
-b) shift; S_PBUILD="$1" ;; |
48 |
S_PVER="${OPTARG}" |
-i) GET_INVENTORY="true" ;; |
49 |
;; |
-f) SEARCH_ONLY_PATH="true"; shift; SEARCH_PATH="$1" ;; |
50 |
|
-e) SEARCH_ONLY_PATH="true"; SEARCH_EXACT_FILENAME="true"; shift; SEARCH_PATH="$1" ;; |
51 |
b) |
-c) SEARCH_ONLY_CAT="true"; shift; SEARCH_CAT="$1" ;; |
52 |
S_PBUILD="${OPTARG}" |
-h) print_usage; exit 0 ;; |
53 |
;; |
-*) print_usage; exit 1 ;; |
|
i) |
|
|
GET_INVENTORY="true" |
|
|
;; |
|
|
h) |
|
|
print_usage |
|
|
exit 0 |
|
|
;; |
|
|
|
|
|
-) break |
|
|
;; |
|
|
|
|
|
*) |
|
|
print_usage |
|
|
exit 1 |
|
|
;; |
|
54 |
esac |
esac |
55 |
|
shift |
56 |
done |
done |
|
shift $(($OPTIND - 1)) |
|
|
|
|
57 |
|
|
58 |
source /etc/mage.rc |
source /etc/mage.rc.global |
59 |
|
source ${MAGERC} |
60 |
RETVAL=1 |
RETVAL=1 |
61 |
|
|
62 |
|
if [[ ${SEARCH_ONLY_PATH} = true ]] |
63 |
|
then |
64 |
|
unset S_PACKAGES |
65 |
|
unset i pkg |
66 |
|
if [[ ${SEARCH_EXACT_FILENAME} = true ]] |
67 |
|
then |
68 |
|
# fix ++, which gets interpreted as a regex |
69 |
|
SEARCH_PATH="${SEARCH_PATH//+/\\+}" |
70 |
|
# fix [, which gets interpreted as a regex |
71 |
|
SEARCH_PATH="${SEARCH_PATH//[/\\[}" |
72 |
|
S_CANDIDATES=$(egrep -rl "^${SEARCH_PATH}§" ${INSTALLDB}) |
73 |
|
else |
74 |
|
S_CANDIDATES=$(fgrep -rl "${SEARCH_PATH}" ${INSTALLDB}) |
75 |
|
fi |
76 |
|
for i in ${S_CANDIDATES} |
77 |
|
do |
78 |
|
# ignore magefiles! |
79 |
|
case ${i} in |
80 |
|
*.mage) continue ;; |
81 |
|
esac |
82 |
|
|
83 |
|
# print categorie and pkgname |
84 |
|
pkg="$(basename ${i%/*/*})/$(basename ${i%/*})" |
85 |
|
if [[ -z $(echo ${S_PACKAGES} | grep ${pkg}) ]] |
86 |
|
then |
87 |
|
S_PACKAGES="${S_PACKAGES} ${pkg}" |
88 |
|
fi |
89 |
|
done |
90 |
|
|
91 |
|
# show packages |
92 |
|
for pkg in ${S_PACKAGES} |
93 |
|
do |
94 |
|
echo "${pkg}" |
95 |
|
done |
96 |
|
|
97 |
|
exit 0 |
98 |
|
fi |
99 |
|
|
100 |
|
if [[ ${SEARCH_ONLY_CAT} = true ]] |
101 |
|
then |
102 |
|
# no packages of SEARCH_CAT are installed |
103 |
|
[[ ! -d ${MROOT}${INSTALLDB}/${SEARCH_CAT} ]] && exit 1 |
104 |
|
|
105 |
|
for i in ${INSTALLDB}/${SEARCH_CAT}/* |
106 |
|
do |
107 |
|
# print categorie and pkgname |
108 |
|
echo "$(basename ${i%/*})/$(basename ${i})" |
109 |
|
done |
110 |
|
|
111 |
|
exit 0 |
112 |
|
fi |
113 |
|
|
114 |
if [[ ${GET_INVENTORY} = true ]] |
if [[ ${GET_INVENTORY} = true ]] |
115 |
then |
then |
116 |
for package in $(find ${MROOT}${INSTALLDB} -mindepth 2 -maxdepth 2 -type d -printf "%h,%f\n" | sort) |
for package in $(find ${MROOT}${INSTALLDB} -mindepth 2 -maxdepth 2 -type d -printf "%h,%f\n" | sort) |
117 |
do |
do |
118 |
pcat="$(basename $(echo ${package} | cut -d, -f1))" |
pcat="$(basename $(echo ${package} | cut -d, -f1))" |
119 |
pname="$(echo ${package} | cut -d, -f2)" |
pname="$(echo ${package} | cut -d, -f2)" |
120 |
if [ -z "${invlist}" ] |
if [[ -z ${invlist} ]] |
121 |
then |
then |
122 |
invlist="${pcat}/${pname}" |
invlist="${pcat}/${pname}" |
123 |
else |
else |
139 |
|
|
140 |
x=$(basename ${i}) |
x=$(basename ${i}) |
141 |
PNAME=${x%-*-*} |
PNAME=${x%-*-*} |
142 |
PVER=$(echo ${x#${PNAME}-}| cut -d- -f1) |
PVER=$(echo ${x#${PNAME}-} | cut -d- -f1) |
143 |
PBUILD=$(echo ${x#${PNAME}-}| cut -d- -f2) |
PBUILD=$(echo ${x#${PNAME}-} | cut -d- -f2) |
144 |
|
|
145 |
if [[ ${PNAME} == ${S_PNAME} ]] |
if [[ ${PNAME} == ${S_PNAME} ]] |
146 |
then |
then |
147 |
INST_PNAME=yes |
INST_PNAME=yes |
148 |
|
|
149 |
if [ -n "${S_PVER}" ] |
if [[ -n ${S_PVER} ]] |
150 |
then |
then |
151 |
if [[ ${PVER} == ${S_PVER} ]] |
if [[ ${PVER} == ${S_PVER} ]] |
152 |
then |
then |
153 |
INST_PVER=yes |
INST_PVER=yes |
154 |
fi |
fi |
155 |
|
|
156 |
if [ -n "${S_PBUILD}" ] |
if [[ -n ${S_PBUILD} ]] |
157 |
then |
then |
158 |
if [[ ${PBUILD} == ${S_PBUILD} ]] |
if [[ ${PBUILD} == ${S_PBUILD} ]] |
159 |
then |
then |
163 |
fi |
fi |
164 |
|
|
165 |
# search for pname only |
# search for pname only |
166 |
if [ -n "${S_PNAME}" -a -z "${S_PVER}" -a -z "${S_PBUILD}" ] |
if [[ -n ${S_PNAME} ]] && [[ -z ${S_PVER} ]] && [[ -z ${S_PBUILD} ]] |
167 |
then |
then |
168 |
if [ "${INST_PNAME}" = yes ] |
if [[ ${INST_PNAME} = yes ]] |
169 |
then |
then |
170 |
echo "${S_PNAME} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" |
echo "${S_PNAME} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" |
171 |
RETVAL=0 |
RETVAL=0 |
174 |
fi |
fi |
175 |
|
|
176 |
# search for pname and pver |
# search for pname and pver |
177 |
if [ -n "${S_PNAME}" -a -n "${S_PVER}" -a -z "${S_PBUILD}" ] |
if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -z ${S_PBUILD} ]] |
178 |
then |
then |
179 |
if [ "${INST_PNAME}" = "yes" -a "${INST_PVER}" = "yes" ] |
if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] |
180 |
then |
then |
181 |
echo "${S_PNAME}-${S_PVER} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" |
echo "${S_PNAME}-${S_PVER} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" |
182 |
RETVAL=0 |
RETVAL=0 |
185 |
fi |
fi |
186 |
|
|
187 |
# search for pname, pver and pbuild |
# search for pname, pver and pbuild |
188 |
if [ -n "${S_PNAME}" -a -n "${S_PVER}" -a -n "${S_PBUILD}" ] |
if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -n ${S_PBUILD} ]] |
189 |
then |
then |
190 |
if [ "${INST_PNAME}" = "yes" -a "${INST_PVER}" = "yes" -a "${INST_PBUILD}" = "yes" ] |
if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] && [[ ${INST_PBUILD} = yes ]] |
191 |
then |
then |
192 |
echo "${S_PNAME}-${S_PVER}-${S_PBUILD} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" |
echo "${S_PNAME}-${S_PVER}-${S_PBUILD} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" |
193 |
RETVAL=0 |
RETVAL=0 |