#!/bin/sh ## author: parv, parv UNDERSCORE AT yahoo DOT com ## date: may 14 2002 ## ## license: free to use as you please w/ proper credit given ## ## name: search-ports-descr ## ## purpose: to search thru each port's description file; currently ## this file name is 'pkg-descr' on freebsd. ## ## usage: ## search-ports-descr ## ## to list lines mentioning 'mozilla' or 'jdk', (or, both)... ## search-ports-descr 'mozilla|jdk' ## ## to list lines mentioning perl... ## search-ports-descr perl ## # 4 lines around the line w/ pattern are printed via "grep -4"; see grep(1) # for details. # # possible modification would be to add option handling to specify alternate # ports tree root, name of package description file, cache directory & file. # until then, set those 4 variables below as required. # # another possible modification could be is to first search INDEX in # $ports_tree to look for the given pattern in the one-line-description. # from there get the corresponding detailed port description file name to get # more context. # # there are only about 8-9 lines of actual code; everything else is dedicated # for error handling. oh well... # set to the root of ports tree; freebsd default is /usr/ports ports_root='/misc/ports' # file containing port description in each port directory descr_file='pkg-descr' # save the $descr_file listing to avoid wait & hardship on the disk cache_dir="${HOME}/log/ports" cache_file="${cache_dir}/port-descr.list" # since "echo ... && exit 1" was often used, created a function to handle it Err_Exit() { echo " holy cow! ${@} " 1>&2 exit 1 } # exit if $ports_root is not a directory [ ! -d "${ports_root}" ] \ && Err_Exit "$ports_root is not a directory; please change $ports_root in $0." # exit if no search pattern given case $# in 0) Err_Exit 'please provide a search pattern' ;; *) ;; esac Chmod() { # try to change permissions ($1) of file(s) ${2 ..} case $# in 0|1) Err_Exit 'only one or none argument was given for chmod' ;; *) local perm perm="${1}" shift chmod "${perm}" "${@}" [ ! $? ] && Err_Exit "'chmod $perm ${@}' failed" ;; esac } List_Cache_Dir() { # create $cache_dir if it doesn't exist # if [ ! -e "${cache_dir}" ] then mkdir -m 700 -p "${cache_dir}" [ ! $? ] \ && Err_Exit "'mkdir -m 700 -p $cache_dir' failed; $cache_dir directory could not be created." # exit if $cache_dir is not a directory # elif [ ! -d "${cache_dir}" ] then Err_Exit " $cache_dir is not a directory; please move it or, change \$cache_dir in $0. " fi # try to change listing permissions; else exit # [ ! -x "${cache_dir}" ] \ && echo " $cache_dir could not be listed; trying to change permissions..." \ && Chmod 'u+x' "${cache_dir}" } Create_Cache_File() { # before trying to create $cache_file, first create/check $cache_dir # List_Cache_Dir # create $cache_file if it doesn't exist # if [ ! -e "${cache_file}" ] then echo " trying to create $cache_file cache..." [ ! -w "${cache_dir}" ] \ && echo " $cache_dir is not writable; trying to change permissions..." \ && Chmod 'u+w' "${cache_dir}" echo " searching for all the $descr_file named files in $ports_root" find "${ports_root}" -type f -maxdepth 3 -mindepth 3 \ -name "${descr_file}" -print \ | sort >> "${cache_file}" [ $? ] && echo ' ...done' [ ! $? ] && Err_Exit '...failed' fi # exit if $cache_file is unreadable [ ! -r "${cache_file}" ] && Chmod 'u+r' "${cache_file}" } # create $cache_file if unreadable/doesn't exist # [ ! -e "${cache_file}" -o ! -r "${cache_file}" ] && Create_Cache_File # finally search thru the $cache_file listing # echo " searching for pattern '${@}' ..." while read descr do #set -x -v egrep -i "${@}" "${descr}" >/dev/null 2>&1 || continue echo ">>--<< ${descr%${descr_file}}" echo ' ' grep -4 -E -i "${@}" "${descr}" echo ' ' echo ' ' done < "${cache_file}" set +x +v