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