/*****************************************************************************
 * Filename: start_man
 *****************************************************************************
 *  The following entries are required to be in the Script Parameters Table,
 *  which is located under the Network and Utilities Menu in Setup.  This
 *  must be done before this script is run.
 *  
 *  teleph	- The phone number used to call the host.  This is required
 *		  if you are connecting remotely.
 *****************************************************************************
 *  The PPP configuration menu in Setup must be prepared before this script
 *  can be used.
 *****************************************************************************/

int c;			// Variable used to check return values from await
str teleph[100];	// String used to store the phone number
str tempnum[100];	// Temporary string to build the dialout string.
str username[64];	// String used to store the username
str password[64];	// String used to store the user's password
str pppstart[3];	// Used to detect when ppp has start on the host
int timecount;		// Used to keep track of the number of timeouts taken
str speed[32];          // Store the modem speed of modem connection.
str tempspeed[16];      // Store the modem speed of modem connection.
str temp[16];           // Store the modem speed of modem connection.

strcpy(pppstart, "~");
timecount = 0;

settimeout(5); // set the timeout value for various functions

// Dial up the host
sendchars("AT\r\n");
sleep(2);
strcpy(teleph,"ATDT");
NvGetVar("teleph",tempnum);
strcat(teleph,tempnum);
strcat(teleph, "\r\n");	// Put together the dial command "ATDT123-5678\r\n"
sendchars(teleph);	// Send the dial command.

HOSTCONNECT: // Wait for the connection to the host.
c = await("CONNECT", 0);
if (c != 0)
{
    timecount = timecount +1;	
    if (timecount > 12){	// Loop for 12 x 5(set by settimout above)
	goto ERROR1;		// seconds.
    }  	
    goto HOSTCONNECT;
}
recv(tempspeed,16);
strtok(tempspeed,"\n",temp);
strcpy(speed, "PPP connection speed:");
strcat(speed, temp);
echo(speed);


timecount = 0;

START:
settimeout(600); // set the timeout value for various functions, 10 minutes. 

CONNECT:
c=await(pppstart,0);  // Wait for ppp to start.  Wait up to 10 minutes.
if (c == 0 )
{
	goto EXIT;
}
else
{
	goto ERROR3;
}
ERROR1:
    echo("The host did not respond after 60 Seconds.  Insure the system is up");
    echo("and your modem is working properly.\n");
ERROR2:
    echo("\nPlease insure the following parameters are in the");
    echo("Script Parameters Table in setup:");
    echo("teleph    - The phone number of the system you wish to connect to.");
    goto EXIT;
ERROR3:
    echo("\nPPP did not start on the host.\n");	
EXIT:  
killparent();			  // Kill the tip window so PPP can start
				  // on the serial port.

