  #!/bin/sh 
  # transnews: connects to a free Ascend server of the LRZ.
  # Author:    Bernhard Hailer <dl4mhk@lrz.uni-muenchen.de>
  # Version:   3.0 (01-Sep-96)

  . /etc/i4l.config
  . /etc/i4l.param

  cd /                                # against PATH=. effect 

  echo -----------------------------------------------------------------------
  echo -e "Starting news transfer: \c"; date

  declare -i con_apps                 #  line already active?
  read con_apps </etc/lines.active
  let con_apps+=1
  echo $con_apps >/etc/lines.active

  line="0"

  while [ $line!="finished" ]
  do

    if [ $con_apps -lt 2 ]
    then
      echo "Calling ippp$line"
      /sbin/isdnctrl dial ippp$line
      echo "Sleeping 8 sec for PPP handshaking..."
      sleep 8s
      /sbin/route add default ippp$line
      echo "Line open - checking..."  
    else
      let con_apps-=1
      echo "Line seems to be open already $con_apps time(s) - checking..."
      let con_apps+=1
    fi

    echo "Checking Server..."
    set `ping -qc5 -i1 prvdr$line | grep transmitted`
    if [ $4 -gt 0 ];
    then
      echo ":-) Dial-in server running - checking news server..."
      set `ping -qc5 -i1 getnews | grep transmitted`
      if [ $4 -gt 0 ];
      then
        echo "8-) News server online - starting news poll"  
        echo "Sending news..."
        su news -c "/usr/lib/news/bin/sendnews"
        echo "Again sleeping 2 sec for PPP handshaking"
        sleep 2
        echo "Fetching news..."
        su news -c "/usr/lib/news/bin/newsflash -v"    
        echo "Again sleeping 2 sec for PPP handshaking"
        sleep 2
        line="finished"                                   # paranoia setting
        break
      else
        echo ":-[ Sorry - news server is down. Try later..."
        line="finished"                                   # paranoia setting
        break
      fi
    else
      /sbin/isdnctrl hangup ippp$line
      route del default
      read con_apps </etc/lines.active
      echo "0" >/etc/lines.active
      if [ $con_apps -gt 0 ]
      then
        echo ":-| Hmm, no. Trying to rebuild..."
        let con_apps=1
        echo $con_apps >/etc/lines.active
        line="0"                                        # paranoia setting
        continue
      else
        case $line in
        0)
        if [ $PROV_NAME_2 = "" ]
        then
          echo ":-[ Sorry, Line is down. Bad luck... Try later!"
          line="finished"
          echo "0" >/etc/lines.active
          con_apps=1
          break
        else
          echo ":-( Sorry, Line 0 is down. Trying second line."
          line="1"
          continue
          fi
          ;;
        1)
          echo "0" >/etc/lines.active
          con_apps=1
          echo ":-[ Sorry, Line 1 is also down. Bad luck... Try later!"
          line="finished"
          break
          ;;
        *)
          echo ":-0 THIS CANNOT HAPPEN. Fatal Error in script /sbin/transnews!"
          break
          ;;
        esac
      fi
    fi
  done

  read con_apps </etc/lines.active
  let con_apps-=1
  echo $con_apps >/etc/lines.active
  if [ $con_apps -eq 0 ]
  then 
    echo "Last application stopped - closing line."
    /sbin/isdnctrl hangup ippp0
    /sbin/isdnctrl hangup ippp1
    /sbin/route del default         #  and delete route.
  else
    echo "There are $con_apps more applications running - keeping line."
  fi  

  echo -e "Finished news transfer: \c"; date 
