#!/bin/sh

# @(#) Info.sh 2.10 Delta: 95/05/10 11:34:04 Extraction: 95/05/10 11:34:11 @(#)

WHERE=/fnal/ups/Linux/Info
NOW=`/usr/local/bin/getdate in secs, 00:00:00`
STANDARD='plus 3 months 1st mon-fri backwards in month'
OLD='minus 6 months'

if [ ! -d $WHERE ]
then
	echo "Info directory, ${WHERE}, doesn't exist."
	echo "Either change definition of WHERE or create directory."
	exit 1
fi

system=`/usr/local/bin/funame -s`
#If you don't have funame, comment above line, and explicitly assign the
#variable system: i.e. system=IRIX
EXPR=/bin/expr
case $system in
  "IRIX")
	ECHO=echo
	WC=/bin/wc
	LS='/bin/ls'
	BASENAME=/bin/basename
	FIND=/bin/find
	XARGS=/usr/bin/xargs;;
  "SunOS")
	ECHO=/usr/5bin/echo
	WC=/usr/ucb/wc
	LS='/bin/ls -1g'
	BASENAME=/bin/basename
	FIND=/bin/find
	XARGS=/usr/bin/xargs;;
  "ULTRIX")
	ECHO=/usr/local/systools/library/echo
	WC=/usr/ucb/wc
	LS='/bin/ls -1g'
	BASENAME=/usr/bin/basename
	FIND=/usr/bin/find
	XARGS=/usr/bin/xargs;;
  "AIX")
	ECHO=/bin/echo
	WC=/bin/wc
	LS='/bin/ls -1'
	BASENAME=/bin/basename
	FIND=/bin/find
	XARGS=/bin/xargs;;
  "OSF1")
        ECHO=echo
        WC=/bin/wc
        LS='/bin/ls -1'
        BASENAME=/bin/basename
        FIND=/bin/find
        XARGS=/bin/xargs;;
  "HP-UX")
        ECHO=echo
        WC=/bin/wc
        LS='/bin/ls -1'
        BASENAME=/bin/basename
        FIND=/bin/find
        XARGS=/usr/bin/xargs;;
  "Linux")
	EXPR=/usr/bin/expr
        ECHO=echo
        WC=/usr/bin/wc
        LS='/bin/ls -1'
        BASENAME=/usr/bin/basename
        FIND=/usr/bin/find
        XARGS=/usr/bin/xargs;;
esac
MORE='/usr/bin/less -E'

DEBUG=
#DEBUG=$ECHO

EOD=`$EXPR $NOW + 86399`

padding='                    '
triming='....................'
trimlen=`$EXPR $triming : '.*'`
if [ $# -gt 0 ]
then
	case $1 in
	-*)	function=$1
		shift
		;;
	*)	function='-display'
                ;;
	esac
else
	function='-list'
fi
case $function in
-anynew|-anynewcode|-any|-anycode)
  case $function in
  -anynew|-anynewcode)
    if [ -f $HOME/.Info ]
    then
      Restriction="-newer $HOME/.Info"
    else
      Restriction=""
    fi
    ;;
  *)
    Restriction=""
    ;;
  esac
  cd $WHERE
  count=`$FIND * -type f $Restriction -print 2>/dev/null | $WC -l`
  case $function in
  -anynewcode|-anycode)
    if [ $count -eq 0 ]
    then
      exit 0
    else
      exit 1
    fi
    ;;
  -anynew)
    if [ $count -eq 0 ]
    then
      $ECHO "There are no new articles."
    else
      if [ $count -eq 1 ]
      then
        $ECHO "There is a new article."
      else
        $ECHO "There are `$EXPR $count + 0` new articles."
      fi
    fi
    ;;
  -any)
    if [ $count -eq 0 ]
    then
      $ECHO "There are no articles."
    else
      if [ $count -eq 1 ]
      then
        $ECHO "There is an article."
      else
        $ECHO "There are `$EXPR $count + 0` articles."
      fi
    fi
    ;;
  *)
    ;;
  esac
  exit 0
  ;;
-list|-new)	
  if [ "$function" = "-new" -a -f $HOME/.Info ]
  then
    Restriction="-newer $HOME/.Info"
  else
    Restriction=""
  fi
  cd $WHERE
  count=`$FIND * -type f $Restriction -print 2> /dev/null | $WC -l`
  ( if [ $count -eq 0 ]
    then
      $ECHO "There are no available articles."
      exit 0
    fi
    if [ $count -eq 1 ]
    then
      $ECHO "This is the available article."
    else
      $ECHO "These are the available articles."
    fi
    $ECHO "  To read an article, enter: Info Tag"
    if [ $count -gt 15 ]
    then
      $ECHO "\tIf you see More, enter space to continue or q to quit."
    fi
    $ECHO "Tag                  Time posted  Headline --------"
    for article in `$FIND * -type f $Restriction -print | \
		$XARGS $LS -t`
    do
      if [ `$EXPR "$article" : '.*'` -le $trimlen ]
      then
        padded=`$EXPR "$article$padding" : "\($triming\).*"`
      else
        padded="$article"
      fi
      set -- `$LS -l $article`
      stamp=`$EXPR "$6 $7 $8   " : '\(............\).*'`
      exec <$article
      read line
      $ECHO "$padded $stamp $line"
    done
  ) | $MORE
  if [ "$function" = "-new" ]
  then
    /bin/rm -f $HOME/.Info
    /bin/cp /dev/null $HOME/.Info
  fi
  ;;
-display)
  cd $WHERE
  if [ $# -gt 0 ]
  then
    if [ -z "$PAGER" ]
    then
      $MORE $*
    else
      $PAGER $*
    fi
  fi
  ;;
-add|-replace|-insert)	
  function=`$EXPR "$function" : '.\(.*\)'`
  if [ \! -w $WHERE ]
  then
    $ECHO "You are not authorized to $function articles!"
    exit 1
  fi
  $ECHO "An empty answer to any question terminates $function function."
  $ECHO "Enter article name (tag):"
  read article
  if [ -z "$article" ]
  then
    exit 0
  else
	article=`echo $article | tr ' ' '_'`
  fi
  if [ "$function" = "add" ]
  then
    while [ `$EXPR "$article" : '.*'` -gt $trimlen -o \
                 -f $WHERE/$article ]
    do
      if [ `$EXPR "$article" : '.*'` -gt $trimlen ]
      then
        $ECHO "Enter $trimlen character or less article name:"
      else
        $ECHO "Enter new article name:"
      fi
      read article
      if [ -z "$article" ]
      then
        exit 0
      fi
    done
  else
    if [ "$function" = "replace" ]
    then
      while [ `$EXPR "$article" : '.*'` -gt $trimlen -o \
                   \! -f $WHERE/$article ]
      do
        if [ `$EXPR "$article" : '.*'` -gt $trimlen ]
        then
          $ECHO "Enter $trimlen character or less article name:"
        else
          $ECHO "Enter existing article name:"
        fi
        read article
        if [ -z "$article" ]
        then
          exit 0
        fi
      done
    else
      while [ `$EXPR "$article" : '.*'` -gt $trimlen -o ]
      do
        $ECHO "Enter $trimlen character or less article name:"
        read article
        if [ -z "$article" ]
        then
          exit 0
        fi
      done
    fi
  fi
  $ECHO "Enter article headline (title):"
  read headline
  if [ -z "$headline" ]
  then
    exit 0
  fi
  $ECHO "Enter article expiration date, 'std' (for standard) or 'none':"
  read date
  if [ -z "$date" ]
  then
    exit 0
  fi
  date=`$ECHO "$date" | /usr/bin/tr '[A-Z]' '[a-z]'`
  case $date in
  none|never)
    date=Never
    ;;
  std|standard)
    date=`/usr/local/bin/getdate $STANDARD`
    ;;
  *)
    date=`/usr/local/bin/getdate $date`
    datesecs=`/usr/local/bin/getdate in secs, $date`
    while [ "$date" = "error" -o $datesecs -lt $EOD ]
    do
      $ECHO 'Enter valid article expiration data:'
      read date
      if [ -z "$date" ]
      then
        exit 0
      fi
      date=`$ECHO "$date" | /usr/bin/tr '[A-Z]' '[a-z]'`
      case $date in
      none|never)
	date=Never
        ;;
      std|standard)
        date=`/usr/local/bin/getdate $STANDARD`
        ;;
      *)
        date=`/usr/local/bin/getdate $date`
        datesecs=`/usr/local/bin/getdate in secs, $date`
        ;;
      esac
    done
    ;;
  esac
  $ECHO "Enter article text filename:"
  read filename
  if [ -z "$filename" ]
  then
    exit 0
  fi
  while [ \! -r $filename ]
  do
    $ECHO "Enter file name of readable file to obtain article text from:"
    read filename
    if [ -z "$filename" ]
    then
      exit 0
    fi
  done
  ( $ECHO "$headline"
    if [ "$date" != "none" ]
    then
      $ECHO "Expires: $date"
    fi
    $ECHO " "
    /bin/cat $filename
  ) > $WHERE/$article
  $ECHO "Article name (tag):      " $article
  $ECHO "Article headline (title):" $headline
  $ECHO "Article expires:         " $date
  $ECHO "Article text from:       " $filename
  ;;
-Add|-Replace|-Insert)	
  function=`$EXPR "$function" : '.\(.*\)' | /usr/bin/tr '[A-Z]' '[a-z]'`
  if [ \! -w $WHERE ]
  then
    $ECHO "You are not authorized to $function articles!"
    exit 1
  fi
  article="$1"
  if [ -z "$article" -o `$EXPR "$article" : '.*'` -gt $trimlen ]
  then
    $ECHO 'Article name length is zero or too long.'
    exit 0
  fi
  if [ "$function" = "add" ]
  then
    if [ -f $WHERE/$article ]
    then
      $ECHO 'Article exists.'
      exit 0
    fi
  else
    if [ "$function" = "replace" ]
    then
      if [ \! -f $WHERE/$article ]
      then
        $ECHO 'Article does not exist.'
        exit 0
      fi
    fi
  fi
  headline="$2"
  if [ -z "$headline" ]
  then
    $ECHO 'Article headline length is zero.'
    exit 0
  fi
  date="$3"
  if [ -z "$date" ]
  then
    $ECHO 'Article expiration date length is zero.'
    exit 0
  fi
  date=`$ECHO "$date" | /usr/bin/tr '[A-Z]' '[a-z]'`
  case $date in
  none|never)
    date=Never
    ;;
  std|standard)
    date=`/usr/local/bin/getdate $STANDARD`
    ;;
  *)
    date=`/usr/local/bin/getdate $date`
    datesecs=`/usr/local/bin/getdate in secs, $date`
    if [ "$date" = "error" -o $datesecs -lt $EOD ]
    then
      $ECHO 'Article expiration date is invalid.'
      exit 0
    fi
    ;;
  esac
  filename="$4"
  if [ -z "$filename" ]
  then
    $ECHO 'Article text filename length is zero.'
    exit 0
  fi
  if [ \! -r $filename ]
  then
    $ECHO 'Article text file is unreadable.'
    exit 0
  fi
  ( $ECHO "$headline"
    if [ "$date" != "none" ]
    then
      $ECHO "Expires: $date"
    fi
    $ECHO " "
    /bin/cat $filename
  ) > $WHERE/$article
  $ECHO "Article name (tag):      " $article
  $ECHO "Article headline (title):" $headline
  $ECHO "Article expires:         " $date
  $ECHO "Article text from:       " $filename
  ;;
-del|-delete)
  if [ \! -w $WHERE ]
  then
    $ECHO "You are not authorized to delete articles!"
    exit 1
  fi
  if [ $# -gt 0 ]
  then
    cd $WHERE
    $DEBUG /bin/rm $*
  else
    $ECHO "Enter article name:"
    read article
    while [ `$EXPR "$article" : '.*'` -gt $trimlen -o \
                 \! -f $WHERE/$article ]
    do
    $ECHO "Enter $trimlen character or less old article name:"
      read article
    done
    $DEBUG /bin/rm $WHERE/$article
  fi
  ;;
-purge)
  if [ \! -w $WHERE ]
  then
    $ECHO "You are not authorized to purge articles!"
    exit 1
  fi
  old=`/usr/local/bin/getdate in secs, 00:00:00 $OLD`
  if [ "$NOW" = "error" ]
  then
    $ECHO "Unable to obtain current beginning-of-day value!"
    exit 1
  fi
  cd $WHERE
  for i in *
  do
    expires=`/bin/grep '^Expires:' $i`
    if [ -n "$expires" ]
    then
      expires=`$EXPR "$expires" : 'Expires: *\([^ ].*\)'`
      if [ -n "$expires" ]
      then
        if [ "$expires" != "Never" ]
        then
          expires=`/usr/local/bin/getdate in secs, $expires`
          if [ "$expires" = 'error' ]
          then
            $ECHO "$i has an invalid Expires: clause!"
          else
            if [ $expires -lt $NOW ]
            then
              $DEBUG /bin/rm $i
            fi
          fi
	fi
      else
        $ECHO "$i has a null Expires: clause!"
      fi
    else  # No Expires clause
      set -- `$LS -l $i`
      written=`/usr/local/bin/getdate in secs, $6 $7 $8`
      if [ "$written" != "error" ]
      then
        if [ $written -lt $old ]
        then
          $DEBUG /bin/rm $i
        fi
      fi
    fi
  done
  exit 0
  ;;
*)
    if [ -z "$PAGER" ]
    then
	pager=$MORE
    else
	pager=$PAGER
    fi
  $pager <<!EOF!
Info may take any one of the following arguments as requests:

  -help           requests this help text
  tag(s)          requests presentation of selected articles by tag
  -display tag(s) requests presentation of selected articles by tag
  -anynew         requests message about number of new articles
  -any            requests message about number of articles
  -anynewcode     requests exit status about number of new articles
  -anycode        requests exit status about number of articles
  -new            requests list of new articles
  -list           requests list of articles
  -add            requests prompting to add new article from text file
  -replace        requests prompting to replace existing article from text file
  -insert         requests prompting to insert article from text file
  -Add     tag headline expiredate textfile
  -Replace tag headline expiredate textfile
  -Insert  tag headline expiredate textfile
                  requests corresponding batch operation
  -del            requests the removal of a specific article by tag
  -delete         requests the removal of a specific article by tag
  -del tag        requests the removal of a specific article by tag
  -delete tag     requests the removal of a specific article by tag
  -purge          requests the removal of expired articles

  If none of the above are supplied, the default is -list.

  A tag is a short (1 to $trimlen character) article identifier.
  A headline is a short (1 to 32 character) description of the contents.
  A expire date is a date (see getdate), 'standard' (or 'std'), or 'never'.
  The expiration date can not be earlier than today.
  A text file is a existing readable readable file from which the text
  for an article is to be copied.

  A new article has a time stamp newer than your \$HOME/.Info file.
  If you do not have a \$HOME/.Info file then all articles are new.

  The article modification operations (-add, -replace, -insert, -Add, -Replace,
  -Replace, -Insert -delete and -purge) requires that you have permission to 
  change the $WHERE directory.
!EOF!
  ;;
esac

