o Change my personal copyright to the DragonFly copyright
[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 <schmidtm@mathematik.uni-marburg.de>, University of
7 # Marburg.
8 #
9 # All rights reserved.
10 #
11 # Redistribution and use in source and binary forms, with or without 
12 # modification, are permitted provided that the following conditions are met:
13 #
14 # - Redistributions of source code must retain the above copyright notice, 
15 #   this list of conditions and the following disclaimer.
16 # - Redistributions in binary form must reproduce the above copyright notice, 
17 #   this list of conditions and the following disclaimer in the documentation 
18 #   and/or other materials provided with the distribution.
19 # - Neither the name of The DragonFly Project nor the names of its
20 #   contributors may be used to endorse or promote products derived
21 #   from this software without specific, prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35 # $DragonFly: src/usr.bin/pkg_search/pkg_search.sh,v 1.4 2007/12/27 11:52:10 matthias Exp $
36
37 UNAME=`uname -s`
38 VERSION=`uname -r | cut -d '.' -f 1,2`
39 SIMPLE=0
40
41 if [ $UNAME = "DragonFly" ]; then
42         PORTSDIR=/usr/pkgsrc
43         PKGSUM=${PORTSDIR}/pkg_summary
44         PKGSRCBOX1=http://pkgbox.dragonflybsd.org/packages/${UNAME}-${VERSION}/i386/
45         PKGSRCBOX2=http://pkgbox.dragonflybsd.org/packages/DragonFly-1.10.1/i386/
46         INDEXFILE=INDEX
47         if [ ! -f ${PKGSUM} ]; then
48                 echo "No pkgsrc(7) tree found.  Fetching pkg_summary(5) file."
49                 FETCHPATH=${PKGSRCBOX1}/All/pkg_summary.bz2
50                 mkdir -p ${PORTSDIR}
51                 fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
52                 if [ $? -ne 0 ]; then
53                         FETCHPATH=${PKGSRCBOX2}/All/pkg_summary.bz2
54                         fetch -o ${PKGSUM}.bz2 ${FETCHPATH}
55                 fi
56                 if [ $? -ne 0 ]; then
57                         echo "Unable to fetch pkg_summary"
58                         exit 1
59                 fi
60                 bunzip2 < ${PKGSUM}.bz2 > ${PKGSUM}
61                 rm -f ${PKGSUM}.bz2
62                 SIMPLE=1
63         fi
64         if [ -e ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
65                 SIMPLE=1
66         fi
67 fi
68
69 if [ -z $1 ]; then 
70         echo "usage: $0 [-k | -v] package"
71         exit 1  
72 fi
73
74
75 case "$1" in
76         -k)
77                 if [ ${SIMPLE} -eq 1 ]; then
78                         grep "PKGNAME=$2" ${PKGSUM} | cut -d '=' -f 2
79                         exit 1
80                 fi
81                 awk -F\| -v name="$2" \
82             '{\
83                     if ($1 ~ name || $4 ~ name || $10 ~ name) { \
84                             split($2, a, "/"); \
85                             printf("%-20s\t%-25s\n", $1, $4); \
86                     }
87             }' ${PORTSDIR}/${INDEXFILE}
88
89         ;;
90         -v)
91                 if [ ${SIMPLE} -eq 1 ]; then
92                         grep "PKGNAME=$2" ${PKGSUM} | cut -d '=' -f 2
93                         exit 1
94                 fi
95                 awk -F\| -v name="$2" \
96             '{\
97                     if ($1 ~ name) { \
98                             split($2, a, "/"); \
99                             printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2, $4, $10, $9); \
100                     }
101             }' ${PORTSDIR}/${INDEXFILE}
102         ;;
103         *)
104                 if [ ${SIMPLE} -eq 1 ]; then
105                         grep "PKGNAME=$1" ${PKGSUM} | cut -d '=' -f 2
106                         exit 1
107                 fi
108
109                 awk -F\| -v name="$1" \
110             '{\
111                     if ($1 ~ name) { \
112                             split($2, a, "/"); \
113                             printf("%-20s\t%-25s\n", $1, $4); \
114                     }
115             }' ${PORTSDIR}/${INDEXFILE}
116         ;;
117 esac
118
119 exit $?