#/bin/csh -f
#
# yo_mail - script to define and use Yohkoh mail aliases
#	    (use $ys/gen/setup/ys_mailrc - same format as .mailrc file)
#
#  7-Feb-1994 (SLF) Provide single point Yohkoh alias maintainence
#	            Avoid shell corruption of multiple setenv commands
# 10-Feb-1994 (SLF) file input, synonyms, pass arg-> mail
# 18-Feb-1994 (SLF) site capability (use $ys/site/setup/ys_mailrc if exists)
# 		    make confirm the default, add /noconfirm switch
# 10-Feb-1997 (TRM) Change to pipe file to Mail
#
# Input Parameters:
#   ALIAS - an alias defined in ys_mailrc (or a pattern)
#
#; Calling Sequence and Examples:
#;   yo_mail [<mailswitches>] [-file <file>] ALIAS [/help /confirm ] 
#;   yo_mail -v -s "subject" -file /path/status.doc SXT_STATUS /confirm	
#; 
#;   <mailswitches>  # OPTIONAL switches passed to mail (ex: -v -s "SUBJECT")
#;   -file <file>    # OPTIONAL send message file named <file>
#;   ALIAS	     # mail alias or pattern defined in ys_mailrc (see below)
#;   /help           # show expanded alias or alias list
#

set mailrc=$ys/gen/setup/ys_mailrc	# Yohkoh alias file

set tmailrc="~/ys_mailrc.temp"
# pre-pend site version if it exists
if (-e $ys/site/setup/ys_mailrc) then
   /bin/rm -f $tmailrc
   cat $ys/site/setup/ys_mailrc $mailrc > $tmailrc
   set mailrc=$tmailrc
endif

#					# alias syntax same as .mailrc
############# set defaults #############
set help="0"
set confirm="1"
set argn=()
set fileon="0"
set subject=""
set subjon="0"
set infile=""
########################################
#
##################################################################
# parse arguments, strip out non-mail related
while ($#argv > 0) 
   if ($fileon == "1") then
      set infile=$argv[1]
      set fileon="0"
   else 
      if ($subjon == "1") then
         set subject="$argv[1]"
         set subjon="0"
         echo SUBJECT $subject
      else
        switch ($argv[1])
         case =:
            breaksw
	 case -mess:
         case -message:
         case -file:
            set fileon="1"
            breaksw
         case /help:
            set help="1"
            breaksw      
         case -s:
         case -S:
            set subjon="1"
            breaksw
	 case /nocon:
         case /noconf:
         case /noconfirm:
	    set confirm="0"
            breaksw
         case /con:
         case /conf:
         case /confirm:
            set confirm="1"
            breaksw
         default:
            set argn=($argn $argv[1] )
            breaksw
         endsw
      endif
   endif
   shift argv
end
##################################################################
if ($infile != "") then
   if (-e $infile) then 
      echo "Using mail message from file: "$infile
      set infile="$infile"
   else
      echo "Cannot find mail message file: "$infile" , exiting..."
      exit
   endif
endif
#
# seperate mail switches from ALIAS
set marg=""
set narg=$#argn
switch ($narg)
   case 0:
      breaksw
   case 1:
      set inpattern=$argn[1]
      breaksw
   default
      set inpattern=$argn[$narg]
      @ narg = $narg - 1
      set marg="$argn[1-$narg]"
      echo "mail arguments "$marg
      breaksw
endsw

# search ys_mailrc file for ALIAS pattern matches
if ($help == "1") then
   if ($#argn == 0) then
        head -30 $ys/gen/script/yo_mail | grep "#;" 
        echo "--- MAIL ALIASES ---"
        awk '/alias/ {print $2}' $mailrc
      exit
   else
      set awkcmd="awk '/'$inpattern'/ {print NR}' $mailrc"
      set cnt=`awk '/'$inpattern'/ {print NR}' $mailrc`
   endif
else
   if ($#argn == 0) then
      csh $ys/gen/script/yo_mail /help
      exit
   else
      set cnt=`awk '/'$inpattern'/ {print NR}' $mailrc`
      if ($#cnt == "0") then
         echo No alias match found for $inpattern
	 csh $ys/gen/script/yo_mail /help
         exit
      endif
   endif
endif
#################################################################3

# loop through matches
set envlist=()
set allnames=()
while ($#cnt >= 1 )
#  determine start line of alias
   set term=`tail +$cnt[1] $mailrc | awk '/alias/ {print NR}'`
#  determine termination line for each
   if ($#term <= 1) then
      set lcnt=100
   else
      set lcnt=$term[2]
      @ lcnt = $lcnt - 1
   endif
   set onealias=`tail +$cnt[1] $mailrc | head -$lcnt`
      set alias=$onealias[2]
      shift onealias
      shift onealias
      set names=()
      foreach name ($onealias)
         if ("$name" != "\") set names=($names $name)
      end
      if ("$help" == "1") then
         echo "------------------ alias: $alias ----------------------"
         echo $names
         echo " "
      endif
      set allnames=($allnames $names)
   shift cnt
end

if ($help == "0") then
   set ok="ok"
   echo Requested mail address list is:
   echo $allnames
   echo " "
   if ($confirm == "1") then 
      echo -n "Enter <ok> to continue, anything else to abort: "
      set ok = $<
   endif

# 
#  Now send the mail	(sloppy if logic due to shell/mail interactions)
   if ("$ok" == "ok") then
      set wmail=`which Mail`
      set wcat=`which cat`
      if ($#wmail > 1) set wmail=`which mail`
      if ($infile == "") then
         if ("$subject" == "") then
            $wmail $marg' '$allnames
         else
            set subject="$subject"" "
            $wmail $marg -s "$subject" $allnames
         endif         
      else
         if ("$subject" == "") then
            $wcat $infile | $wmail $marg' '$allnames
         else
            set subject="$subject"" "
            $wcat $infile | $wmail -s "$subject"$marg $allnames
         endif
      endif
   else
      echo Aborting, no mail sent...
   endif
endif

/bin/rm -f $tmailrc

exit
