Bump .Dd in pkg_radd.1 manpage and make pkg_search working again.
[dragonfly.git] / usr.bin / pkg_search / pkg_search.sh
CommitLineData
674a427d
MD
1#!/bin/sh
2#
b0ec74b2 3# Copyright (c) 2007-09 The DragonFly Project. All rights reserved.
9765f86e
MS
4#
5# This code is derived from software contributed to The DragonFly Project
a016e194 6# by Matthias Schmidt <matthias@dragonflybsd.org>, University of Marburg.
674a427d
MD
7#
8# All rights reserved.
9#
4faa2bb3 10# Redistribution and use in source and binary forms, with or without
674a427d
MD
11# modification, are permitted provided that the following conditions are met:
12#
4faa2bb3 13# - Redistributions of source code must retain the above copyright notice,
674a427d 14# this list of conditions and the following disclaimer.
4faa2bb3
MS
15# - Redistributions in binary form must reproduce the above copyright notice,
16# this list of conditions and the following disclaimer in the documentation
674a427d 17# and/or other materials provided with the distribution.
9765f86e
MS
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.
674a427d
MD
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.
674a427d 33
22904266 34set_binpkg_sites() {
03e57c5e 35 : ${BINPKG_BASE:=http://mirror-master.dragonflybsd.org/packages}
22904266
SS
36 : ${BINPKG_SITES:=$BINPKG_BASE/$cpuver/DragonFly-$osver/stable}
37}
38
13a8f106 39UNAME=`uname -s`
22904266
SS
40osver=`uname -r | awk -F - '{ print $1; }'`
41cpuver=`uname -p | awk -F - '{ print $1; }'`
1999fd99 42[ -f /etc/pkg_radd.conf ] && . /etc/pkg_radd.conf
22904266
SS
43set_binpkg_sites
44
a016e194
MS
45NO_INDEX=0
46PORTSDIR=/usr/pkgsrc
47PKGSUM=${PORTSDIR}/pkg_summary
a016e194 48INDEXFILE=INDEX
13a8f106 49
b0ec74b2
MS
50# Download the pkg_summary file
51download_summary()
52{
53 echo "Fetching pkg_summary(5) file."
22904266
SS
54 for tries in 1 2
55 do
56 FETCHPATH=${BINPKG_SITES}/All/pkg_summary.bz2
13a8f106 57 fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
22904266
SS
58 [ $? -eq 0 ] && break
59
60 # retry with default
61 unset BINPKG_BASE
62 unset BINPKG_SITES
63 set_binpkg_sites
64 done
a016e194
MS
65 if [ $? -ne 0 ]; then
66 echo "Unable to fetch pkg_summary(5) file."
67 exit 1
13a8f106 68 fi
a016e194
MS
69 bunzip2 < ${PKGSUM}.bz2 > ${PKGSUM}
70 rm -f ${PKGSUM}.bz2
b0ec74b2 71}
a016e194
MS
72
73# Perform simple search in pkg_summary
74bin_simple_search()
75{
76 awk -F= -v name="$1" '{
77 if ($1 == "PKGNAME") {
a5da7f43 78 if (tolower($2) ~ name) {
a016e194
MS
79 printf("%-20s\t", $2);
80 found = 1;
81 }
82 else found = 0;
83 }
84 if (found == 1 && $1 == "COMMENT") printf("%-25s\n", $2);
85 }' ${PKGSUM}
86}
87
88# Perform extended search in pkg_summary
89bin_ext_search()
90{
91 awk -F= -v name="$1" '{
92 if ($1 == "PKGNAME")
a5da7f43 93 if (tolower($2) ~ name) {
a016e194
MS
94 printf("\nName\t: %-50s\n", $2);
95 found = 1;
96 }
97 else found = 0;
4faa2bb3 98
a016e194
MS
99 if (found == 1 && $1 == "COMMENT")
100 printf("Desc\t: %-50s\n", $2);
101 if (found == 1 && $1 == "PKGPATH")
102 printf("Path\t: %-50s\n", $2);
103 if (found == 1 && $1 == "HOMEPAGE")
104 printf("URL\t: %-50s\n", $2);
105 }' ${PKGSUM}
106}
107
108# Perform extended search in INDEX
109index_v_search()
110{
111 if [ ${KFLAG} -eq 0 ]; then
112 awk -F\| -v name="$1" '{
a5da7f43 113 if (tolower($1) ~ name) {
a016e194 114 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
4faa2bb3 115 "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
a016e194
MS
116 $4, $12, $9);
117 }
118 }' ${PORTSDIR}/${INDEXFILE}
119 else
120 awk -F\| -v name="$1" '{
a5da7f43
MS
121 if (tolower($1) ~ name || tolower($4) ~ name ||
122 tolower($12) ~ name) {
a016e194 123 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
4faa2bb3 124 "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
a016e194
MS
125 $4, $12, $9);
126 }
127 }' ${PORTSDIR}/${INDEXFILE}
128 fi
129}
130
131# Perform simple search in INDEX
132index_search()
133{
134 if [ ${KFLAG} -eq 0 ]; then
135 awk -F\| -v name="$1" '{
a5da7f43 136 if (tolower($1) ~ name) {
a016e194
MS
137 printf("%-20s\t%-25s\n", $1, $4);
138 }
139 }' ${PORTSDIR}/${INDEXFILE}
140 else
141 awk -F\| -v name="$1" '{
a5da7f43
MS
142 if (tolower($1) ~ name || tolower($4) ~ name ||
143 tolower($12) ~ name) {
a016e194
MS
144 printf("%-20s\t%-25s\n", $1, $4);
145 }
146 }' ${PORTSDIR}/${INDEXFILE}
147 fi
148}
674a427d 149
4faa2bb3
MS
150show_description()
151{
152 PDESC=`awk -F\| -v name="$1" '{
153 if (tolower($1) == name) {
154 split($2, ppath, "/");
155 printf("%s\n", $5);
156 }
157 }' ${PORTSDIR}/${INDEXFILE}`
158 if [ -f "${PORTSDIR}/${PDESC}" ]; then
159 cat "${PORTSDIR}/${PDESC}"
160 else
161 echo "Unable to locate package $1. Please provide the exact"
162 echo "package name as given by pkg_search(1). You need the"
163 echo "complete pkgsrc(7) tree to perform this operation."
164 fi
165}
166
a016e194
MS
167usage()
168{
83cc1f74
MS
169 echo "usage: `basename $0` [-kv] package"
170 echo " `basename $0` -s package"
b0ec74b2 171 echo " `basename $0` -d"
4faa2bb3 172 exit 1
a016e194 173}
674a427d 174
b0ec74b2
MS
175if [ ! -f ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
176 echo "No pkgsrc(7) tree found."
177 mkdir -p ${PORTSDIR}
178 download_summary
179 NO_INDEX=1
180fi
181if [ -e ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
182 NO_INDEX=1
183fi
184
185args=`getopt dksv $*`
674a427d 186
4faa2bb3 187SFLAG=0
a016e194
MS
188KFLAG=0
189VFLAG=0
b0ec74b2 190DFLAG=0
7896f28d 191
a016e194
MS
192set -- $args
193for i; do
194 case "$i" in
b0ec74b2
MS
195 -d)
196 DFLAG=1; shift;;
a016e194
MS
197 -k)
198 KFLAG=1; shift;;
4faa2bb3
MS
199 -s)
200 SFLAG=1; shift;;
7896f28d 201 -v)
a016e194
MS
202 VFLAG=1; shift;;
203 --)
204 shift; break;;
205 esac
206done
13a8f106 207
b0ec74b2 208if [ ${DFLAG} -eq 0 -a -z ${1} ]; then
a016e194
MS
209 usage
210fi
211
b0ec74b2
MS
212if [ ${DFLAG} -eq 1 ]; then
213 download_summary
214 exit $?
215fi
216
a016e194
MS
217if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
218 bin_simple_search $1
219elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
220 bin_ext_search $1
4faa2bb3
MS
221elif [ ${SFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
222 show_description $1
a016e194
MS
223elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
224 index_search $1
225elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
226 index_v_search $1
227fi
674a427d
MD
228
229exit $?