#!/usr/local/bin/python
# forwarding-engine for vjbox. 
# (c) 1997 dg4iad

import posix,sys,signal,time,os,string

config={}

# first we create two pipes
p1=posix.pipe()
p2=posix.pipe()

def sig_child_handler(sig,param):
  print "SIGCHLD"
  posix.wait()
  sys.exit()

def ax_readline():
  s=""
  ch=posix.read(p2[0],1)
  while (ch!=chr(13)):
    s=s+ch
    ch=posix.read(p2[0],1)
  print "<",s  
  return s  
  
def ax_sendline(line):
  print ">",line
  posix.write(p1[1],line+chr(13))

def do_forward():
  s=ax_readline()
  while string.find(s,"$]")==-1:
   s=ax_readline()
  while ax_readline()!=">": pass
  ax_sendline("[VjBox-0.7d-H$]")
  while ax_readline()!=">": pass
  for bid in bids:
    head=box.header(bid)
    for line in head:
      [key,r=string.splitfi
  
  ax_sendline("F>")
  ax_readline()

def readconfig():
  global config,bids
  for line in open(sys.argv[1].readlines():
    l=string.split(line)
    config[string.lower(l)]=l[1:]
  bids=open(config["queue"]).readlines():

# then we make children ;)

pid=posix.fork()
if pid==0:
  # a child is born
  # close one end of each pipe
  posix.close(p1[1])
  posix.close(p2[0])
  # and make the pipes our stdin/stdout
  posix.dup2(p1[0],0)
  posix.dup2(p2[1],1)
  # now we start the ax25_call program to establish the connection...
  os.execl("/usr/sbin/ax25_call","ax25_call","wampes","dg4iad-5","dg4fdi-8","dg4iad-10")
else:
  # ok, we are the proud father
  signal.signal(signal.SIGCHLD,sig_child_handler)
  posix.close(p1[0])
  posix.close(p2[1])
   # getting children is hard, so we sleep some seconds...
  time.sleep(2)
  # then we start working...
  do_forward()
  #print posix.read(p2[0],1024)
  posix.kill(pid,signal.SIGTERM)
  time.sleep(2)

