Merge from vendor branch TNFTP:
[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.5 2008/01/03 16:42:43 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 ($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 ($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 ($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 ($1 ~ name || $4 ~ name || $12 ~ name) {
116                                 split($2, a, "/");
117                                 printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
118                                         "\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2, 
119                                         $4, $12, $9);
120                         }
121                 }' ${PORTSDIR}/${INDEXFILE}
122         fi
123 }
124
125 # Perform simple search in INDEX
126 index_search()
127 {
128         if [ ${KFLAG} -eq 0 ]; then
129                 awk -F\| -v name="$1" '{
130                         if ($1 ~ name) {
131                                 split($2, a, "/");
132                                 printf("%-20s\t%-25s\n", $1, $4);
133                         }
134                 }' ${PORTSDIR}/${INDEXFILE}
135         else
136                 awk -F\| -v name="$1" '{
137                         if ($1 ~ name || $4 ~ name || $12 ~ name) {
138                                 split($2, a, "/");
139                                 printf("%-20s\t%-25s\n", $1, $4);
140                         }
141                 }' ${PORTSDIR}/${INDEXFILE}
142         fi
143 }
144
145 usage()
146 {
147         echo "usage: $0 [-k | -v] package"
148         exit 1  
149 }
150
151 args=`getopt kv $*`
152
153 KFLAG=0
154 VFLAG=0
155
156 set -- $args
157 for i; do
158         case "$i" in
159         -k)
160                 KFLAG=1; shift;;
161         -v)
162                 VFLAG=1; shift;;
163         --)
164                 shift; break;;
165         esac
166 done
167
168 if [ -z ${1} ]; then
169         usage
170 fi
171
172 if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
173         bin_simple_search $1
174 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
175         bin_ext_search $1
176 elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
177         index_search $1
178 elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
179         index_v_search $1
180 fi
181
182 exit $?