#include "includes.h"
#include "uucp.h"
#include "sysdep.h"
#include "modem.h"

#define HAYES  /* I have a Hayes compatable */

#ifdef HAYES

int twrite();
int instr();

void set_baud();

void openline()
{
    signal(SIGINT,sigint);
    start_again:
    chkabort();
    if (instr("CONNECT",7))
      goto start_again;
    set_baud(get_baud());
    Delay(120); /* sleep 2 seconds */
}


int
get_baud()
{
/* We've seen the CONNECT message, now we must see what baud rate
   we've connected with */
/* gather input until \r then see if it's 300, 1200, or 2400 */
/* this is for hayes compatibles */

        int data;
        char rate[10];
        int rate_inx = 0;

        DEBUG(2,"looking for baud rate\n",0);

        while ( ((data = xgetc()) != EOF) && ((char)data != '\r')) {
            if ((char)data == ' ') continue;
            rate[rate_inx++] = (char)data;
        }
        DEBUG(2, "found baud rate of %s\n", rate);
        if (strncmp(rate,"1200",4) == 0) return 1200;
        if (strncmp(rate,"2400",4) == 0) return 2400;
        if (rate_inx == 0) return 300;
        return 1200;  /* default */
}

void modem_init()
{
        char init[30];

        reset_modem();

        sprintf(init, "%s\r", "ATS2");

        twrite(init, strlen(init));

        Delay(60);
}


/*
 * Simple dialer routine.  Needs replacement with a full blown
 * script driven dialer.  Next week maybe :-).  FIXME.
 */
int
dial_nbr(nbr)
        char  *nbr;
{
        char  dial[30];
        int   i;

        sprintf(dial, "%s%s\r", "ATDT", nbr);

        DEBUG(2,"dialing %s\n", dial);

        twrite(dial, strlen(dial));

        i = instr("CONNECT", 7);

        return (i);
}

void reset_modem()
{
    set_baud(2400);
    twrite("+++", 3);
    Delay(60);
    twrite("ATH0\r", 5);
    Delay(120);
    twrite("ATZ\r", 4);
    Delay(60);
    twrite("ATZ\r", 4);
}

#endif
