/*
 * sock_init - easy way to guarentee:
 *  - card is ready
 *  - shutdown is handled
 *  - cbreaks are handled
 *      - config file is read
 *  - bootp is run
 *
 * 0.1 : May 2, 1991  Erick - reorganized operations
 */

#include "copyright.h"
#include "wattcp.h"
#include "errors.h"
#include <stdlib.h>

word _survivebootp = 0;

void sock_exit()
{
    tcp_shutdown();
#ifdef WINDOWS
    WinExit();
#endif
}

int sock_init(void)
{
    int err;

#ifdef WINDOWS
    if (err = WinInit()) return err;    /* Initialize Windows environment */
#endif
    if (err = tcp_init()) return err;   /* must precede tcp_config because we need eth addr */
    atexit(sock_exit);  /* must not precede tcp_init() incase no PD */
    tcp_cbrk( 0x10 );   /* allow control breaks, give message */

    if (tcp_config( NULL )) { /* if no config file use BOOTP w/broadcast */
       _bootpon = 1;
#ifdef WINDOWS
       /* nothing */
#else
       outs("Configuring through BOOTP");
#endif
    }

    if (_bootpon) /* non-zero if we use bootp */
      if (_dobootp()) {
#ifdef WINDOWS
	 /* nothing */
#else
         outs("BOOTP failed\n\r");
#endif
         if ( !_survivebootp )
           sock_exit();
           return(ER_CONF);
      }
    return(SUCCESS);
}
