#!/bin/sh
#
# Copyright (c) 2006-08 Matthias Schmidt <matthias@dragonflybsd.org>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

UNAME=`uname -s`
VERSION=`uname -r | cut -d '.' -f 1,2`
ARCH=`uname -m`
NO_INDEX=0
PKGSUM=${PORTSDIR}/pkg_summary
PKGSRCBOX1=http://ftp.netbsd.org/pub/packages/${UNAME}-${VERSION}/${ARCH}/
PKGSRCBOX2=http://ftp.netbsd.org/pub/packages/NetBSD-4.0/${ARCH}/
MASTER_SITE_INDEX=http://www.FreeBSD.org/ports/
INDEXFILE=INDEX

if [ ${UNAME} = "FreeBSD" ]; then
	PORTSDIR=/usr/ports
	id=`uname -r | cut -d '.' -f 1`
	INDEXFILE=INDEX-$id
	if [ ! -d ${PORTSDIR} ]; then
		echo "Create ports directory ..."
		mkdir -p ${PORTSDIR}
	fi

	if [ ! -e ${PORTSDIR}/${INDEXFILE} ]; then
		echo "INDEX not found.  Fetch new INDEX file ..."
		fetch -o ${PORTSDIR}/${INDEXFILE}.bz2 \
		${MASTER_SITE_INDEX}${INDEXFILE}.bz2
		bunzip2 < ${PORTSDIR}/${INDEXFILE}.bz2 > ${PORTSDIR}/${INDEXFILE} && \
		chmod a+r ${PORTSDIR}/${INDEXFILE}
	fi
elif [ ${UNAME} = "NetBSD" ]; then
	PORTSDIR=/usr/pkgsrc
	if [ ! -f ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
		echo "No pkgsrc(7) tree found.  Fetching pkg_summary(5) file."
		FETCHPATH=${PKGSRCBOX1}/All/pkg_summary.gz
		mkdir -p ${PORTSDIR}
		fetch -o ${PKGSUM}.gz ${FETCHPATH}
		if [ $? -ne 0 ]; then
			FETCHPATH=${PKGSRCBOX2}/All/pkg_summary.gz
			fetch -o ${PKGSUM}.gz ${FETCHPATH}
		fi
		if [ $? -ne 0 ]; then
			echo "Unable to fetch pkg_summary(5) file."
			exit 1
		fi
		gunzip < ${PKGSUM}.gz > ${PKGSUM}
		rm -f ${PKGSUM}.gz
		NO_INDEX=1
	fi
	if [ -e ${PKGSUM} -a ! -e ${PORTSDIR}/${INDEXFILE} ]; then
		NO_INDEX=1
	fi
else
	echo "Sorry.  You OS ${UNAME} is unsupported.  A version for DragonFly"
	echo "is included in the DragonFly base."
	exit 1
fi

# Perform simple search in pkg_summary
bin_simple_search()
{
	awk -F= -v name="$1" '{
		if ($1 == "PKGNAME") {
			if ($2 ~ name) {
				printf("%-20s\t", $2);
				found = 1;
			}
			else found = 0;
		}
		if (found == 1 && $1 == "COMMENT") printf("%-25s\n", $2);
	}' ${PKGSUM}
}

# Perform extended search in pkg_summary
bin_ext_search()
{
	awk -F= -v name="$1" '{
		if ($1 == "PKGNAME")
			if ($2 ~ name) {
				printf("\nName\t: %-50s\n", $2);
				found = 1;
			}
			else found = 0;
		
		if (found == 1 && $1 == "COMMENT")
			printf("Desc\t: %-50s\n", $2);
		if (found == 1 && $1 == "PKGPATH")
			printf("Path\t: %-50s\n", $2);
		if (found == 1 && $1 == "HOMEPAGE")
			printf("URL\t: %-50s\n", $2);
	}' ${PKGSUM}
}

# Perform extended search in INDEX
index_v_search()
{
	if [ ${KFLAG} -eq 0 ]; then
		awk -F\| -v name="$1" '{
			if ($1 ~ name) {
				split($2, a, "/");
				printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
					"\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
					$4, $12, $9);
			}
		}' ${PORTSDIR}/${INDEXFILE}
	else
		awk -F\| -v name="$1" '{
			if ($1 ~ name || $4 ~ name || $12 ~ name) {
				split($2, a, "/");
				printf("Name\t: %s-50\nDir\t: %-50s\nDesc\t: %-50s"\
					"\nURL\t: %-50s\nDeps\t: %s\n\n", $1, $2,
					$4, $12, $9);
			}
		}' ${PORTSDIR}/${INDEXFILE}
	fi
}

# Perform simple -r search in INDEX
index_r_search()
{
	if [ ${KFLAG} -eq 0 ]; then
		awk -F\| -v name="$1" '{
			if ($1 ~ name) {
				split($2, a, "/");
				printf("%-20s\t%-25s\n", $1, $2);
			}
		}' ${PORTSDIR}/${INDEXFILE}
	else
		awk -F\| -v name="$1" '{
			if ($1 ~ name || $4 ~ name || $12 ~ name) {
				split($2, a, "/");
				printf("%-20s\t%-25s\n", $1, $2);
			}
		}' ${PORTSDIR}/${INDEXFILE}
	fi
}

# Perform simple search in INDEX
index_search()
{
	if [ ${KFLAG} -eq 0 ]; then
		awk -F\| -v name="$1" '{
			if ($1 ~ name) {
				split($2, a, "/");
				printf("%-20s\t%-25s\n", $1, $4);
			}
		}' ${PORTSDIR}/${INDEXFILE}
	else
		awk -F\| -v name="$1" '{
			if ($1 ~ name || $4 ~ name || $12 ~ name) {
				split($2, a, "/");
				printf("%-20s\t%-25s\n", $1, $4);
			}
		}' ${PORTSDIR}/${INDEXFILE}
	fi
}

usage()
{
cat << EOF 
Usage: pkg_search [ -h | -k | -r | -v ] <name>
	-h Print usage
	-k Also query the Info and WWW field.
	-r Print the directory within the Ports Collection where the
	   port lives.
	-v Print verbose information

	<name> Required package name.
EOF
        exit 1
}

args=`getopt kvr $*`

KFLAG=0
VFLAG=0
RFLAG=0

set -- $args
for i; do
	case "$i" in
	-h)
		usage; shift;;
	-k)
		KFLAG=1; shift;;
	-r)
		RFLAG=1; shift;;
	-v)
		VFLAG=1; shift;;
	--)
		shift; break;;
	esac
done

if [ -z ${1} ]; then
	usage
fi

if [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 1 ]; then
	bin_simple_search $1
elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 1 ]; then
	bin_ext_search $1
elif [ ${RFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
	index_r_search $1
elif [ ${VFLAG} -eq 1 -a ${NO_INDEX} -eq 0 ]; then
	index_v_search $1
elif [ ${VFLAG} -eq 0 -a ${NO_INDEX} -eq 0 ]; then
	index_search $1
fi

exit $?
