#if 1

#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <assert.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#endif
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <pwd.h>

#else

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>

#endif

//#define BB_DISPLAY "10.0.4.5"
//#define BB_PORT 1984
#define BB_HOSTS "/home/bb/bb/etc/bb-hosts"
#define BB_CFGDIR "/home/bb/minicfg"

#if 1
int start_winsock(void)
{
	return 1;
}

void stop_winsock(void)
{
	return;
}
#else
static WSADATA wsaData;
static int ws_started = 0;

int start_winsock(void)
{
	int n;

	if (!ws_started) {
		n = WSAStartup(MAKEWORD(2, 2), &wsaData);
		if (n != NO_ERROR) {
			printf("Error at WSAStartup()");
		} else {
			ws_started = 1;
		}
	}
	return ws_started;
}

void stop_winsock(void)
{
	if (ws_started) WSACleanup();
	ws_started = 0;
}
#endif

int main(void) {

    // Initialize Winsock.
    int n, m, x;
    FILE *fp;
    struct sockaddr_in cli_addr;
    int clilen;
    char *cli;
    char b[1024], ipaddr[1024], machine[1024], mach[1024];
    int one = 1;
    int m_socket;
    struct sockaddr_in service;
    int s;
    int bytesRecv = -1;
    char sendbuf[100000];

    if (!start_winsock()) return 0;

    // Create a socket.
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == -1 ) {
        printf( "Error at socket(): %d\n", m_socket );
	stop_winsock();
        return 0;
    }

    // Bind the socket.

    service.sin_family = AF_INET;
    service.sin_addr.s_addr = inet_addr( "0.0.0.0" );
    service.sin_port = htons( 27016 );

    setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof one);

    if ( bind( m_socket, (struct sockaddr *) &service, sizeof(service) ) == -1 ) {
        printf( "bind() failed.\n" );
        close(m_socket);
	stop_winsock();
        return 0;
    }
    
    // Listen on the socket.
    if ( listen( m_socket, 1 ) == -1 )
        printf( "Error listening on socket.\n");

    // Accept connections.

    for (;;) {
        printf( "Waiting for a client to connect...\n" );
        while (1) {
            s = -1;
            while ( s == -1 ) {
		clilen = sizeof cli_addr;
                s = accept( m_socket, (struct sockaddr *)&cli_addr, &clilen );
            }
	    cli = inet_ntoa(cli_addr.sin_addr);
            printf( "Client Connected from %s.\n", cli);
	    fflush(stdout);
            break;
        }

        // Send and receive data.
 
	sprintf(machine, "brokencfg-%s", cli);
	fp = fopen(BB_HOSTS, "r");
	if (fp) {
		while (fgets(b, sizeof b, fp)) {
			n = sscanf(b, "%s %s", ipaddr, mach);
			//printf("n = %d\n", n);
			if (n != 2) continue;
			//printf("ipaddr '%s', machine '%s'\n", ipaddr, mach);
			if (!strcmp(ipaddr, cli)) {
				strcpy(machine, mach);
				break;
			}
		}
	} else {
		printf("Can't open file %s\n", BB_HOSTS);
	}
	if (fp) fclose(fp);
	snprintf(sendbuf, sizeof sendbuf,
		"[mrbig]\r\n"
		"machine %s\r\n",
		machine);
	snprintf(b, sizeof b, "%s/%s", BB_CFGDIR, cli);
	fp = fopen(b, "r");
	if (fp == NULL) {
		snprintf(b, sizeof b, "%s/%s", BB_CFGDIR, "0.0.0.0");
		fp = fopen(b, "r");
	}
	if (fp) {
		n = strlen(sendbuf);
		m = fread(sendbuf+n, 1, sizeof sendbuf - n, fp);
		sendbuf[n+m] = '\0';
		fclose(fp);
	}
        n = strlen(sendbuf);
        m = 0;
        while ((m < n) && (x = send(s, sendbuf+m, n-m, 0)) > 0) {
	    m += x;
        }
        close(s);
    }

    return 0;
}

