#!/usr/bin/tcl

# Copyright 1996 James Lowe, Jr.
# Copying policy: FSF's GPL

# $Id: shak_notify_v 1.4.1.2 1996/11/29 03:25:58 jhl Exp $
#
# Notification Mechanisms for error logging. 
 
# ARGS
# ARG1 : is the facility,level   parameter which is an option of the logger(1) program.
# ARG2 : is the user command.

# Here is a test command line for a test of this program 
# SHAK_stderrmonlog=yes SHAK_stderrmonmail=yes  SHAK_USERLOGIN=jhl ./shak_notify user.err test 1 2 3 </etc/passwd

set NLEVEL [lindex $argv 0] 
set USERCMD [lindex $argv 1] 

set fd_logger ""
set fd_mail ""


# Here are the notification commands
set str_logger "open {|logger -i -t SHAK -p $NLEVEL} w"
set str_mailer "open {|mail -s \"shak_errmon: USER BACKUP: $USERCMD\" -c root  $env(SHAK_USERLOGIN)}  w"


if { $env(SHAK_stderrmonlog) == "yes" } {
	set fd_logger [ eval $str_logger ]
}

if { $env(SHAK_stderrmonmail) == "yes" } {
	set fd_mail [ eval $str_mailer ]
}

set fd_stderr "stderr"

while {[gets stdin line] >= 0} {
    if { $env(SHAK_stderrmonterm) == "yes" } {
        puts stderr $line
        flush stderr
    } 
    if { $fd_logger != "" } {
       puts $fd_logger $line
    }
    if { $fd_mail != "" } {
       puts $fd_mail $line
    }
}

if { $fd_logger != "" } {
    close  $fd_logger
}

if { $fd_mail != "" } {
    close $fd_mail
}

close stderr

exit

