#!/bin/sh
#
#  ** Script package for Suck & INN **
#          Read Manual for more
#
# subscribe by Peter Sobisch <petersob@gmx.net>
# Syntax: subscribe
#
# Description: read the newsrc files from all user's home directory
#              and rebuilds the suckrc file
#

#
# defaults, may be changed in the env file
#
NEWSRC=yes		# enables .newsrc file
NEWSRC_KRN=no		# enables krn (KDE) subscription file
NEWSRC_XSP4SI=no	# enables xsp4si subscription file 
LAST=-10	# last messages to suck 

#
# apply env
#
if [ -s $(dirname $0)/env ]; then               # look for env file
   . $(dirname $0)/env                          # include environment
else
   echo "** ERROR: File $(dirname $0)/env not found !"
   echo "Check your config file or rerun the install.sh Script."
   exit 1
fi
BASENAME=`basename $0`  # don't touch this !

#
# parse command line parameter
#
if [ $# -ne 0 ]; then
   case $1 in
      -? | -h | --help)
         echo "** Script package for Suck & INN (sp4si) **"
         echo "NOTE: must be run as root.";
         echo "Syntax: $BASENAME";
         exit;;
esac
fi
if [ $UID -ne 0 ]; then         # check if run as root
   echo "** ERROR: you must run als root, not '`id -un`'."
   exit 1
fi

#
# set environment
#
NEWSHOSTS=$(awk '!/^#/ && NF>1 { print $1":"$2":"$3":"$4":"$5":"$6; found=1 } \
            END { if (found == 0) exit 1 }' ${BASE_DIR}/newshosts)
if [ $? -ne 0 ]; then
   echo "** ERROR: no valid host entries found. Check your newshosts file."
   exit 1
fi
TMP=/tmp/${BASENAME}.tmp
TOTAL=/tmp/${BASENAME}.total.tmp 
PATTERN=/tmp/${BASENAME}.pattern.tmp
SUBSCRIBING=/tmp/${BASENAME}.subscribing.tmp
[ $LAST -gt -1 ] && LAST=-10    # LAST must be a negative number
#
COM=""				# init command string
# all lines must have a subscribed newsgroups names as result
[ "$NEWSRC"="yes" ] && COM=$COM'cat 2>/dev/null ${HOME_DIR}/.newsrc | cut -s -f1 -d:;'
[ "$NEWSRC_KRN"="yes" ] && COM=$COM'cat 2>/dev/null ${HOME_DIR}/.kde/share/apps/krn/subscribed;'
[ "$NEWSRC_XSP4SI"="yes" ] && COM=$COM'cat 2>/dev/null ${HOME_DIR}/.newsrc-xsp4si;'

#
# define GUP mail content
# 
guptext2 () { cat <<EOF
site $(uuname -l) $PASSWORD
$(awk '{ print "include",$1 }' $SUBSCRIBING)
list
quit
EOF
}

#
# define GUP mail content
#
guptext1 () { cat <<EOF
site $(uuname -l) $PASSWORD
exclude *
quit
EOF
}

#
# lock
#
abort () { echo "Script $BASENAME aborted."; rm -f $LOCK_FILE; exit 1; }
quit () { rm -f $LOCK_FILE; exit 1; }
LOCK_FILE=${SITE_DIR}/LOCK.$BASENAME		# set lock file
trap 'abort' 1 2 3 15
shlock -p $$ -f ${LOCK_FILE}			# lock the run
if [ $? -ne 0 ]; then				# if already running
   echo "$BASENAME is already running, can't run two at one time"
   exit 1
fi

#
# main
#

cp /dev/null $TOTAL	# collect all subscribing
echo -n "Look for subscribed newsgroups"
for HOME_DIR in `cut -f6 -d: /etc/passwd`; do	# for each home directory
   echo -n "."
   eval $COM >>$TOTAL	# invoke command line
done
echo "done."

if [ ! -s $TOTAL ]; then		# exiting if nothing subscribed
   echo "Nothing subscribed. Script aborted."
   quit
fi

for LINE in $NEWSHOSTS; do
   SITE=$(echo $LINE | cut -f1 -d:)		# expose site
   if [ -s "${SITE_DIR}/${SITE}/spool" ]; then	# spool file is required
      echo -n "Subscribing for $SITE."
      awk '{ print "^"$1"$" }' $TOTAL >$PATTERN	# pattern for global subscribing
      echo -n "."
      egrep -f $PATTERN ${SITE_DIR}/${SITE}/spool >$SUBSCRIBING		# create sites subscribing
      echo -n "."
      case $(echo $LINE | cut -f3 -d:) in	# select spool type
         #
         # UUCP subcribing 
         #
         uucp|uucp/uucp|nntp/uucp)   		# for uucp download
            GUP=$(echo $LINE | cut -f5 -d:)	# expose email gup destination address
            PASSWORD=$(echo $LINE |cut -f6 -d:)	# expose the gup password
            if [ -n "$GUP" -a -n "$PASSWORD" ]; then
               # only be sure the file exists uucpnewsrc file 
               touch ${SITE_DIR}/${SITE}/uucpnewsrc
               diff -q $SUBSCRIBING ${SITE_DIR}/${SITE}/uucpnewsrc 1>/dev/null 2>&1
               if [ "$?" -ne "0" ]; then
                  guptext1 | mail $GUP
                  guptext2 | mail $GUP                # send mail to remote gup for subscribing
                  echo "subscription changed, mail is sent."
               else
                  echo "subcription not changed, no mail sent."
               fi
               cat $SUBSCRIBING >${SITE_DIR}/${SITE}/uucpnewsrc
               chown news.news ${SITE_DIR}/${SITE}/uucpnewsrc
            else
               echo "WARNING: none uucp gup or password set. UUCP subscribing not possible"
               echo "Check your newshosts file"
            fi;;
         #
         # suck subscribing
         #
         nntp|nntp/nntp|uucp/nntp|*)			# for nntp download
            echo -n "creating sucknewsrc."
            touch ${SITE_DIR}/${SITE}/sucknewsrc	# create a file if not exists
            echo -n "."
            awk '{ print "^"$1"[ ]" }' $SUBSCRIBING >$PATTERN	# pattern for SUCKNEWSRC: ^newsgroup[ ]
            echo -n "."
            egrep -f $PATTERN ${SITE_DIR}/${SITE}/sucknewsrc >$TMP	# already subscribed groups
            echo -n "."
            awk '{ print "^"$1"$" }' $TMP >$PATTERN	# create pattern for already subscribed groups
            echo -n "."
            if [ -s "$PATTERN" ]; then
               egrep -vf $PATTERN $SUBSCRIBING | awk '{ print $0, "'"$LAST"'" }' >>$TMP
            else
               # because if no PATTERN, all must be matched
               awk '{ print $0, "'"$LAST"'" }' $SUBSCRIBING >>$TMP
            fi
            echo -n "."
            mv -f $TMP ${SITE_DIR}/${SITE}/sucknewsrc
            chown news ${SITE_DIR}/${SITE}/sucknewsrc
            chgrp news ${SITE_DIR}/${SITE}/sucknewsrc
            echo "done.";;
      esac
   else
      echo "No ${SITE_DIR}/${SITE}/spool file found. Site skipped."
      echo "Run 'get.active' first."
   fi
done
rm -f $TMP $SUBSCRIBING $PATTERN $TOTAL

quit
