/*****************************************************************************
 * Filename: start_isp
 *****************************************************************************
 * Description:  This is alternative to the start_ppp where the user login
 * prompt could be different.  Instead of reading prompt1 from the Script
 * parameter screen in setup it reads prompt4.  This allows the use of both
 * scripts without having to make modifications to the Scrip[t Parameters
 * screen everytime.
 *****************************************************************************
 *  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.
 *  prompt4     - 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("prompt4",usrprompt);
NvGetVar("prompt2",pwdprompt);
NvGetVar("prompt3",sysprompt);

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"); // Build the dialout command in the form:
			//   "ATDT123-4567\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){	// Loop until 60 seconds is up
	goto ERROR1;
    }  	
    goto HOSTCONNECT;
}
recv(tempspeed,16);
strtok(tempspeed,"\n",temp);
strcpy(speed, "PPP connection speed:");
strcat(speed, temp);
echo(speed);

timecount = 0;

START:
// If the user prompt doesn't appear check to see if ppp automatically 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);
	strcat(username,"\n");
	sendchars(username);
	c = await(pwdprompt, 0);
	getPPPpassword(password);
	strcat(password,"\r");
	sendchars(password);
	setechomode();

	c = await(sysprompt, 0, pppstart, 1);
	if (c == 0)
	{
		echo ("\nStarting ppp on the host...\n");
		sendchars("ppp\n");  // This string may be
			   	     // different on your host
		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);
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("prompt4   - 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.

