Merge branch 'vendor/LIBARCHIVE'
[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 | cut -d '.' -f 1,2`
38 NO_INDEX=0
39 PORTSDIR=/usr/pkgsrc
40 PKGSUM=${PORTSDIR}/pkg_summary
41 PKGSRCBOX1=http://avalon.dragonflybsd.org/packages/${UNAME}-${VERSION}/stable/
42 PKGSRCBOX2=http://avalon.dragonflybsd.org/packages/DragonFly-2.2/stable/
43 INDEXFILE=INDEX
44
45 # Download the pkg_summary file
46 download_summary()
47 {
48         echo "Fetching pkg_summary(5) file."
49         FETCHPATH=${PKGSRCBOX1}/All/pkg_summary.bz2
50         fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
51         if [ $? -ne 0 ]; then
52                 FETCHPATH=${PKGSRCBOX2}/All/pkg_summary.bz2
53                 fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
54         fi
55         if [ $? -ne 0 ]; then
56                 echo "Unable to fetch pkg_summary(5) file."
57                 exit 1
58         fi
59         bunzip2 < ${PKGSUM}.bz2 > ${PKGSUM}
60         rm -f ${PKGSUM}.bz2
61 }
62
63 # Perform simple search in pkg_summary
64 bin_simple_search()
65 {
66         awk -F= -v name="$1" '{
67                 if ($1 == "PKGNAME") {
68                         if (tolower($2) ~ name) {
69                                 printf("%-20s\t", $2);
70                                 found = 1;
71                         }
72                         else found = 0;
73                 }
74                 if (found == 1 && $1 == "COMMENT") printf("%-25s\n", $2);
75         }' ${PKGSUM}
76 }
77
78 # Perform extended search in pkg_summary
79 bin_ext_search()
80 {
81         awk -F= -v name="$1" '{
82                 if ($1 == "PKGNAME")
83                         if (tolower($2) ~ name) {
84                                 printf("\nName\t: %-50s\n", $2);
85                                 found = 1;
86                         }
87                         else found = 0;
88
89                 if (found == 1 && $1 == "COMMENT")
90                         printf("Desc\t: %-50s\n", $2);
91                 if (found == 1 && $1 == "PKGPATH")
92                         printf("Path\t: %-50s\n", $2);
93                 if (found == 1 && $1 == "HOMEPAGE")
94                         printf("URL\t: %-50s\n", $2);
95         }' ${PKGSUM}
96 }
97
98 # Perform extended search in INDEX
99 index_v_search()
100 {
101         if [ ${KFLAG} -eq 0 ]; then
102                 awk -F\| -v name="$1" '{
103                         if (tolower($1) ~ name) {
104                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
105                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
106                                         $4, $12, $9);
107                         }
108                 }' ${PORTSDIR}/${INDEXFILE}
109         else
110                 awk -F\| -v name="$1" '{
111                         if (tolower($1) ~ name || tolower($4) ~ name ||
112                             tolower($12) ~ name) {
113                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
114                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
115                                         $4, $12, $9);
116                         }
117                 }' ${PORTSDIR}/${INDEXFILE}
118         fi
119 }
120
121 # Perform simple search in INDEX
122 index_search()
123 {
124         if [ ${KFLAG} -eq 0 ]; then
125                 awk -F\| -v name="$1" '{
126                         if (tolower($1) ~ name) {
127                                 printf("%-20s\t%-25s\n", $1, $4);
128                         }
129                 }' ${PORTSDIR}/${INDEXFILE}
130         else
131                 awk -F\| -v name="$1" '{
132                         if (tolower($1) ~ name || tolower($4) ~ name ||
133                             tolower($12) ~ name) {
134                                 printf("%-20s\t%-25s\n", $1, $4);
135                         }
136                 }' ${PORTSDIR}/${INDEXFILE}
137         fi
138 }
139
140 show_description()
141 {
142         PDESC=`awk -F\| -v name="$1" '{
143                 if (tolower($1) == name) {
144                         split($2, ppath, "/");
145                         printf("%s\n", $5);
146                 }
147         }' ${PORTSDIR}/${INDEXFILE}`
148         if [ -f "${PORTSDIR}/${PDESC}" ]; then
149                 cat "${PORTSDIR}/${PDESC}"
150         else
151                 echo "Unable to locate package $1.  Please provide the exact"
152                 echo "package name as given by pkg_search(1).  You need the"
153                 echo "complete pkgsrc(7) tree to perform this operation."
154         fi
155 }
156
157 usage()
158 {
159         echo "usage: `basename $0` [-kv] package"
160         echo "       `basename $0` -s package"
161         echo "       `basename $0` -d"
162         exit 1
163 }
164
165 if [ ! -f ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
166         echo "No pkgsrc(7) tree found."
167         mkdir -p ${PORTSDIR}
168         download_summary
169         NO_INDEX=1
170 fi
171 if [ -e ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
172         NO_INDEX=1
173 fi
174
175 args=`getopt dksv $*`
176
177 SFLAG=0
178 KFLAG=0
179 VFLAG=0
180 DFLAG=0
181
182 set -- $args
183 for i; do
184         case "$i" in
185         -d)
186                 DFLAG=1; shift;;
187         -k)
188                 KFLAG=1; shift;;
189         -s)
190                 SFLAG=1; shift;;
191         -v)
192                 VFLAG=1; shift;;
193         --)
194                 shift; break;;
195         esac
196 done
197
198 if [ ${DFLAG} -eq 0 -a -z ${1} ]; then
199         usage
200 fi
201
202 if [ ${DFLAG} -eq 1 ]; then
203         download_summary
204         exit $?
205 fi
206
207 if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
208         bin_simple_search $1
209 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
210         bin_ext_search $1
211 elif [ ${SFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
212         show_description $1
213 elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
214         index_search $1
215 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
216         index_v_search $1
217 fi
218
219 exit $?