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