#! /bin/ksh
#
#! /bin/ksh
#
#ident  "@(#)notifier   1.7     96/03/11 SMI"
#
# Copyright (c) 1996 by Sun Microsystems, Inc.
# All rights reserved.
#
# THIS SAMPLE PROGRAM IS BEING PROVIDED "AS IS" AND ONLY AS A COURTESY TO
# THE RECIPIENT.  SUN MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS 
# OR IMPLIED WITH RESPECT TO THIS SAMPLE PROGRAM INCLUDING QUALITY, PERF- 
# FORMANCE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON- 
# INFRINGEMENT.  IN NO EVENT WILL SUN BE LIABLE FOR ANY DIRECT, INDIRECT,  
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR  
# INABILITY TO USE THIS SAMPLE PROGRAM.
 
# Process CodeManager notification mail to make it more useful
 
# This works by running two nawk's back-to-back, the first to pick out the
# information we are interested in using to rewrite the mail, the
# second to actually rewrite the mail.  While this could be done with a
# single nawk, it's easier to do it this way and let the pattern matching
# do what would otherwise take a big gnarly if statement to accomplish.
# The second nawk's output goes into a temp file, and then that file is passed
# to sendmail to do the actual notifications.
 
# mf is the temporary mail file
mf=/tmp/mf.$$
# ma is the temporary file we put the notification alias into
ma=/tmp/ma.$$
# this is the file we look in for the notify alias, it must be in the root
# directory of the workspace named in the mail.
nf="/notify"
# Header is first in subject line at all times
hdr="CodeMgr"
 
nawk '
cmt == 1 { comment = $0; cmt = 0; }
/^Event:/ { ev = $2; }
/^Parent workspace:/ { pws = $3; }
/^Child workspace:/ { cws = $3; }
/^User:/ { usr = $2; }
/^Workspace:/ { pws = $2; }
/^New location:/ { cws = $3; }
/^New parent:/ { cws = $3; }
/^Comment:/ { cmt = 1; }
        { lines[NR] = $0 }
END {   print comment;
        print ev, usr, pws, cws;
        for (i=1; i <= NR; ++i)
                print lines[i] }
' | nawk -v nf=${nf} -v ma=${ma} -v hdr=${hdr} '
NR == 2 { ev = $1; usr = $2; pws = $3; cws = $4;
          if (ev == "putback-to") {
                np = pws nf;
                n = split(pws, p, "/");
                subj = sprintf("%s:%s:%s:%s", hdr, ev, p[n], substr(comment, 0, 60));
          } else if (ev == "workspace-reparent") {
                np = pws nf;
                n = split(pws, p, "/");
                subj = sprintf("%s:%s:%s:to %s", hdr, ev, p[n], cws);
          } else if (ev == "bringover-to") {
                np = cws nf;
                n = split(cws, p, "/");
                subj = sprintf("%s:%s:%s:%s", hdr, ev, p[n], substr(comment, 0, 60));
          } else if (ev == "workspace-move") {
                np = cws nf;
                n = split(pws, p, "/");
                subj = sprintf("%s:%s:%s to %s", hdr, ev, p[n], cws);
          } else if (ev == "undo") {
                np = pws nf;
                n = split(pws, p, "/");
                subj = sprintf("%s:%s in workspace %s", hdr, ev, p[n]);
          } else {
                np = pws nf;
                subj = sprintf("%s: Notification", hdr);
          }

          getline t <np;
          print t >ma; next }
NR == 1 { comment = $0; next }
/^To:/  { print "To: ", t; next }
/^Subject:/ { printf "Subject: %s\n", subj; next; }
        { print }
'>${mf}
/usr/lib/sendmail $(<${ma}) <${mf}
rm ${ma} ${mf}
 
