Magellan Linux

Annotation of /branches/mage-next/src/magequery.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2567 - (hide annotations) (download)
Wed Jan 29 12:21:17 2014 UTC (10 years, 3 months ago) by niro
File size: 4403 byte(s)
-renamed magequery.sh > magequery.in
1 niro 32 #!/bin/bash
2    
3 niro 1290 # query mage database for installed packages
4     # $Id$
5 niro 32
6 niro 1783 # always export C locale to fix utf-8 issues
7     export LC_ALL=C
8    
9 niro 32 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 niro 777 echo " -c CAT shows all packages of given categorie"
17 niro 210 echo " -i shows an inventory of all installed packages"
18 niro 1503 echo " -f PATH searches packages which own given file"
19     echo " -e PATH searches the package which owns the exact given filename"
20 niro 32 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 niro 210 # default args:
29     GET_INVENTORY=false
30 niro 304 SEARCH_ONLY_PATH=false
31 niro 777 SEARCH_ONLY_CAT=false
32 niro 1503 SEARCH_EXACT_FILENAME=false
33 niro 210
34 niro 1290 # 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 niro 1206 do
44 niro 1290 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 niro 1503 -e) SEARCH_ONLY_PATH="true"; SEARCH_EXACT_FILENAME="true"; shift; SEARCH_PATH="$1" ;;
51 niro 1290 -c) SEARCH_ONLY_CAT="true"; shift; SEARCH_CAT="$1" ;;
52     -h) print_usage; exit 0 ;;
53     -*) print_usage; exit 1 ;;
54 niro 32 esac
55 niro 1290 shift
56 niro 32 done
57    
58 niro 304 source /etc/mage.rc.global
59 niro 1546 source ${MAGERC}
60 niro 32 RETVAL=1
61    
62 niro 304 if [[ ${SEARCH_ONLY_PATH} = true ]]
63     then
64     unset S_PACKAGES
65     unset i pkg
66 niro 1503 if [[ ${SEARCH_EXACT_FILENAME} = true ]]
67     then
68 niro 1509 # fix ++, which gets interpreted as a regex
69     SEARCH_PATH="${SEARCH_PATH//+/\\+}"
70 niro 1540 # fix [, which gets interpreted as a regex
71     SEARCH_PATH="${SEARCH_PATH//[/\\[}"
72 niro 1507 S_CANDIDATES=$(egrep -rl "^${SEARCH_PATH}§" ${INSTALLDB})
73 niro 1503 else
74     S_CANDIDATES=$(fgrep -rl "${SEARCH_PATH}" ${INSTALLDB})
75     fi
76     for i in ${S_CANDIDATES}
77 niro 304 do
78 niro 1288 # ignore magefiles!
79     case ${i} in
80     *.mage) continue ;;
81     esac
82    
83 niro 332 # print categorie and pkgname
84     pkg="$(basename ${i%/*/*})/$(basename ${i%/*})"
85 niro 304 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 niro 777 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 niro 210 if [[ ${GET_INVENTORY} = true ]]
115     then
116 niro 228 for package in $(find ${MROOT}${INSTALLDB} -mindepth 2 -maxdepth 2 -type d -printf "%h,%f\n" | sort)
117 niro 210 do
118     pcat="$(basename $(echo ${package} | cut -d, -f1))"
119     pname="$(echo ${package} | cut -d, -f2)"
120 niro 1290 if [[ -z ${invlist} ]]
121 niro 210 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 niro 228 for i in ${MROOT}${INSTALLDB}/*/*
135 niro 32 do
136     INST_PNAME=no
137     INST_PVER=no
138     INST_PBUILD=no
139    
140     x=$(basename ${i})
141     PNAME=${x%-*-*}
142 niro 1290 PVER=$(echo ${x#${PNAME}-} | cut -d- -f1)
143     PBUILD=$(echo ${x#${PNAME}-} | cut -d- -f2)
144 niro 32
145     if [[ ${PNAME} == ${S_PNAME} ]]
146     then
147     INST_PNAME=yes
148    
149 niro 1290 if [[ -n ${S_PVER} ]]
150 niro 32 then
151     if [[ ${PVER} == ${S_PVER} ]]
152     then
153     INST_PVER=yes
154     fi
155    
156 niro 1290 if [[ -n ${S_PBUILD} ]]
157 niro 32 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 niro 1290 if [[ -n ${S_PNAME} ]] && [[ -z ${S_PVER} ]] && [[ -z ${S_PBUILD} ]]
167 niro 32 then
168 niro 1290 if [[ ${INST_PNAME} = yes ]]
169 niro 32 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 niro 1290 if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -z ${S_PBUILD} ]]
178 niro 32 then
179 niro 1290 if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]]
180 niro 32 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 niro 1290 if [[ -n ${S_PNAME} ]] && [[ -n ${S_PVER} ]] && [[ -n ${S_PBUILD} ]]
189 niro 32 then
190 niro 1290 if [[ ${INST_PNAME} = yes ]] && [[ ${INST_PVER} = yes ]] && [[ ${INST_PBUILD} = yes ]]
191 niro 32 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}