Annotation of /branches/mage-next/src/magequery.in
Parent Directory | Revision Log
Revision 1290 -
(hide annotations)
(download)
(as text)
Thu May 12 22:20:59 2011 UTC (13 years, 4 months ago) by niro
Original Path: trunk/mage/usr/lib/mage/magequery.sh
File MIME type: application/x-sh
File size: 3831 byte(s)
Thu May 12 22:20:59 2011 UTC (13 years, 4 months ago) by niro
Original Path: trunk/mage/usr/lib/mage/magequery.sh
File MIME type: application/x-sh
File size: 3831 byte(s)
-reworked magequery getopts and fixed coding style
1 | niro | 32 | #!/bin/bash |
2 | |||
3 | niro | 1290 | # query mage database for installed packages |
4 | # $Id$ | ||
5 | niro | 32 | |
6 | print_usage() | ||
7 | { | ||
8 | echo "$(basename $0 .sh) querys the mage database for installed packages." | ||
9 | echo | ||
10 | echo " -n NAME searches for name NAME" | ||
11 | echo " -v VER searches for version VER, needs -n" | ||
12 | echo " -b BUILD searches for build number BUILD, needs -n -v" | ||
13 | niro | 777 | echo " -c CAT shows all packages of given categorie" |
14 | niro | 210 | echo " -i shows an inventory of all installed packages" |
15 | niro | 1206 | echo " -f PATH searches package names in given path" |
16 | niro | 32 | echo |
17 | echo " Examples:" | ||
18 | echo | ||
19 | echo " $(basename $0 .sh) -n xorg -v 6.8.0 -b r1" | ||
20 | echo " searches for package xorg-6.8.0-r1" | ||
21 | echo | ||
22 | } | ||
23 | |||
24 | niro | 210 | # default args: |
25 | GET_INVENTORY=false | ||
26 | niro | 304 | SEARCH_ONLY_PATH=false |
27 | niro | 777 | SEARCH_ONLY_CAT=false |
28 | niro | 210 | |
29 | niro | 1290 | # no params given, or is only an arg not ar param, exit |
30 | if [[ $# -lt 1 ]] || [[ ${1:0:1} != - ]] | ||
31 | then | ||
32 | print_usage | ||
33 | exit 1 | ||
34 | fi | ||
35 | |||
36 | # very basic getops | ||
37 | for i in $* | ||
38 | niro | 1206 | do |
39 | niro | 1290 | case $1 in |
40 | -n) shift; S_PNAME="$1" ;; | ||
41 | -v) shift; S_PVER="$1" ;; | ||
42 | -b) shift; S_PBUILD="$1" ;; | ||
43 | -i) GET_INVENTORY="true" ;; | ||
44 | -f) SEARCH_ONLY_PATH="true"; shift; SEARCH_PATH="$1" ;; | ||
45 | -c) SEARCH_ONLY_CAT="true"; shift; SEARCH_CAT="$1" ;; | ||
46 | -h) print_usage; exit 0 ;; | ||
47 | -*) print_usage; exit 1 ;; | ||
48 | niro | 32 | esac |
49 | niro | 1290 | shift |
50 | niro | 32 | done |
51 | |||
52 | niro | 304 | source /etc/mage.rc.global |
53 | niro | 229 | source /etc/mage.rc |
54 | niro | 32 | RETVAL=1 |
55 | |||
56 | niro | 304 | if [[ ${SEARCH_ONLY_PATH} = true ]] |
57 | then | ||
58 | unset S_PACKAGES | ||
59 | unset i pkg | ||
60 | niro | 332 | for i in $(fgrep -rl "${SEARCH_PATH}" ${INSTALLDB}) |
61 | niro | 304 | do |
62 | niro | 1288 | # ignore magefiles! |
63 | case ${i} in | ||
64 | *.mage) continue ;; | ||
65 | esac | ||
66 | |||
67 | niro | 332 | # print categorie and pkgname |
68 | pkg="$(basename ${i%/*/*})/$(basename ${i%/*})" | ||
69 | niro | 304 | if [[ -z $(echo ${S_PACKAGES} | grep ${pkg}) ]] |
70 | then | ||
71 | S_PACKAGES="${S_PACKAGES} ${pkg}" | ||
72 | fi | ||
73 | done | ||
74 | |||
75 | # show packages | ||
76 | for pkg in ${S_PACKAGES} | ||
77 | do | ||
78 | echo "${pkg}" | ||
79 | done | ||
80 | |||
81 | exit 0 | ||
82 | fi | ||
83 | |||
84 | niro | 777 | if [[ ${SEARCH_ONLY_CAT} = true ]] |
85 | then | ||
86 | # no packages of SEARCH_CAT are installed | ||
87 | [[ ! -d ${MROOT}${INSTALLDB}/${SEARCH_CAT} ]] && exit 1 | ||
88 | |||
89 | for i in ${INSTALLDB}/${SEARCH_CAT}/* | ||
90 | do | ||
91 | # print categorie and pkgname | ||
92 | echo "$(basename ${i%/*})/$(basename ${i})" | ||
93 | done | ||
94 | |||
95 | exit 0 | ||
96 | fi | ||
97 | |||
98 | niro | 210 | if [[ ${GET_INVENTORY} = true ]] |
99 | then | ||
100 | niro | 228 | for package in $(find ${MROOT}${INSTALLDB} -mindepth 2 -maxdepth 2 -type d -printf "%h,%f\n" | sort) |
101 | niro | 210 | do |
102 | pcat="$(basename $(echo ${package} | cut -d, -f1))" | ||
103 | pname="$(echo ${package} | cut -d, -f2)" | ||
104 | niro | 1290 | if [[ -z ${invlist} ]] |
105 | niro | 210 | then |
106 | invlist="${pcat}/${pname}" | ||
107 | else | ||
108 | invlist="${invlist} | ||
109 | ${pcat}/${pname}" | ||
110 | fi | ||
111 | done | ||
112 | |||
113 | # now show the list | ||
114 | echo "${invlist}" | ||
115 | exit 0 | ||
116 | fi | ||
117 | |||
118 | niro | 228 | for i in ${MROOT}${INSTALLDB}/*/* |
119 | niro | 32 | do |
120 | INST_PNAME=no | ||
121 | INST_PVER=no | ||
122 | INST_PBUILD=no | ||
123 | |||
124 | x=$(basename ${i}) | ||
125 | PNAME=${x%-*-*} | ||
126 | niro | 1290 | PVER=$(echo ${x#${PNAME}-} | cut -d- -f1) |
127 | PBUILD=$(echo ${x#${PNAME}-} | cut -d- -f2) | ||
128 | niro | 32 | |
129 | if [[ ${PNAME} == ${S_PNAME} ]] | ||
130 | then | ||
131 | INST_PNAME=yes | ||
132 | |||
133 | niro | 1290 | if [[ -n ${S_PVER} ]] |
134 | niro | 32 | then |
135 | if [[ ${PVER} == ${S_PVER} ]] | ||
136 | then | ||
137 | INST_PVER=yes | ||
138 | fi | ||
139 | |||
140 | niro | 1290 | if [[ -n ${S_PBUILD} ]] |
141 | niro | 32 | then |
142 | if [[ ${PBUILD} == ${S_PBUILD} ]] | ||
143 | then | ||
144 | INST_PBUILD=yes | ||
145 | fi | ||
146 | fi | ||
147 | fi | ||
148 | |||
149 | # search for pname only | ||
150 | niro | 1290 | if [[ -n ${S_PNAME} ]] && [[ -z ${S_PVER} ]] && [[ -z ${S_PBUILD} ]] |
151 | niro | 32 | then |
152 | niro | 1290 | if [[ ${INST_PNAME} = yes ]] |
153 | niro | 32 | then |
154 | echo "${S_PNAME} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" | ||
155 | RETVAL=0 | ||
156 | break | ||
157 | fi | ||
158 | fi | ||
159 | |||
160 | # search for pname and pver | ||
161 | niro | 1290 | if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -z ${S_PBUILD} ]] |
162 | niro | 32 | then |
163 | niro | 1290 | if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] |
164 | niro | 32 | then |
165 | echo "${S_PNAME}-${S_PVER} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" | ||
166 | RETVAL=0 | ||
167 | break | ||
168 | fi | ||
169 | fi | ||
170 | |||
171 | # search for pname, pver and pbuild | ||
172 | niro | 1290 | if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -n ${S_PBUILD} ]] |
173 | niro | 32 | then |
174 | niro | 1290 | if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] && [[ ${INST_PBUILD} = yes ]] |
175 | niro | 32 | then |
176 | echo "${S_PNAME}-${S_PVER}-${S_PBUILD} is installed [ ${PNAME}-${PVER}-${PBUILD} ]" | ||
177 | RETVAL=0 | ||
178 | break | ||
179 | fi | ||
180 | fi | ||
181 | fi | ||
182 | done | ||
183 | |||
184 | exit ${RETVAL} |