#!/bin/sh
#
# SysMail 1.0.0
#
# Script used to mail a message containing
# tokens to all the users on a system
#
# Stefan van der Walt   1998/03/31
# (trax@the-force.ml.org)
#

# ------------------------------------------#

# config section

testrun=1 # define testrun=1 to first just run through
          # list of names without actually mailing
	  
# end of config section -- no more changes needed below this line

# We don't need all those newlines, now, do we?
echo="echo -e"

# And too lazy to type out ANSi codes every time
black="\033[0;30m"
darkred="\033[0;31m"
darkgreen="\033[0;32m"
brown="\033[0;33m"
darkblue="\033[0;34m"
darkmagenta="\033[0;35m"
darkcyan="\033[0;36m"
darkgray="\033[0;30;1m"
gray="\033[0;37m"
red="\033[0;31;1m"
green="\033[0;32;1m"
yellow="\033[0;33;1m"
blue="\033[0;34;1m"
magenta="\033[0;35;1m"
cyan="\033[0;36;1m"
white="\033[0;37;1m"

normal="\033[0;0m"

# Let's just, par default, accept that
# this admin knows it all...
parameter=true
case $1 in
  '')
$echo "${white}Usage: sysmail 'message-subject'"
$echo
$echo "${blue}Remember to quote the message topic, so it can be read as one parameter.  Edit"
$echo "the file ${gray}./message${blue} in the current directory.  It should contain the message"
$echo "to be sent in the following format:"
$echo
$echo "${darkcyan}  cat << END"
$echo "    this is a test message to \$1 where \$1 will be replaced by the user's"
$echo "    first name, as found in /etc/passwd"
$echo "  END"

    parameter=false # Well, maybe I was wrong...
  ;;
esac

case $parameter in
true)
  subject=$1
  # Now make sure we have the correct subject
  $echo "${white}Subject of messages: ${darkmagenta}$1"
  echo -en "${white}Continue? ${darkgray}(${gray}y/n${darkgray})[${gray}N${darkgray}] ${green}"
  read userkey # woohoo! user input! yeah :)
  case $userkey in
    Y) userkey='1' ;;
    y) userkey='1' ;;
    *) userkey='0' ;;
  esac
  
  # But let's just make sure that ./message IS chmod +x
  # So, if he presses Y:
  
  case $userkey in
  '1')
  
  # Set RED in case ls doesn't find the file...
  $echo ${red}
  chmodx=`ls -al ./message | cut -f 1 -d ' ' | grep x`
  case $chmodx in
  '') $echo ${gray}Checking if ./message is chmod +x...no
  userkey='0' # EEk! Abort while we still can!
      ;;
  *) $echo ${gray}Checking if ./message is chmod +x...yes ; echo ;; # Beam it, Scotty.
  esac
  ;;
  '0') echo # Hey, it looks nice :)
  ;;
  esac

case $userkey in
  1)
  for username in `cut -d ":" -f 1 < /etc/passwd` ; do
  # next, find the user's first real name
  #
  # other tokens can be implemented here too
  realname=`cat /etc/passwd | grep $username | head --line=1 | cut -d ':' -f 5  | cut -d ',' -f 1 | cut -d ' ' -f 1`
  
  # if this is not a testrun, execute the mailer
  case $testrun in
  '0') ./message $realname | mail $username -s $subject
  ;; esac
  
  $echo "${white}Mailing ${blue}${username} ${gray}(${darkcyan}${realname}${gray})"
  done ;;
  *) $echo "${gray}Aborted${darkgray}... ";;
esac
esac

$echo $normal
