#!/bin/csh -f
#
#	rn.sh - News reading program - check for news every so often,
#		change icon etc. when news is present/absent.
#
#	Normally invoked by "tooltool -f rn.tt".
#
#	Written by Jonathan Bowen, July/August 1988
#	Based on "newsreader", written by Jonathan Bowen, October 1987
#	Based on a "newsread" shell script by Jeremy Jacob, 9 Oct 87
#	Updated by Jonathan Bowen, February 1989
#

onintr -

if (! ${?ROOT}) then
  set ROOT=/usr/news
endif

set PATH=/bin:/usr/bin:/ucb/bin:/usr/local/bin:$ROOT/bin

if (! ${?PROGNAME}) then
  set PROGNAME=`basename $0`
endif

# Directory containing icons for this shell script
set ICONDIR=$ROOT/images

# Interval in seconds between checks for news
set INTERVAL=600

# BEL is the ASCII bell character (octal 007)
set BEL=`echo -n x | tr x '\007'`
# ESC is the ASCII escape character (octal 033)
set ESC=`echo -n x | tr x '\033'`

# "rn" is the default newsreading program unless NEWSPROG is set
if (! ${?NEWSPROG}) then
  set NEWSPROG=rn
endif

echo -n "${ESC}]L${ESC}\"

while (1)

  echo -n "${ESC}[2t"
  set NEWNEWS=`rn -c`

  if (! $#NEWNEWS) then
    echo -n "${ESC}]I$ICONDIR/nonews.icon${ESC}\"
    echo -n "${ESC}]l$PROGNAME - No news${ESC}\"

# Check for new news
    while (! $#NEWNEWS)
      onintr read
      sleep $INTERVAL
      set NEWNEWS=`rn -c`
      goto cont
# Read news anyway
read:
      onintr -
      echo -n "${ESC}]l$PROGNAME - Reading news${ESC}\${ESC}[1t"
      clear
      $NEWSPROG
      clear
      echo -n "${ESC}[2t${ESC}]l$PROGNAME - No news${ESC}\"
cont:
      onintr -
    end

# Enable "News" icon.
    echo -n "${ESC}]I$ICONDIR/news.icon${ESC}\"
# Sound bell  ("Read   all    a------bout   it!")
    sunbell     200 50 200 50 100 50 400 50 100
# Sound bell twice (use if "sunbell" is not available)
#   echo -n "${BEL}" ; sleep 1; echo -n "${BEL}"
# Sound bell and make icon visible  (Not safe if screen is locked)
#   echo -n "${BEL}${ESC}[5t" ; echo -n "${BEL}"
  endif

# Read new news
  echo -n "${ESC}]I$ICONDIR/news.icon${ESC}\"
  echo -n "${ESC}]l$PROGNAME - News${ESC}\"
  clear
  $NEWSPROG
  clear

end

