a065938d8e6be00aa885f40d1cce1f6178f2431a
[dragonfly.git] / usr.bin / pkg_search / pkg_search.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007-09 The DragonFly Project.  All rights reserved.
4 #
5 # This code is derived from software contributed to The DragonFly Project
6 # by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg.
7 #
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are met:
12 #
13 # - Redistributions of source code must retain the above copyright notice,
14 #   this list of conditions and the following disclaimer.
15 # - Redistributions in binary form must reproduce the above copyright notice,
16 #   this list of conditions and the following disclaimer in the documentation
17 #   and/or other materials provided with the distribution.
18 # - Neither the name of The DragonFly Project nor the names of its
19 #   contributors may be used to endorse or promote products derived
20 #   from this software without specific, prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #
34 # $DragonFly: src/usr.bin/pkg_search/pkg_search.sh,v 1.11 2008/09/04 10:33:50 matthias Exp $
35
36 UNAME=`uname -s`
37 VERSION=`uname -r | awk -F - '{ print $1; }'`
38 NO_INDEX=0
39 PORTSDIR=/usr/pkgsrc
40 PKGSUM=${PORTSDIR}/pkg_summary
41 if [ -z "$BINPKG_SITES" ]; then
42         BINPKG_SITES=http://avalon.dragonflybsd.org/packages/${UNAME}-${VERSION}/stable/
43         [ -f /etc/settings.conf ] && . /etc/settings.conf
44 fi
45 PKGSRCBOX1=$BINPKG_SITES
46 PKGSRCBOX2=http://avalon.dragonflybsd.org/packages/DragonFly-2.2.0/stable/
47 INDEXFILE=INDEX
48
49 # Download the pkg_summary file
50 download_summary()
51 {
52         echo "Fetching pkg_summary(5) file."
53         FETCHPATH=${PKGSRCBOX1}/All/pkg_summary.bz2
54         fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
55         if [ $? -ne 0 ]; then
56                 FETCHPATH=${PKGSRCBOX2}/All/pkg_summary.bz2
57                 fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
58         fi
59         if [ $? -ne 0 ]; then
60                 echo "Unable to fetch pkg_summary(5) file."
61                 exit 1
62         fi
63         bunzip2 < ${PKGSUM}.bz2 > ${PKGSUM}
64         rm -f ${PKGSUM}.bz2
65 }
66
67 # Perform simple search in pkg_summary
68 bin_simple_search()
69 {
70         awk -F= -v name="$1" '{
71                 if ($1 == "PKGNAME") {
72                         if (tolower($2) ~ name) {
73                                 printf("%-20s\t", $2);
74                                 found = 1;
75                         }
76                         else found = 0;
77                 }
78                 if (found == 1 && $1 == "COMMENT") printf("%-25s\n", $2);
79         }' ${PKGSUM}
80 }
81
82 # Perform extended search in pkg_summary
83 bin_ext_search()
84 {
85         awk -F= -v name="$1" '{
86                 if ($1 == "PKGNAME")
87                         if (tolower($2) ~ name) {
88                                 printf("\nName\t: %-50s\n", $2);
89                                 found = 1;
90                         }
91                         else found = 0;
92
93                 if (found == 1 && $1 == "COMMENT")
94                         printf("Desc\t: %-50s\n", $2);
95                 if (found == 1 && $1 == "PKGPATH")
96                         printf("Path\t: %-50s\n", $2);
97                 if (found == 1 && $1 == "HOMEPAGE")
98                         printf("URL\t: %-50s\n", $2);
99         }' ${PKGSUM}
100 }
101
102 # Perform extended search in INDEX
103 index_v_search()
104 {
105         if [ ${KFLAG} -eq 0 ]; then
106                 awk -F\| -v name="$1" '{
107                         if (tolower($1) ~ name) {
108                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
109                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
110                                         $4, $12, $9);
111                         }
112                 }' ${PORTSDIR}/${INDEXFILE}
113         else
114                 awk -F\| -v name="$1" '{
115                         if (tolower($1) ~ name || tolower($4) ~ name ||
116                             tolower($12) ~ name) {
117                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
118                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
119                                         $4, $12, $9);
120                         }
121                 }' ${PORTSDIR}/${INDEXFILE}
122         fi
123 }
124
125 # Perform simple search in INDEX
126 index_search()
127 {
128         if [ ${KFLAG} -eq 0 ]; then
129                 awk -F\| -v name="$1" '{
130                         if (tolower($1) ~ name) {
131                                 printf("%-20s\t%-25s\n", $1, $4);
132                         }
133                 }' ${PORTSDIR}/${INDEXFILE}
134         else
135                 awk -F\| -v name="$1" '{
136                         if (tolower($1) ~ name || tolower($4) ~ name ||
137                             tolower($12) ~ name) {
138                                 printf("%-20s\t%-25s\n", $1, $4);
139                         }
140                 }' ${PORTSDIR}/${INDEXFILE}
141         fi
142 }
143
144 show_description()
145 {
146         PDESC=`awk -F\| -v name="$1" '{
147                 if (tolower($1) == name) {
148                         split($2, ppath, "/");
149                         printf("%s\n", $5);
150                 }
151         }' ${PORTSDIR}/${INDEXFILE}`
152         if [ -f "${PORTSDIR}/${PDESC}" ]; then
153                 cat "${PORTSDIR}/${PDESC}"
154         else
155                 echo "Unable to locate package $1.  Please provide the exact"
156                 echo "package name as given by pkg_search(1).  You need the"
157                 echo "complete pkgsrc(7) tree to perform this operation."
158         fi
159 }
160
161 usage()
162 {
163         echo "usage: `basename $0` [-kv] package"
164         echo "       `basename $0` -s package"
165         echo "       `basename $0` -d"
166         exit 1
167 }
168
169 if [ ! -f ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
170         echo "No pkgsrc(7) tree found."
171         mkdir -p ${PORTSDIR}
172         download_summary
173         NO_INDEX=1
174 fi
175 if [ -e ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
176         NO_INDEX=1
177 fi
178
179 args=`getopt dksv $*`
180
181 SFLAG=0
182 KFLAG=0
183 VFLAG=0
184 DFLAG=0
185
186 set -- $args
187 for i; do
188         case "$i" in
189         -d)
190                 DFLAG=1; shift;;
191         -k)
192                 KFLAG=1; shift;;
193         -s)
194                 SFLAG=1; shift;;
195         -v)
196                 VFLAG=1; shift;;
197         --)
198                 shift; break;;
199         esac
200 done
201
202 if [ ${DFLAG} -eq 0 -a -z ${1} ]; then
203         usage
204 fi
205
206 if [ ${DFLAG} -eq 1 ]; then
207         download_summary
208         exit $?
209 fi
210
211 if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
212         bin_simple_search $1
213 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
214         bin_ext_search $1
215 elif [ ${SFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
216         show_description $1
217 elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
218         index_search $1
219 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
220         index_v_search $1
221 fi
222
223 exit $?