#!/bin/sh
# <BEGIN COPYRIGHT>
# This file is part of the "Bad Penguin" GNU/Linux distribution
# Copyright (C) 1997-98-99 by Antonio GALLO
# Home Page at ......: http://www.badpenguin.org
# Home Page at ......: http://www.linux.it/~agx/
# e-mail contact ....: mailto:agx@linux.it	mailto:agx@geocities.com
# This program is distributed under the GNU General Public License
# You are not allowed to remove the copyright notice
# <END COPYRIGHT>

# Script description goes here
# 1999-11-29, Version 1.1.0 by AGX

disconnect
clear
say "Connecting to peoples !"  > /dev/null &

# Check Parameters
PURGE=0
REPOST=0
SITECOPY=0
LASTWAIT=0
EXPIRE=""

while [ ! "$1" = "" ]; do
  case $1 in
    --purge)
      PURGE=1
      echo "purge mode ON"
      ;;
    --repost)
      REPOST=1
      echo "repost mode ON"
      ;;
    --sitecopy)
      SITECOPY=1
      echo "sitecopy ENABLED"
      ;;
    --wait)
      LASTWAIT=1
      echo "LAST WAIT OK"
      ;;
    --expire)
      EXPIRE="--expire"
      echo "EXPIRE NEWS OK"
      ;; 
  esac
  shift 1
done



# --- Start unusefull loggers ------------------ 
if [ "`pidof tcplogd`" = "" ]; then
  echoc GREEN starting tcp logger daemon
  tcplogd
fi

if [ "`pidof icmplogd`" = "" ]; then
  echoc GREEN starting icmp logger daemon
  icmplogd
fi



# --- Purge / Repost --------------------------------

if [ $REPOST -eq 1 ]; then
  for i in /var/spool/slrnpull/out.going/rejects/*; do
    if [ -f $i ]; then
      echoc CYAN "Re-posting article $i"
      mv $i /var/spool/slrnpull/out.going/`basename $i`
    fi
  done
else
  for i in /var/spool/slrnpull/out.going/rejects/*; do
    [ -f $i ] && echoc YELLOW "There are rejected message. Use --repost or --purge"
    break
  done
fi
if [ $PURGE -eq 1 ]; then
  echoc CYAN Purging rejected files ...
  rm -f /var/spool/slrnpull/out.going/rejects/*
fi

sleep 2

# --- Connect loop -----------------------------
REPLY=1
TRIES=1
while [ $REPLY -ne 0 ]; do
  echoc CYAN "Tentativo nr. $TRIES"
  connect peoples NOWAIT
  REPLY=$?
  if [ $REPLY -ne 0 ]; then
    TRIES=$[ TRIES + 1 ]
    WAIT=$[ TRIES * 60 ]
    echo
    echoc YELLOW "`date`: Prossimo tentativo tra $WAIT secondi."
    echo
    sleep $WAIT
  fi
done

# --- Mail ------------------------
postfix start

# --- SITECOPY --------------------
if [ $SITECOPY -eq 1 ]; then
  echoc CYAN Staring sitecopy -ua
  sitecopy -ua &
fi


say "Downloading e-mail and neews"  > /dev/null &
> .procmail.log
fetchmail -l 50000 --mda "formail -s procmail"  &


# --- News ------------------------
fetchnews $EXPIRE

# --- I keep sendmail at the end because it need postfix to start
sleep 2
sendmail -bp
sendmail -q

REPLY=1
while [ $REPLY -eq 1 ]; do
	echo "Sending e-mail ..."
	sleep 20
	sendmail -bp | grep -q "Mail queue is empty"
	REPLY=$?
done



# --- Quit -------------------------
say "e-mail and Neews have been downloaded "  > /dev/null &
echoc CYAN newsgroup completed, waiting for fetchmail ...
wait `pidof fetchmail`

if [ $SITECOPY -eq 1 ]; then
	echoc CYAN newsgroup completed, waiting for sitecopy ...
	wait `pidof sitecopy`
fi

# --- LastKey ----------------------
if [ $LASTWAIT -eq 1 ]; then
  say "press any key to continue"  > /dev/null
  read x
fi

# --- Disconnect --------------------
say "Disconnecting from peoples"  > /dev/null &
disconnect
