  #!/bin/sh
  # transmail.sh: exchanges email with the provider.
  # 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 mail transfer: \c"; date

  declare -i con_apps                 #  makes it comfortable
  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 server..."
    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 mail server..."
      set `ping -qc5 -i1 getmail | grep transmitted`
      if [ $4 -gt 0 ];
      then
        echo "8-) Mail server online - starting mail exchange"  
        echo "Sending mail..."
        sendmail -q
        echo "Again sleeping 2 sec for PPP handshaking"
        sleep 2
        echo "Fetching mail..."
        popclient -3 -u $MAIL_LOGIN -p $MAIL_PASSWD -o $MAIL_PATH getmail
        echo "Again sleeping 2 sec for PPP handshaking"
        sleep 2
        line="finished"                                   # paranoia setting
        break
      else
        echo ":-[ Sorry - mail 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 ":-[ Sorry, Line 1 is also down. Bad luck... Try later!"
          line="finished"
          echo "0" >/etc/lines.active
          con_apps=1
          break
          ;;
        *)
          echo ":-0 THIS CANNOT HAPPEN. Fatal Error in script /sbin/transmail!"
          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

  # allow dl4mhk access to mail:
  chown dl4mhk.users /var/spool/mail/dl4mhk

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