#! /bin/sh
# $Id: edprofile,v 1.0 92/08/03 08:30:09 jerry book2 $
### edprofile - edit line in $MH file (default: $HOME/.xmh_profile)
### Usage: edprofile [-v] component [new-value] --
##
##  xmh USES THE DEFAULT MH COMMANDS scan, sortm, AND OTHERS.  xmh MAKES IT
##  TOUGH TO SET ANY OPTIONS, LIKE THE SORTING ORDER (ADDED IN MH 6.7),
##  WHILE xmh IS RUNNING.  YOU CAN EDIT THE MH PROFILE FILE IN AN xterm
##  WINDOW, OR PLAY SOME TRICKS WITH THE MhPath AND FRONT-END SHELL
##  SCRIPTS, BUT THAT'S ABOUT ALL.  THIS edprofile SCRIPT WORKS WITH xmh
##  RELEASE 5 AND ITS XmhShellCommand() TO LET YOU EDIT LINES IN THE MH
##  PROFILE WHILE YOU'RE RUNNING xmh.  YOU CAN BIND edprofile TO A BUTTON
##  THAT, SAY, CHANGES THE sort: OPTIONS.  YOUR NEXT SORT WILL USE THOSE
##  OPTIONS.  IF new-value IS EMPTY, SCRIPT WILL LEAVE AN EMPTY COMPONENT
##  THERE FOR NEXT TIME (THIS DOESN'T SEEM TO BOTHER MH).
##
##  THE -v FLAG WRITES A MESSAGE TO THE STANDARD ERROR WITH THE NEW
##  CONTENTS OF THE MH PROFILE LINE.  xmh R5 SHOWS THAT IN A DIALOG BOX.
##
##  IF THE MH ENVIRONMENT VARIABLE HAS BEEN DEFINED, edprofile EDITS THE
##  FILE THAT POINTS TO.  THE DEFAULT FILE IS $HOME/.mh_profile.
##
##  HERE'S AN EXAMPLE--HOW TO MAKE TWO BUTTONS THAT CHANGE sortm.
##  PUT THESE LINES IN YOUR RESOURCE FILE:
##
##  Xmh*CommandButtonCount: 2
##  Xmh*commandBox.button3.label: From sort
##  Xmh*commandBox.button3.translations: #override\
##    <Btn1Down>,<Btn1Up>: XmhShellCommand(edprofile sortm -textfield from --) unset()
##  Xmh*commandBox.button1.label: date sort
##  Xmh*commandBox.button1.translations: #override\
##    <Btn1Down>,<Btn1Up>: XmhShellCommand(edprofile -v sortm --) unset()
##
##  THE FIRST BUTTON, LABELED From sort, RUNS THE COMMAND
##      edprofile sortm -textfield from --
##  THAT CHANGES THE MH PROFILE sortm ENTRY TO LOOK LIKE THIS:
##      sortm: -textfield from
##  ANY SWITCHES ON THE LINE BEFORE ARE TAKEN AWAY.
##
##  IF YOU CLICK THE SECOND BUTTON, IT WILL RUN THE COMMAND:
##      edprofile -v sortm
##  THAT LINE WILL BE CHANGED TO THIS:
##      sortm:
##
##  UNFORTUNATELY, THERE MUST BE A CURRENT OR SELECTED MESSAGE BEFORE YOU
##  RUN XmhShellComand.  IF THERE'S NOT A MESSAGE SELECTED, PICK ANY ONE
##  BEFORE YOU CLICK THE BUTTON THAT RUNS edprofile.
# 
#                             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.

# ON ERROR, XmhShellCommand SHOWS ALL ARGUMENTS IN NOTICE BOX; WE DON'T HAVE TO.

grep=/bin/grep

: ${MH=${HOME?}/.mh_profile}    # THE PROFILE FILE WE EDIT

case "$1" in
-v) verbose=yes; shift;;
esac
# COMPONENT WE SEARCH FOR IS IN $1; REST OF ARGS HANDLED IN for LOOP:
case $# in
0)  echo "`basename $0` quitting: not enough arguments." 1>&2; exit 1 ;;
*)  cmd="$1"; shift;;
esac

ended= newargs=
for arg
do
    case "$arg" in
    --) ended=yes; break ;;
    *)  newargs="$newargs $arg" ;;
    esac
done

if [ "$ended" != yes ]; then
    echo "`basename $0` quitting: missing '--' after argument list." 1>&2
    exit 1
fi

if [ ! -r "$MH" -o ! -w "$MH" ]; then
    echo "`basename $0` quitting: can't read and/or write profile
    '$MH'" 1>&2
    exit 1
elif $grep "^$cmd:" "$MH" >/dev/null; then
    # DO THE EDIT.  SAVE ed OUTPUT AND STATUS FOR TESTING:
    errs=`/bin/ed - "$MH" << ENDEDIT 2>&1
    /^$cmd:[    ]*/s/:.*/: $newargs/
    w
ENDEDIT`
    status=$?
    if [ -n "$errs" -o $status != 0 ]; then
        echo "`basename $0`: edit bombed?  ed status=$status, messages:
        $errs" 1>&2
        exit 1
    elif [ "$verbose" = yes ]; then
        case "$newargs" in
        "") echo "$cmd set to default (no parameters)" 1>&2 ;;
        *)  echo "$cmd set to: $newargs" 1>&2 ;;
        esac
    fi
    exit 0
else
    echo "`basename $0` quitting: can't find '$cmd:' in profile
    '$MH'" 1>&2
    exit 1
fi
