Magellan Linux

Contents of /trunk/mage/usr/lib/mage/magequery.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1783 - (show annotations) (download) (as text)
Mon Mar 12 23:23:28 2012 UTC (12 years, 1 month ago) by niro
File MIME type: application/x-sh
File size: 4403 byte(s)
-always export C locale to fix utf-8 issues
1 #!/bin/bash
2
3 # query mage database for installed packages
4 # $Id$
5
6 # always export C locale to fix utf-8 issues
7 export LC_ALL=C
8
9 print_usage()
10 {
11 echo "$(basename $0 .sh) querys the mage database for installed packages."
12 echo
13 echo " -n NAME searches for name NAME"
14 echo " -v VER searches for version VER, needs -n"
15 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"
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
21 echo " Examples:"
22 echo
23 echo " $(basename $0 .sh) -n xorg -v 6.8.0 -b r1"
24 echo " searches for package xorg-6.8.0-r1"
25 echo
26 }
27
28 # default args:
29 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 # very basic getops
42 for i in $*
43 do
44 case $1 in
45 -n) shift; S_PNAME="$1" ;;
46 -v) shift; S_PVER="$1" ;;
47 -b) shift; S_PBUILD="$1" ;;
48 -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 -c) SEARCH_ONLY_CAT="true"; shift; SEARCH_CAT="$1" ;;
52 -h) print_usage; exit 0 ;;
53 -*) print_usage; exit 1 ;;
54 esac
55 shift
56 done
57
58 source /etc/mage.rc.global
59 source ${MAGERC}
60 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 ]]
115 then
116 for package in $(find ${MROOT}${INSTALLDB} -mindepth 2 -maxdepth 2 -type d -printf "%h,%f\n" | sort)
117 do
118 pcat="$(basename $(echo ${package} | cut -d, -f1))"
119 pname="$(echo ${package} | cut -d, -f2)"
120 if [[ -z ${invlist} ]]
121 then
122 invlist="${pcat}/${pname}"
123 else
124 invlist="${invlist}
125 ${pcat}/${pname}"
126 fi
127 done
128
129 # now show the list
130 echo "${invlist}"
131 exit 0
132 fi
133
134 for i in ${MROOT}${INSTALLDB}/*/*
135 do
136 INST_PNAME=no
137 INST_PVER=no
138 INST_PBUILD=no
139
140 x=$(basename ${i})
141 PNAME=${x%-*-*}
142 PVER=$(echo ${x#${PNAME}-} | cut -d- -f1)
143 PBUILD=$(echo ${x#${PNAME}-} | cut -d- -f2)
144
145 if [[ ${PNAME} == ${S_PNAME} ]]
146 then
147 INST_PNAME=yes
148
149 if [[ -n ${S_PVER} ]]
150 then
151 if [[ ${PVER} == ${S_PVER} ]]
152 then
153 INST_PVER=yes
154 fi
155
156 if [[ -n ${S_PBUILD} ]]
157 then
158 if [[ ${PBUILD} == ${S_PBUILD} ]]
159 then
160 INST_PBUILD=yes
161 fi
162 fi
163 fi
164
165 # search for pname only
166 if [[ -n ${S_PNAME} ]] && [[ -z ${S_PVER} ]] && [[ -z ${S_PBUILD} ]]
167 then
168 if [[ ${INST_PNAME} = yes ]]
169 then
170 echo "${S_PNAME} is installed [ ${PNAME}-${PVER}-${PBUILD} ]"
171 RETVAL=0
172 break
173 fi
174 fi
175
176 # search for pname and pver
177 if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -z ${S_PBUILD} ]]
178 then
179 if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]]
180 then
181 echo "${S_PNAME}-${S_PVER} is installed [ ${PNAME}-${PVER}-${PBUILD} ]"
182 RETVAL=0
183 break
184 fi
185 fi
186
187 # search for pname, pver and pbuild
188 if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -n ${S_PBUILD} ]]
189 then
190 if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] && [[ ${INST_PBUILD} = yes ]]
191 then
192 echo "${S_PNAME}-${S_PVER}-${S_PBUILD} is installed [ ${PNAME}-${PVER}-${PBUILD} ]"
193 RETVAL=0
194 break
195 fi
196 fi
197 fi
198 done
199
200 exit ${RETVAL}