#!/bin/bash

NameMod=ttyS1
SpeedMod=38400
IdleConn=60

USER=fidonet
FInb='/var/spool/fidonet/inb'
FOutb='/var/spool/fidonet/outb'
MyLog='/var/log/fidonet/my.log'
NODE1="f1.n4646.z2.fidonet.org"
NODE2="f12.n4646.z2.fidonet.org"

IPTeleph=(61999 92259 91263 67235 99262 96786)
NumbIPTeleph=6  

time_APull=7200              # سإث
time_try=180                 # سإث
#time_try1=1800
Hpath='/usr/local/bin/'
Cycle=1                      # سإث

try_t1=0
try_t2=0
try_t3=0
work=0
comand=0
Npoll_t=0
Npoll_t1=0
Test=0
i=0
LastDay=$(date +%d);

su $USER -c "/usr/local/sbin/binkd -c /etc/fidonet/binkd.cfg &"

function dial_IP ()
{
 echo dial_IP 
 if [ $Npoll_t1 != "0" ]; then let Npoll_t1=Npoll_t1-1; return 1; fi
 let try_c=try_c+1;
 if [ $try_t3 != "0" ]; then let try_t3=try_t3-1; return 1; fi
 chk_time 23 7; 
 if [ $? != "0" ]; then return 1; fi
 try_t3=$time_try/$Cycle;
 setsid /usr/sbin/pppd -d -detach /dev/$NameMod idle $IdleConn $SpeedMod connect "/etc/ppp/scripts/dialer_PPP 1 ${IPTeleph[ $[$try_c%$NumbIPTeleph] ]}" &
 sleep $Cycle; i=0;
 if [ ! -e $FOutb/01d015b3.ilo ]; then su $USER -c "touch $FOutb/01d015b3.ilo"; fi
 if [ ! -e $FOutb/139c1159.ilo ]; then su $USER -c "touch $FOutb/139c1159.ilo"; fi
 if [ ! -e $FOutb/12260001.ilo ]; then su $USER -c "touch $FOutb/12260001.ilo"; fi
 echo `date "+%d.%m.20%y %H:%M:%S"`: Try conecting to Internet: ${IPTeleph[ $[$try_c%$NumbIPTeleph] ]}  >> $MyLog
 while [ -e "/var/lock/LCK..$NameMod" ]; do
#  chk_time 2 7;
  if [ -e "$FOutb/01d015b3.bsy" ] || [ -e "$FOutb/139c1159.bsy" ] || [ -e "$FOutb/12260001.bsy" ]; 
  then 
   if [ $i != "0" ]; then `/usr/sbin/ntpdate bear.zoo.bt.co.uk >> $MyLog`; fi   
   i=0; 
  else 
#   echo i= $i
   let i=i+1; 
   if [ $i = $IdleConn ]; then 
    if [ ! -e $FOutb/01d015b3.ilo ] && [ ! -e $FOutb/139c1159.ilo ] || [ ! -e $FOutb/12260001.ilo ] ; 
    then 
     Npoll_t1=$time_APull/$Cycle;
     echo `date "+%d.%m.20%y %H:%M:%S"`: True try ${IPTeleph[ $[$try_c%$NumbIPTeleph] ]} >> $MyLog;
     try_c=0; 
    fi
    kill -s TERM `cat /var/lock/LCK..$NameMod`;     
    return 0; 
   fi
  fi
  sleep $Cycle; 
 done
 return 1;
}

function dial ()
{
 echo dial 
 if [ $Npoll_t != "0" ]; then let Npoll_t=Npoll_t-1; fi 
# ********* NODE 1 **************
 chk_time 20 7; 
 if [ $? = "0" ]; then 
  if [ $Npoll_t = "0" ] || [ $(find $FOutb -maxdepth 1 -iname '12260001.c*' | wc -w) != "1" ]; then 
   if [ $try_t1 != "0" ]; then let try_t1=try_t1-1; return 1; fi
   try_t1=$time_try/$Cycle; su $USER -c "/usr/sbin/ifcico -r1 $NODE1";
   if [ $? = "0" ]; then let Npoll_t=$time_APull/$Cycle; fi
   return 0
  fi      
 fi 
# ********* NODE 2 **************
 chk_time 23 5; 
 if [ $? = "0" ]; then 
  if [ $(find $FOutb -maxdepth 1 -iname '1226000c.c*' | wc -w) != "1" ] ; then 
   if [ $try_t2 != "0" ]; then let try_t2=try_t2-1; return 1; fi
   try_t2=$time_try/$Cycle; su $USER -c "/usr/sbin/ifcico -r1 $NODE2"; return 0;
  fi
 fi 

 return 1
}

function chk_time ()
{
 if [ $1 -gt $2 ]; then 
  if [ $(date +%k) -ge $1 ] || [ $(date +%k) -lt $2 ]; then return 0; fi
 else 
  if [ $(date +%k) -ge $1 ] && [ $(date +%k) -lt $2 ]; then return 0; fi
 fi
 return 1
}

function scaninb ()
{
 echo scan

 if [ ! $(find $FInb -maxdepth 1 -iname '*.tic' | wc -w) -eq 0 ]; then 
  su $USER -c "${Hpath}htick toss"; 
 fi
 if [ ! $(find $FInb -maxdepth 1 -iname '*.mo?' | wc -w) -eq 0 ] || 
    [ ! $(find $FInb -maxdepth 1 -iname '*.tu?' | wc -w) -eq 0 ] ||
    [ ! $(find $FInb -maxdepth 1 -iname '*.th?' | wc -w) -eq 0 ] ||
    [ ! $(find $FInb -maxdepth 1 -iname '*.fr?' | wc -w) -eq 0 ] ||
    [ ! $(find $FInb -maxdepth 1 -iname '*.we?' | wc -w) -eq 0 ] ||
    [ ! $(find $FInb -maxdepth 1 -iname '*.sa?' | wc -w) -eq 0 ] ||
    [ ! $(find $FInb -maxdepth 1 -iname '*.su?' | wc -w) -eq 0 ]; then 
  su $USER -c "${Hpath}hpt toss"; 
  su $USER -c "${Hpath}hpt pack";
 fi
 if [ ! $(find $FInb -maxdepth 1 -iname '*.pkt' | wc -w) -eq 0  ]; then 
  su $USER -c "${Hpath}hpt toss"; 
  su $USER -c "${Hpath}hpt afix"; 
  su $USER -c "${Hpath}htick scan"; 
  su $USER -c "${Hpath}hpt pack";
 fi
}

if [ $# = 1 ]; then comand=$1; fi 
if [ $comand = 'scan' ]; then scaninb; exit; fi  

if [ $comand -eq 0 ]; then
 while [ "1" = "1" ]; do
  work=0;
  if [ $(date +%w) -eq 0 ] || [ $(date +%w) -eq 6 ]; then
   chk_time 19 9;  if [ $? = "0" ]; then work=1; fi
  else 
   chk_time 20 8;  if [ $? = "0" ]; then work=1; fi
  fi 
  echo work $work

  if [ ! $(date +%d) -eq $LastDay ]; then 
	LastDay=$(date +%d);
   	su $USER -c "${Hpath}sqpack"; echo "Purg Fido BASA" >> $MyLog; 
   	su $USER -c "${Hpath}hptlink"; echo "Link Fido BASA" >> $MyLog; 
  fi
  if [ $work  = '1' ] && [   -e "/etc/nologin.$NameMod" ]; then rm -f /etc/nologin.$NameMod; fi
  if [ $work != '1' ] && [ ! -e "/etc/nologin.$NameMod" ]; then touch /etc/nologin.$NameMod; fi 
  if [ ! -e "/var/lock/LCK..$NameMod" ]; then dial; dial_IP; scaninb; fi 
  sleep $Cycle
 done
fi

exit 0