Add new option to pkg_search
[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.
33#
83cc1f74 34# $DragonFly: src/usr.bin/pkg_search/pkg_search.sh,v 1.11 2008/09/04 10:33:50 matthias Exp $
674a427d 35
13a8f106
MD
36UNAME=`uname -s`
37VERSION=`uname -r | cut -d '.' -f 1,2`
a016e194
MS
38NO_INDEX=0
39PORTSDIR=/usr/pkgsrc
40PKGSUM=${PORTSDIR}/pkg_summary
e26b3c96 41PKGSRCBOX1=http://pkgbox.dragonflybsd.org/packages/${UNAME}-${VERSION}/stable/
f47197fe 42PKGSRCBOX2=http://pkgbox.dragonflybsd.org/packages/DragonFly-2.2/stable/
a016e194 43INDEXFILE=INDEX
13a8f106 44
b0ec74b2
MS
45# Download the pkg_summary file
46download_summary()
47{
48 echo "Fetching pkg_summary(5) file."
a016e194 49 FETCHPATH=${PKGSRCBOX1}/All/pkg_summary.bz2
a016e194
MS
50 fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
51 if [ $? -ne 0 ]; then
52 FETCHPATH=${PKGSRCBOX2}/All/pkg_summary.bz2
13a8f106 53 fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
13a8f106 54 fi
a016e194
MS
55 if [ $? -ne 0 ]; then
56 echo "Unable to fetch pkg_summary(5) file."
57 exit 1
13a8f106 58 fi
a016e194
MS
59 bunzip2 < ${PKGSUM}.bz2 > ${PKGSUM}
60 rm -f ${PKGSUM}.bz2
b0ec74b2 61}
a016e194
MS
62
63# Perform simple search in pkg_summary
64bin_simple_search()
65{
66 awk -F= -v name="$1" '{
67 if ($1 == "PKGNAME") {
a5da7f43 68 if (tolower($2) ~ name) {
a016e194
MS
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
79bin_ext_search()
80{
81 awk -F= -v name="$1" '{
82 if ($1 == "PKGNAME")
a5da7f43 83 if (tolower($2) ~ name) {
a016e194
MS
84 printf("\nName\t: %-50s\n", $2);
85 found = 1;
86 }
87 else found = 0;
4faa2bb3 88
a016e194
MS
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
99index_v_search()
100{
101 if [ ${KFLAG} -eq 0 ]; then
102 awk -F\| -v name="$1" '{
a5da7f43 103 if (tolower($1) ~ name) {
a016e194 104 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
4faa2bb3 105 "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
a016e194
MS
106 $4, $12, $9);
107 }
108 }' ${PORTSDIR}/${INDEXFILE}
109 else
110 awk -F\| -v name="$1" '{
a5da7f43
MS
111 if (tolower($1) ~ name || tolower($4) ~ name ||
112 tolower($12) ~ name) {
a016e194 113 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
4faa2bb3 114 "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
a016e194
MS
115 $4, $12, $9);
116 }
117 }' ${PORTSDIR}/${INDEXFILE}
118 fi
119}
120
121# Perform simple search in INDEX
122index_search()
123{
124 if [ ${KFLAG} -eq 0 ]; then
125 awk -F\| -v name="$1" '{
a5da7f43 126 if (tolower($1) ~ name) {
a016e194
MS
127 printf("%-20s\t%-25s\n", $1, $4);
128 }
129 }' ${PORTSDIR}/${INDEXFILE}
130 else
131 awk -F\| -v name="$1" '{
a5da7f43
MS
132 if (tolower($1) ~ name || tolower($4) ~ name ||
133 tolower($12) ~ name) {
a016e194
MS
134 printf("%-20s\t%-25s\n", $1, $4);
135 }
136 }' ${PORTSDIR}/${INDEXFILE}
137 fi
138}
674a427d 139
4faa2bb3
MS
140show_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
a016e194
MS
157usage()
158{
83cc1f74
MS
159 echo "usage: `basename $0` [-kv] package"
160 echo " `basename $0` -s package"
b0ec74b2 161 echo " `basename $0` -d"
4faa2bb3 162 exit 1
a016e194 163}
674a427d 164
b0ec74b2
MS
165if [ ! -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
170fi
171if [ -e ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
172 NO_INDEX=1
173fi
174
175args=`getopt dksv $*`
674a427d 176
4faa2bb3 177SFLAG=0
a016e194
MS
178KFLAG=0
179VFLAG=0
b0ec74b2 180DFLAG=0
7896f28d 181
a016e194
MS
182set -- $args
183for i; do
184 case "$i" in
b0ec74b2
MS
185 -d)
186 DFLAG=1; shift;;
a016e194
MS
187 -k)
188 KFLAG=1; shift;;
4faa2bb3
MS
189 -s)
190 SFLAG=1; shift;;
7896f28d 191 -v)
a016e194
MS
192 VFLAG=1; shift;;
193 --)
194 shift; break;;
195 esac
196done
13a8f106 197
b0ec74b2 198if [ ${DFLAG} -eq 0 -a -z ${1} ]; then
a016e194
MS
199 usage
200fi
201
b0ec74b2
MS
202if [ ${DFLAG} -eq 1 ]; then
203 download_summary
204 exit $?
205fi
206
a016e194
MS
207if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
208 bin_simple_search $1
209elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
210 bin_ext_search $1
4faa2bb3
MS
211elif [ ${SFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
212 show_description $1
a016e194
MS
213elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
214 index_search $1
215elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
216 index_v_search $1
217fi
674a427d
MD
218
219exit $?