#! /bin/sh
# $Id: mhprofile,v 1.6 92/07/24 17:36:00 jerry book2 $
### mhprofile - show matching line(s) from MH profile file
### Usage: mhprofile [-b] component-name
##
##  USE mhprofile TO READ A LINE FROM THE .mh_profile FILE.
##  FOR EXAMPLE, IF YOU WANT TO READ THE "DRAFT-FOLDER" LINE, TYPE:
##      $ mhprofile draft-folder
##      Draft-Folder: drafts
##  THE -b OPTION STRIPS OFF THE COMPONENT NAME.  EXAMPLE:
##      $ mhprofile -b draft-folder
##      drafts
##
##  RETURNS 0 IF MATCH FOUND, 1 IF NO MATCHES, 2 ON ERRORS
# 
#                             NO WARRANTY
# 
#   BECAUSE THIS PROGRAM IS AVAILABLE FREE OF CHARGE, THERE IS NO WARRANTY
# FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
# TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
# PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
# REPAIR OR CORRECTION.
# 
#   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
# WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
# REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
# INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
# TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
# YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
# PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGES.


grep=/bin/grep  # HAS -i OPTION, HANDLES REGULAR EXPRESSIONS
sed=/bin/sed
profile=${MH-${HOME?}/.mh_profile}  # COMPLAIN, EXIT IF $HOME NOT SET

# GET -b OPTION, IF ANY, AND shift IT AWAY:
case "$1" in
-b) stripname=y; shift;;
-*) echo "Usage: `basename $0` [-b] component-name" 1>&2; exit 2 ;;
esac

# ONLY REMAINING ARGUMENT SHOULD BE A COMPONENT NAME:
case $# in
1)  ;;
*)  echo "`basename $0` quitting: wrong number of args." 1>&2; exit 2 ;;
esac

# IF grep FAILS, RETURN ITS STATUS (1=NO MATCH, 2=ERROR):
lines="`$grep -i \"^${1}:\" $profile`" || exit

# IF -b SET, USE sed TO SEARCH AND STRIP OFF LABEL+WHITESPACE.
# ASSUME NO COLON IN NAME, ":" AND MAYBE WHITESPACE AFTER NAME:
case "$stripname" in
y)  echo "$lines" | $sed -n 's/^[^:]*:[     ]*//p' ;;
*)  echo "$lines" ;;
esac
exit 0  # A LITTLE PRESUMPTUOUS
