b24f07e3943be8ef4d29b7e5124c90b8dd12bee7
[dragonfly.git] / usr.bin / pkg_search / pkg_search.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 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.6 2008/02/10 11:01:46 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://pkgbox.dragonflybsd.org/packages/${UNAME}-${VERSION}/i386/
42 PKGSRCBOX2=http://pkgbox.dragonflybsd.org/packages/DragonFly-1.10.1/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                                 split($2, a, "/");
108                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
109                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2, 
110                                         $4, $12, $9);
111                         }
112                 }' ${PORTSDIR}/${INDEXFILE}
113         else
114                 awk -F\| -v name="$1" '{
115                         if (tolower($1) ~ name || tolower($4) ~ name ||
116                             tolower($12) ~ name) {
117                                 split($2, a, "/");
118                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
119                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2, 
120                                         $4, $12, $9);
121                         }
122                 }' ${PORTSDIR}/${INDEXFILE}
123         fi
124 }
125
126 # Perform simple search in INDEX
127 index_search()
128 {
129         if [ ${KFLAG} -eq 0 ]; then
130                 awk -F\| -v name="$1" '{
131                         if (tolower($1) ~ name) {
132                                 split($2, a, "/");
133                                 printf("%-20s\t%-25s\n", $1, $4);
134                         }
135                 }' ${PORTSDIR}/${INDEXFILE}
136         else
137                 awk -F\| -v name="$1" '{
138                         if (tolower($1) ~ name || tolower($4) ~ name ||
139                             tolower($12) ~ name) {
140                                 split($2, a, "/");
141                                 printf("%-20s\t%-25s\n", $1, $4);
142                         }
143                 }' ${PORTSDIR}/${INDEXFILE}
144         fi
145 }
146
147 usage()
148 {
149         echo "usage: $0 [-k | -v] package"
150         exit 1  
151 }
152
153 args=`getopt kv $*`
154
155 KFLAG=0
156 VFLAG=0
157
158 set -- $args
159 for i; do
160         case "$i" in
161         -k)
162                 KFLAG=1; shift;;
163         -v)
164                 VFLAG=1; shift;;
165         --)
166                 shift; break;;
167         esac
168 done
169
170 if [ -z ${1} ]; then
171         usage
172 fi
173
174 if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
175         bin_simple_search $1
176 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
177         bin_ext_search $1
178 elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
179         index_search $1
180 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
181         index_v_search $1
182 fi
183
184 exit $?