#!/bin/sh
###############################################################################################
#
#  PPP Reconnecter for OpenWRT      Version 1.0     (c)2012 by Volker S. Latainski
#
###############################################################################################
#
#  This program is free software; you can redistribute it and/or modify it under the terms
#  of the GNU General Public License as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
#  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with this program;
#  if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110, USA
#
#  See also http://www.gnu.org/licenses/gpl-3.0.txt
#
###############################################################################################

# public settings (feel free to change)
TIMEOUT=5 #timeout to exit
MAXTRY=5 #max. attempts to connect before die

# private settings (better don't touch them :P)
ATTEMPTS=0 #start with 0 is recommended
PIDFILE="/var/run/ppp-wan.pid" #pid file

# include files
. /etc/profile

# local functions
getIP () 
{ 
  echo `/sbin/ifconfig $1 | grep "inet addr:" | awk '{print $2}' | awk -F: '{print $2}'`
}
getPID ()
{
  echo `awk 'NR==1 {print $1}' $1`
}
getINTERFACE ()
{
  echo `awk 'NR==2 {print $1}' $1`
}
connect2PPP ()
{
  until [ `/sbin/ifup wan >/dev/null 2>&1` ]; do
	if [ ! -f "$PIDFILE" ]; then
	  echo -n "."
	else	  
	  break
	fi
	if [ "$ATTEMPTS" -lt "$MAXTRY" ]; then
	  ATTEMPTS=$(($ATTEMPTS+1))
	else
	  echo -e "Error: tried to reconnect $MAXTRY times,...so give it up."
	  closeME # exit
	fi
  done
}
closeME ()
{
  echo -ne "\nExit/Logout in "
  while [ "$TIMEOUT" -gt 0 ]; do
    if [ "$TIMEOUT" -eq 1 ]; then
      echo -ne "$TIMEOUT\n\n"
    else
      echo -ne "$TIMEOUT "
    fi
    sleep 1
	TIMEOUT=$(($TIMEOUT-1))
  done
  exit 1
}

###############################################################################################

# header
echo -ne "\nPPP Reconnecter for OpenWRT\tVersion 1.0\t(c)2012 by xray\n"
echo -e "Released under the GNU/GPL\n"

# an existing connection was detected, let's run our stuff to reconnect
if [ -f "$PIDFILE" ]; then

  # set interface
  INTERFACE=`getINTERFACE $PIDFILE`  
  echo -e "IP now:\t\t\t`getIP $INTERFACE` [PID `getPID $PIDFILE`]"

  # close the existing connection
  echo -ne "Close connection:\t"
  until [ `/sbin/ifdown wan >/dev/null 2>&1` ]; do
	if [ -f "$PIDFILE" ]; then
	  echo -n "."
	else
	  break
	fi
  done 
  echo -e "done!"

  # wait a second
  sleep 1

  # reconnect
  echo -ne "New connection:\t\t"  
  connect2PPP
  echo -e "established!"

  # wait another second
  sleep 1

  # print current IP
  echo -e "New IP:\t\t\t`getIP $INTERFACE` [PID `getPID $PIDFILE`]"

# if there is no connection established, try to connect...
else

  echo -ne "No connection available, try to connect..."
  connect2PPP
  echo -e "done!"

  # wait a second
  sleep 1

  # print current IP
  INTERFACE=`getINTERFACE $PIDFILE`  
  echo -e "New IP:\t`getIP $INTERFACE` [PID `getPID $PIDFILE`]"

fi

# exit
closeME