/*****************************************************************************
 * Filename: start_ppp
 *****************************************************************************
 *  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 executed.
 *  
 *  teleph	- The phone number used to call the host.  This is required
 *		  if you are connecting remotely.
 *  prompt1     - Prompt seen when user is asked for their username
 *  prompt2	- Prompt seen when user is asked for their password
 *  prompt3	- Prompt seen when the system is ready for the user to start
 *		  ppp on the host.
 *****************************************************************************
 *  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
int timecount;		// Used to keep track of the number of timeouts taken
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 usrprompt[32];	// Holds the login/username prompt
str pwdprompt[32];	// Holds the password prompt
str sysprompt[32];	// Holds the system prompt
str pppstart[3];	// Used to detect when ppp has start on the host
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.

// Initialize the prompts 
NvGetVar("prompt1",usrprompt);	// Get the user prompt from NVram
NvGetVar("prompt2",pwdprompt);	// Get the password prompt from NVram
NvGetVar("prompt3",sysprompt);  // Get the system prompt from NVram

strcpy(pppstart, "~");		// Used to detect when ppp is started on the host
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");
sendchars(teleph);

HOSTCONNECT: // Wait for the connection to the host.
c = await("CONNECT", 0);
if (c != 0)
{
    timecount = timecount +1;
    if (timecount > 12){
	goto ERROR1;
    }  	
    goto HOSTCONNECT;	// Keep trying for a full minute
}
recv(tempspeed,16);
strtok(tempspeed,"\n",temp);
strcpy(speed, "PPP connection speed:");
strcat(speed, temp);
echo(speed);

timecount = 0;

START:
// See if the userprompt appeared or that ppp has already started.
c = await(usrprompt,0, pppstart,1);
if (c != 0 ) // The system is not awake yet.
{
	if (c == 1)	// ppp is already running.
	{
	    goto CONNECT;
	}
	timecount = timecount +1;
	if (timecount > 12){  
		goto ERROR1;	// The host is not responding after 60 seconds.
	}
	sendchars("\n\r"); // Wake up the host it might need it
	goto START;
}

settimeout(30); // set the timeout value for various functions
if (c == 0)
{
	getPPPusername(username);	// Get the user's name from NVram.
	strcat(username,"\n");		
	sendchars(username);
	c = await(pwdprompt, 0);	// Wait for the password prompt
	getPPPpassword(password);	// Get the user's password from NVram
	strcat(password,"\r");
	sendchars(password);
	setechomode();

	c = await(sysprompt, 0, pppstart, 1); // Wait for the system prompt or see if ppp
					      // is auto started on the host.	
	if (c == 0)
	{
		echo ("\nStarting ppp on the host...\n");
		sendchars("ppp\n");  // This is the most common was to start ppp on the host.
			   	     // Some hosts may be different.
		goto CONNECT;
	}
	elseif (c == 1)
	{
		goto CONNECT;
	}
	else
	{
		echo("\n No system prompt found.  The prompt3 script parameter must be set.\n");
		goto ERROR2;
	}

}
elseif (c == 1)
{
	goto CONNECT;
}
else
{
	goto ERROR2;
}

CONNECT:
c=await(pppstart,0);  // Wait for ppp to start on the host.  
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.");
    echo("prompt1   - The prompt that appears when you are asked for your username");
    echo("prompt2   - The prompt that appears when you are asked for your password");
    echo("prompt3   - The system prompt. If PPP starts automatically, this is");
    echo("            not required.");
    echo("\n\nPPP requires authentication.  Please insure you have your");
    echo("username and password enter in PPP Configuration in Setup.\n");
    goto EXIT;
ERROR3:
    echo("\nPPP did not start on the host.\n");	
EXIT:  
killparent();			  // Kill the tip window so PPP can start
				  // on the tip port.

