#! /bin/sh
# $Id: mk_fol_names 0.5 1997/06/20 12:07:49 jpeek Exp $
### mk_fol_names - make a folder-changing command or variable from program name
### Usage: fol_names-command [MH-command_options-or_arguments...]

##  Command executed is:
##      exec <command> <command-line arguments (if any)> <folder(s)>

T=' '   # TAB character; easier to distinguish from a space when used as $T

# Table of folder abbreviations and full names, in this syntax:
#   abbrev<TAB>fulname
# Example:
#   mb  mh-book
#   mba mh-book/authors
#   gf  +g/a +g/foreign
# That table would handle commands amb, amba, agf fmb, fmba, fgf.
# Note that last line gives two folder names.  That's okay.
#
# 
#                             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.

# Directory name is hardcoded for speed.  Can use `mhpath +` for portability.
table=/home/jpeek/.Mail/folder_table

# Cannot make commands like these; override with aliases or don't link:
# alias fgd='refile +g/a +g/done'
# alias fgi='refile +g/a +g/ignore'
# alias fgm='refile +g/a +g/memos'
# alias fgp='refile +g/a +g/pending'
# alias aga='rfl +g/a'
# alias ams='rfl -q -r all +mailsvcs/surveys'

# To install: hard or soft link once for each folder abbreviation.
# First letter of program name should be:
#   a = rfl
#   f = refile
# For shell variables, link as "csh_folders" or "sh_folders".

# If we're calling this as csh_folders or sh_folders, spit out commands
# to set shell variables with csh or sh syntax and then exit.
case "$0" in
*csh_folders)
    sed -n "/^[^#]/s/^\([^$T]*\)$T\(.*\)/set \1 = '\2'/p" $table
    exit
    ;;
*sh_folders)
    sed -n "/^[^#]/s/^\([^$T]*\)$T\(.*\)/\1='\2'/p" $table
    exit
    ;;
esac

# Get name of this program (without directory path) and any abbreviation.
# Store name in $myname, first letter of name in $func, abbreviation in $abbrev
eval `echo "$0" |
# Note: delete comments in sed script if your version of sed complains:
sed -n '
# Strip leading pathname, if any:
s@^.*/@@
# Keep stripped name in hold buffer:
h
# Output shell variable setting commands to be read by "eval" command:
s/.*/myname="&"/p
g
s/^\(.\)\(.*\)/func=\1 abbrev="\2"/p'`

# DEBUG:
#echo "myname is '$myname'"
#echo "func is '$func'"
#echo "abbrev is '$abbrev'"

# IF ADDING NEW COMMANDS, CUSTOMIZE HERE.
# This puts command name in $cmd.
case "$func" in
a)  cmd=rfl ;;
f)  cmd=refile ;;
*)  echo "$0: Help!  Don't know how to run myself!" 1>&2; exit 1 ;;
esac

# If we get here, need to do lookup.  Store folder name(s) in $fols:
fols=`sed -n "/^$abbrev$T/s///p" $table`
case "$fols" in
"") echo "$myname: no match for '$abbrev' in $table." 1>&2; exit 1;;
*"
"*) echo "$myname: more than one match for '$abbrev' in $table:" 1>&2
    echo "$fols" 1>&2
    exit 1
    ;;
*)  # exec command (to save a process).
    # The ${1+"$@"} avoids problem with an empty "$@" in some Bourne shells:
    exec $cmd ${1+"$@"} $fols
    ;;
esac
