snippet: view plain - save this
1 #!/bin/bash
2 #
3 # pkgsearch
4 #
5 # Copyright (c) 2006 by Benoit Chesneau <benoit@bchesneau.info>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 # USA.
21 #
22 # AUTHORS :
23 # - Maintainer : Benoit Chesneau <benoitc@archlinuxfr.org>
24 # - Contributor : slubman <slubman@archlinuxfr.org>
25 # CHANGELOG :
26 # - version 0.6
27 # cosmethics changes
28 # internal rewrite
29 # - version 0.5 (2007-02-28 15:19) :
30 # search on multiple words (slubman)
31 # search in description is case insensitive (slubman)
32 # - version 0.4 (2007-02-28 13:14) :
33 # add possibility to search in repository (slubman)
34 # add possibility to exclude repository in search (slubman)
35 # - version 0.3 (2007-02-28) :
36 # add possibility to display only installed packages (slubman)
37 # add possibility to display only not installed packages (benoitc)
38 # - version 0.1
39 # initial release
40 #
41
42 PACMAN_SOURCES="/var/lib/pacman"
43 PWD=`pwd`
44
45 cd $PACMAN_SOURCES
46
47 myver="0.6"
48
49
50 NAME_ONLY=0
51 USE_COLOR="y"
52 NOT_INSTALLED=0
53 ONLY_INSTALLED=0
54 IN="all packages"
55 SKIP_REPO=()
56 IN_REPO=()
57 search_string=()
58
59 installed() {
60 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
61 echo -e "\033[1;35m$1\033[1;0m \033[1;35m(*)\033[1;0m" >&2
62 else
63 echo "$1 (*)" >&2
64 fi
65 let nb=nb+1
66 }
67
68 not_installed() {
69 echo "$1" >&2
70 let nb=nb+1
71 }
72
73 msg() {
74 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
75 echo -e "\033[1;32m==>\033[1;0m \033[1;1m$1\033[1;0m" >&2
76 else
77 echo "==> $1" >&2
78 fi
79 }
80
81 error() {
82 if [ "$USE_COLOR" = "Y" -o "$USE_COLOR" = "y" ]; then
83 echo -e "\033[1;31m==> ERROR:\033[1;0m \033[1;1m$1\033[1;0m" >&2
84 else
85 echo "==> ERROR: $1" >&2
86 fi
87 }
88
89 usage() {
90 echo "pkgsearch $myver"
91 echo "usage : $0 [-n] <searchstring>"
92 echo
93 echo "pkgsearch list all packages that contain your"
94 echo "string and display installed packages"
95 echo
96 echo "Options"
97 echo " -n, --name Search on pkgname only"
98 echo " -m, --nocolor Disable colorized output messages"
99 echo " -i, --installed Display only installed packages"
100 echo " -sr, --skip-repo Exclude repo in search"
101 echo " -ir, --ni-repo Search only in repo"
102 echo " -s, --skip Display only not installed packages"
103 echo " -h, --help Display usage"
104 }
105
106 if [ $# -lt 1 ]; then
107 usage
108 exit 1
109 fi
110
111 while [ "$#" -ne "0" ]; do
112 case $1 in
113 --installed|-i)
114 ONLY_INSTALLED=1
115 IN="only installed packages"
116 ;;
117 --help|-h)
118 usage
119 exit 0
120 ;;
121 --name|-n)
122 NAME_ONLY=1
123 ;;
124 --nocolor|-m)
125 USE_COLOR=n
126 ;;
127 --skip|-s)
128 NOT_INSTALLED=1
129 IN="only not installed packages"
130 ;;
131 --skip-repo|-sr)
132 SKIP_REPO[${#SKIP_REPO[*]}]=$2
133 shift
134 ;;
135 --in-repo|-ir)
136 IN_REPO[${#IN_REPO[*]}]=$2
137 shift
138 ;;
139 *)
140 search_string[${#search_string[*]}]=$1
141 ;;
142 esac
143 shift
144 done
145
146 if [ "$NOT_INSTALLED" = "1" -a "$ONLY_INSTALLED" = "1" ]; then
147 error "Error : Skip or show installed packages, but not both..."
148 exit 0
149 fi
150
151 nb=0
152
153 msg "Looking for \""${search_string[*]:-nothing}\""\n\tin ${IN}\n\tof ${IN_REPO[*]:-all repos}\n\texcluding ${SKIP_REPO[*]:-no repo}..."
154 search_token=${search_string[0]}
155 unset search_string[0]
156 for word in ${search_string[*]}; do
157 search_token="$search_token|$word"
158 done
159 search_token="($search_token)"
160
161 for repo in `find * -maxdepth 0 -type d`; do
162 if ! [ $repo = 'local' ]; then
163 if [ "$(echo ${SKIP_REPO[@]} | grep $repo)" ]; then
164 continue
165 fi
166 if [ "$(echo ${IN_REPO[@]} | grep -v $repo)" ]; then
167 continue
168 fi
169 cd $repo
170 for pkg in `/bin/ls`; do
171 if [ -f $pkg/desc ]; then
172 if [ "$NAME_ONLY" = "1" ]; then
173 if [ "$(echo $pkg | grep -E $search_token)" ]; then
174 NAME=`grep -A 1 NAME $pkg/desc | tail -1`
175 VERSION=`grep -A 1 VERSION $pkg/desc | tail -1`
176 if [ -d $PACMAN_SOURCES/local/$pkg ]; then
177 if [ "$NOT_INSTALLED" = "0" ]; then
178 installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
179 fi
180 else
181 if [ "$ONLY_INSTALLED" = "0" ]; then
182 not_installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
183 fi
184 fi
185 fi
186 else
187 if [ "$(grep -E -i $search_token $pkg/desc)" ]; then
188 NAME=`grep -A 1 NAME $pkg/desc | tail -1`
189 VERSION=`grep -A 1 VERSION $pkg/desc | tail -1`
190 if [ -d $PACMAN_SOURCES/local/$pkg ]; then
191 if [ "$NOT_INSTALLED" = "0" ]; then
192 installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
193 fi
194 else
195 if [ "$ONLY_INSTALLED" = "0" ]; then
196 not_installed "${nb} - [${repo}] - ${NAME} - ${VERSION}"
197 fi
198 fi
199 fi
200 fi
201 fi
202 done
203 cd ..
204 fi
205 done
206
207 echo
208 echo "$nb packages found.."
209
210 cd $PWD

0 comments