int dterate;
int dcerate;
int protocol;
int compression;

parseResult(int result)
{
    int retcode = 0;
    
    printf("TellyScript: parseResult -- %d", result);
    
    if (result == 0)
    {
        retcode = 0;
    }
    else if (result == 1)
    {
        dterate = 300;
    }
    else if (result == 3)
    {
        printf("TellyScript: Got NO CARRIER");
        retcode = 8;
    }
    else if (result == 5)
    {
        dterate = 0x4b0;
    }
    else if (result == 6)
    {
        printf("TellyScript: Got NO DIALTONE");
        retcode = 5;
    }
    else if (result == 7)
    {
        printf("TellyScript: Got BUSY");
        retcode = 7;
    }
    else if (result == 8)
    {
        retcode = 7;
    }
    else if (result == 9)
    {
        dterate = 0x258;
    }
    else if (result == 10)
    {
        dterate = 0x960;
    }
    else if (result == 11)
    {
        dterate = 0x12c0;
    }
    else if (result == 12)
    {
        dterate = 0x2580;
    }
    else if (result == 13)
    {
        dterate = 0x1c20;
    }
    else if (result == 14)
    {
        dterate = 0x2ee0;
    }
    else if (result == 15)
    {
        dterate = 0x3840;
    }
    else if (result == 16)
    {
        dterate = 0x4b00;
    }
    else if (result == 17)
    {
        dterate = 0x9600;
    }
    else if (result == 18)
    {
        dterate = 0xe100;
    }
    else if (result == 19)
    {
        dterate = 0x1c200;
    }
    else if (result == 22)
    {
        printf("TellyScript: Got a CONNECT 75TX/1200RX and I don't know why");
        dterate = 75;
    }
    else if (result == 23)
    {
        printf("TellyScript: Got a CONNECT 75TX/1200RX and I don't know why");
        dterate = 0x4b0
    }
    else if (result == 40)
    {
        dcerate = 300;
    }
    else if (result == 44)
    {
        printf("TellyScript: Got a CONNECT 75TX/1200RX and I don't know why");
        dcerate = 0x4b0;
    }
    else if (result == 45)
    {
        printf("TellyScript: Got a CONNECT 75TX/1200RX and I don't know why");
        dcerate = 0x4b0;
    }
    else if (result == 46)
    {
        dcerate = 0x4b0;
    }
    else if (result == 47)
    {
        dcerate = 0x960;
    }
    else if (result == 48)
    {
        dcerate = 0x12c0;
    }
    else if (result == 49)
    {
        dcerate = 0x1c20;
    }
    else if (result == 50)
    {
        dcerate = 0x2580;
    }
    else if (result == 51)
    {
        dcerate = 0x2ee0;
    }
    else if (result == 52)
    {
        dcerate = 0x3840;
    }
    else if (result == 53)
    {
        dcerate = 0x41a0;
    }
    else if (result == 54)
    {
        dcerate = 0x4b00;
    }
    else if (result == 55)
    {
        dcerate = 0x5460;
    }
    else if (result == 56)
    {
        dcerate = 0x5dc0;
    }
    else if (result == 57)
    {
        dcerate = 0x6720;
    }
    else if (result == 58)
    {
        dcerate = 0x7080;
    }
    else if (result == 59)
    {
        dcerate = 0x41a0;
    }
    else if (result == 61)
    {
        dcerate = 0x5460;
    }
    else if (result == 62)
    {
        dcerate = 0x5dc0;
    }
    else if (result == 63)
    {
        dcerate = 0x6720;
    }
    else if (result == 64)
    {
        dcerate = 0x7080;
    }
    else if (result == 66)
    {
        compression = 1;
    }
    else if (result == 67)
    {
        compression = 2;
    }
    else if (result == 69)
    {
        compression = 0;
    }
    else if (result == 76)
    {
        protocol = 0;
    }
    else if (result == 77)
    {
        protocol = 1;
    }
    else if (result == 78)
    {
        dcerate = 0x79e0;
    }
    else if (result == 79)
    {
        dcerate = 0x8340;
    }
    else if (result == 80)
    {
        protocol = 2;
    }
    else if (result == 81)
    {
        protocol = 3;
    }
    else if (result == 84)
    {
        dcerate = 0x8340;
    }
    else if (result == 91)
    {
        dcerate = 0x79e0;
    }
    else 
    {
        printf("TellyScript: Unknown result code %d", result);
        retcode = 9;
    }
    
    return retcode;
}

winkdtr()
{
    setdtr(0);
    delay(30);
    setdtr(1);
    delay(30);
}

main()
{
    int count, i, retries, result;
    int start = ticks();
    int end;
    char *number;
    char buffer[32];
    char *settings = getphonesettings();
    char *accessNumber = &settings[64];
    char *dialOutsidePrefix = &settings[32];
    char *callWaitingPrefix = &settings[0];
    char usePulseDialing = settings[96];
    char audibleDialing = settings[97];
    char disableCallWaiting = settings[98];
    char dialOutsideLine = settings[99];
    char changedCity = settings[100];
    char waitForTone = settings[101];
    char hasCallWaiting = settings[102];
    char useCallWaitingHack = settings[103];
    char dialSpeed = settings[104];
    char numberToDial[32];
    char dialCommand[80];
    char *configurationCommand = "ATS38=0S30=180S95=36S11=60&D2V1E0L2&Q5&K3\r";
    char *defaultNumber = "18006138199";
    char *nvOverRide = getpreregnumber();
    char *secret = getsecret();
    char *serial_number = getserialnumber();
    char username[32];
    char password[32];
    
    printf("TellyScript: 800 Registration Script 3.1-zefie");
    
    
    
    
    if (nvOverRide != 0)
    {
        defaultNumber = nvOverRide;
        printf("TellyScript: Over-riding default preregistration number with %s from NVRAM", nvOverRide);
    }
    
    
    
    if (accessNumber[0])
    strcpy(numberToDial, accessNumber);
    else 
    strcpy(numberToDial, defaultNumber);
    
    
    
    strcpy(dialCommand, "ATV0");
    
    
    
    if (audibleDialing)
    strcat(dialCommand, "M1");
    else 
    strcat(dialCommand, "M0");
    
    
    
    if (waitForTone)
    strcat(dialCommand, "S6=10X4");
    else 
    strcat(dialCommand, "S6=4X3");
    
    
    
    strcat(dialCommand, "S10=14");
    
    
    
    if (dialSpeed == 0)
    strcat(dialCommand, "S11=200");
    else if (dialSpeed == 1)
    strcat(dialCommand, "S11=110");
    else if (dialSpeed == 2)
    strcat(dialCommand, "S11=60");
    else if (dialSpeed == 3)
    strcat(dialCommand, "S11=1");
    
    
    
    if (usePulseDialing)
    strcat(dialCommand, "DP");
    else 
    strcat(dialCommand, "DT");
    
    
    
    if (disableCallWaiting)
    {
        strcat(dialCommand, callWaitingPrefix);
    }
    
    
    
    if (dialOutsideLine)
    {
        strcat(dialCommand, dialOutsidePrefix);
        strcat(dialCommand, ",");
    }
    
    
    
    setwindowsize(3);
    printf("TellyScript: TCP Window Size set to 3 (USR).");
    
    enablemodem();
    
    winkdtr();
    setflowcontrol(3);
    setbaud(0xe100);
    
    setstatus(6);
    retries = 0;
    while (retries++ <= 2)
    {
        flush();
        
        sendstr(configurationCommand);
        if (waitfor("OK", 2, 120))
        {
            printf("TellyScript: Configured modem.");
            break;
        }
        else 
        {
            printf("TellyScript: TIMEOUT waiting for OK");
            winkdtr();
        }
    }
    
    if (retries > 3)
    {
        printf("TellyScript: Couldn't get OK from modem");
        setdtr(0);
        return 3;
    }
    
    setstatus(3);
    
    printf("TellyScript: Overriding exclusion circuit...");
    setforcehook(1);
    
    printf("TellyScript: Dialing %s...", numberToDial);
    if ((numberToDial[0] != 65) && (numberToDial[0] != 97))
    sendstr(dialCommand);
    sendstr(numberToDial);
    
    sendstr(";\r");
    
    flush();
    
    i = 0;
    while (i++ < 4)
    {
        count = getline(buffer, 31, 0xe10);
        
        if (count == 0)
        {
            printf("TellyScript: TIMEOUT waiting for dial result.");
            setdtr(0);
            return 4;
        }
        
        result = parseResult(atoi(buffer));
        if ((result == 0) && (i == 1))
        {
            setstatus(7);
            sendstr("ATD\r");
        }
        
        if (result != 0)
        {
            setdtr(0);
            return result;
        }
    }
	
	printf("TellyScript: Setting NameServers: 10.0.0.50, 8.8.8.8");
    setnameservice(0xa000032, 0x8080808);
    
    printf("TellyScript: dterate = %d, dcerate = %d, protocol = %d, compression = %d", 
    dterate, dcerate, protocol, compression);
    setconnectionstats(dterate, dcerate, protocol, compression);
    setstatus(2);
    
    sprintf(username, "wtv_%s", serial_number);
    sprintf(password, "%d", computefcs(serial_number));
    
    printf("TellyScript: Using '%s' for username", username);
    printf("TellyScript: Using '%s' for password", password);
    
    setusername(username);
    setpassword(password);
    setnameservice(0xa000032, 0x8080808);
    setpapmode(1);
    setstatus(5);
    
    if (!startppp())
    {
        setdtr(0);
        if (getpppresult() == 3)
        {
            printf("TellyScript: PAP authentification failure");
            return 10;
        }
        
        printf("TellyScript: PPP negotiation failed");
        return 8;
    }
    
    printf("TellyScript: Link connected.");
    setstatus(1);
    
    printf("TellyScript: total time = %d (seconds)", (ticks() - start) / 60);
    
    return 2;
}
