mds@hpfcmgw.HP.COM (Mark Schulz) (10/18/89)
Here's a little script. It's got comments so you can see what it's all
about. Of course there are many enhancements that could be made to type
of thing if anyone is game.... enjoy.
---------------------------- cut here ---- >-8 --------------------------------
#! /bin/ksh -f
###############################################################################
# This UNSUPPORTED utility script adds names to your ELM alias file. It #
# searches for names via the "where" command and, if not found there, via #
# the "hpdesk" command. Several possibilities can be dealt with when told #
# to add a name: #
# #
# o A single occurrence is found by "where"; it is added. #
# o The name is not found by "where", but a single occurrence is found #
# by "hpdesk"; it is added. #
# o Multiple occurrences are found by "where"; the user is presented #
# with a list of possible choices, he makes a selection, and it is #
# added. #
# o No occurrences are found by "where", but multiple occurrences are #
# found by "hpdesk"; again, the user is asked to make a selection. #
# o No occurrences are found by "where" or by "hpdesk"; the user is #
# notified that the name was not found, and nothing is changed. #
# #
# In any case in which a name is found/selected, its address is checked #
# against the current alias file. If it is already there, the user is #
# notified, and the name is not added to the file. #
# #
# By default, the "where" directory is searched before the "hpdesk" direc- #
# tory. If you want to add a name that occurs in "hpdesk" but which also #
# occurs in the "where" directory, the program will stop searching when it #
# finds the name in the "where" directory UNLESS you specify that the pro- #
# gram is to search only the "hpdesk" directory. This is accomplished by #
# using the "-hpdesk" option. #
# #
# There are three formats of names that can be found: the first two below #
# are from "where", the last from "hpdesk". #
# #
# Arns,Dave ..................... davea@hpfcmgcf ....... davea@hpfcla #
# Aalbu,Jarl .................... jarl@hp-lsd #
# Arns,Dave ..................... %ux@hp4000 #
# #
# In the "where" case that contains two addresses, the first one is used. #
#-----------------------------------------------------------------------------#
# Author: Dave Arns (send any bug reports to davea@hpfcmgcf) #
# Written: October 6, 1989 #
#-----------------------------------------------------------------------------#
# Oct 10, '89 Made the alias file name variable for those systems #
# (Enhancements) where it is something other than "$HOME/.alias_text". #
# Made the alias file delimiter specifiable. Allows #
# user to specify alias name if desired. #
###############################################################################
#------------------------------------------------------------------------------
#--- Explain the usage of the script ------------------------------------------
function usage
{
echo "Usage:"
echo " addtoelm [-where] [-hpdesk] [-help] <name> [<name>]"
echo ""
echo "where:"
echo " -where is an option that restricts searching to the \"where\""
echo " command only. Default is both \"where\" and \"hpdesk\"."
echo " -hpdesk is an option that restricts searching to the \"hpdesk\""
echo " command only. Default is both \"where\" and \"hpdesk\"."
echo " -custom prompts the user for the alias name, rather than using"
echo " \"firstname_lastname\"."
echo " -help give this Usage message."
echo " <name> is a string specifying the name of the person whose mail"
echo " address you want to add to your alias file."
}
#------------------------------------------------------------------------------
#--- Search for the name ("$Adding") by using the command "$NameSource" -------
function search_for_name
{
echo ".\c" # extra period signifies hpdesk
($NameSource $Adding > $temp) 2> /dev/null # ignore any error messages
Entries=$(echo $(cat $temp | wc -l)) # determine the number of lines
if [ $Entries -gt 0 ] # did we find any?
then
Found=true # yes, at least one
else
Found=false # nope, none.
fi
if [ $Entries -gt 1 ] # did we find more than one?
then
Ambiguous=true # yes, user must choose one
else
Ambiguous=false # nope, no ambiguity
fi
}
#------------------------------------------------------------------------------
#--- Let user select one of multiple possible names ---------------------------
function disambiguate
{
echo "Multiple entries--\nPlease select the desired entry:"
cat $temp | awk '{printf "%3d: %s\n", NR, $0}'
Number=0 # no number selected yet
Abort=false # assume a good answer
while [ \( $Number -lt 1 -o $Number -gt $Entries \) -a "$Abort" = "false" ]
do
read Number?"Selection? " # ask for a selection
if [ -z "$Number" ] # user just hit [Return]?
then
Number=0 # don't let the "if" blow up
Abort="true" # if so, don't pick anything
fi
done
if [ "$Abort" = "false" ] # if not [Return], it's a number
then # extract specified line
NewLine="$(cat $temp | awk '{if(NR == '$Number') print $0}')"
fi
}
#------------------------------------------------------------------------------
#--- Add name and appropriate address to alias file ---------------------------
function add_name_to_alias_file
{
RealName="$FirstName $LastName" # "plain-English" name
if [ "$CustomAliasNames" = "true" ]
then
read AliasName?"Alias name? (defaults to \"$FirstField\") "
if [ -z "$AliasName" ]
then
AliasName="$FirstField"
fi
else
AliasName="$FirstField"
fi
if [ "$NameSource" = "where" ]
then # (where)
NewEntry="$AliasName\t$Delim $RealName\t$Delim $Address"
else # (hpdesk)
NewEntry="$AliasName\t$Delim $RealName\t$Delim $HubName!$Address"
fi
echo $NewEntry >> $AliasFileName # append to alias file
echo "Added:\n $NewEntry" # notify user of what was added
FileChanged=true # set flag for "newalias"
}
#------------------------------------------------------------------------------
#--- Add name to alias file if it's not already there -------------------------
function compose_and_check_name
{
typeset -l FirstField # lowercase variable
Address="$(echo $NewLine | cut -d" " -f3)" # 3rd field of line
Name="$(echo $NewLine | cut -d" " -f1)" # 1st field of line
FirstName="$(echo $Name | cut -d, -f2)" # 1st subfield of $Name
LastName="$(echo $Name | cut -d, -f1)" # 2nd subfield of $Name
FirstField=$FirstName'_'$LastName # compose ELM token
if [ "$NameSource" = "hpdesk" ] # name come from "hpdesk"?
then
Address="$FirstField$Address" # yes, make desk-style address
fi
if [ -n "$(grep $Address $AliasFileName)" ] # already in alias file?
then
echo "Already on file." # yes, tell user
else
add_name_to_alias_file # no, add to alias file
fi
}
#------------------------------------------------------------------------------
#--- Deal with parameters that are people's names (not options) ---------------
function process_name_parameter
{
echo "Adding \"$Adding\" to the ELM alias file..\c"
Found=false # assume it's not found (yet)
((SourceNo=1))
while [ $SourceNo -le 2 -a $Found = "false" ] # search specified sources
do
NameSource=${NameSources[SourceNo]}
search_for_name # ...then search again
((SourceNo=SourceNo+1))
done
echo "" # print tab for alignment
if [ "$Found" = "true" ] # did we find one?
then
if [ "$Ambiguous" = "true" ] # yes; but is it ambiguous?
then
disambiguate # yes; ask user to choose one
else
NewLine="$(cat $temp)" # not ambiguous; take it
Abort=false # selection not aborted
fi
if [ "$Abort" = "false" ] # if search completed...
then
compose_and_check_name # add to file (if not there)
else
echo "No change."
fi
else
echo "Not found." # not found; tell user
fi
}
#------------------------------------------------------------------------------
#-- Set any requested options -------------------------------------------------
function process_option_parameter
{
case $Option in
-where) NameSources[1]="where"
NameSources[2]="";; # search only "where" directory
-hpdesk) NameSources[1]="hpdesk"
NameSources[2]="";; # search only "hpdesk" directory
-custom) CustomAliasNames=true;; # user-specifiable alias names
-help) Usage;; # give user instructions
*) echo "Illegal parameter: \"$Option\""; Usage; exit 1;;
esac
}
#==============================================================================
#=== Main Program =============================================================
temp=/tmp/temp$$ # scratchpad
FileChanged=false # assume not changed
NameSources[1]="where" # default: search both places
NameSources[2]="hpdesk" # default: search both places
HubName="hpfcla" # prefix for "hpdesk" addresses
AliasFileName="$HOME/.alias_text" # the file to modify
Delim=":" # "=" can also be used
CustomAliasNames=false # ask user for alias names?
echo "ELM alias file: \"$AliasFileName\"" # advise user which file used
while [ $# -gt 0 ] # for all parameters...
do
Adding="$1" # make global variable
case $Adding in # what kind is it?
-*) Option="$1"; process_option_parameter;; # directive
*) Adding="$1"; process_name_parameter;; # data
esac
shift # get next one
done
if [ "$FileChanged" = "true" ] # if a name was added...
then
newalias # install new aliases
fi
/bin/rm $temp