#! /bin/bash
#
# The System Manager
# An Administration Tool For UNIX
#
# Copyright (c) 1994 Stuart Herbert
# Released under the GNU GPL v2
#
# Bugs/comments/contributions to S.Herbert@Sheffield.ac.uk

function main()
{
  local MenuSelection
  local NewsFile
  
  if [ ! -w /var/adm/news ]; then
    Abort "You are not permitted to add to the news"
    return
  fi
  
  echo "--- Width Of Display --- Width Of Display --- Width Of Display ----" > $SysmanFile1
  echo "Subject : " >> $SysmanFile1
  
  if [ -f $HOME/.sig ]; then
    cat $HOME/.sig >> $SysmanFile1
  fi
  
  if [ "$EDITOR" = "" ]; then
    vi $SysmanFile1
  else
    $EDITOR $SysmanFile1
  fi
  
  while [ 0 ]; do
    Dialog --menu "Please select an option." 10 71 3 \
    " EDIT " " Edit Your News Item " \
    " POST " " Post Your News Item " \
    " QUIT " " Abandon Your News Item " 2> $SysmanFile2
    
    if [ -s $SysmanFile2 ]; then
      MenuSelection="`cat $SysmanFile2`"
      case "$MenuSelection" in
        " EDIT ")
	  if [ "$EDITOR" = "" ]; then
	    vi $SysmanFile1
	  else
	    $EDITOR $SysmanFile1
	  fi
	  ;;
	" POST ")
	  break
	  ;;
	*)
	  return
	  ;;
      esac
    else
      return
    fi
  done
  
  Dialog --menu "Who Is The Item For?" 9 71 2 \
  " EVERYONE " " Item Is For All Users " \
  " GROUP "    " Item Is For A Particular Group " 2> $SysmanFile2
  
  if [ -s $SysmanFile2 ]; then
    MenuSelection="`cat $SysmanFile2`"
  else
    MenuSelection=" EVERYONE "
  fi
  
  if [ "$MenuSelection" = " GROUP " ]; then
    SelectGroup "OneOfMine" "Which group is the item for?" "$SysmanFile2"
    if [ -s $SysmanFile2 ]; then
      MenuSelection="`cat $SysmanFile2`"
      NewsFile="/var/news/$MenuSelection"
    else
      MenuSelection=""
      NewsFile="/var/news/All.Users"
    fi
  else
    MenuSelection=""
    NewsFile="/var/news/All.Users"
  fi
  
  InfoBox "Adding news item ... please wait"
  
  RealName="`grep ^$USER: /etc/passwd | cut -d : -f 5 | cut -d , -f 1`"
  echo "------------------------------------[`date +"%l:%M %p"`]--[`date +"%a %d %b %Y"`]--" > $SysmanFile3
  echo "From    : ($RealName) $USER" >> $SysmanFile3
  if [ ! "$MenuSelection" = "" ]; then
    `echo "For     : Group '$MenuSelection'" >> $SysmanFile3`
  fi
  tail +2 $SysmanFile1 >> $SysmanFile3
  
  if [ -r $NewsFile ]; then
    `cat $NewsFile >> $SysmanFile3`
  fi
  `cat $SysmanFile3 > $NewsFile`
  
  if [ ! "$MenuSelection" = "" ]; then
    chgrp $MenuSelection $NewsFile
    chmod 660 $NewsFile
  else
    chmod 664 $NewsFile
  fi
  
  touch $NewsFile
  
  MsgBox "News item has been added"
}

# main code
if [ -n "$1" ]; then
  case "$1" in
    --SysmanInstall)
      echo "Y : Y : ADD : Add To The News :"
      exit
      ;;
    --SysmanAuthor)
      echo "ADD : Add A News Item"
      echo "Copyright (c) 1994 Stuart Herbert"
      exit
      ;;
  esac
else
  main
fi

# clean up here
for x in $SysmanFile1 $SysmanFile2 $SysmanFile3 ; do
  if [ -f $x ]; then rm -f $x ; fi
done

# that's it, folks
