/* jawsd - connects to AWS datalogger over serial connection and provides
 *         weather information to the network
 * Copyright (C) 2003-2004 Jeffrey Grafton <jgrafton@tjhsst.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <syslog.h>

#include "config.h"
#include "data_types.h"
#include "datalogger.h"
#include "network_raw.h"
#include "network_raw2.h"
#include "wunderground.h"
#include "aws_server.h"

int main (void)
{
    data_processed *processed;
    curobs_raw *curobs;
    hilo_raw *hilo;
    char *serial, *history;
    unsigned short *history_id;
    int *history_length;
    pthread_t aws_thread, data_thread, socket_thread, wunderground_thread, socket_thread2;
    void *ptrs[7];

    openlog("jawsd", LOG_PID, LOG_DAEMON);
    syslog(LOG_INFO, "jawsd version %s starting up", VERSION);
    
    if (
	((processed = malloc(sizeof(data_processed))) == NULL) ||
	((curobs = malloc(sizeof(curobs_raw))) == NULL) ||
	((hilo = malloc(sizeof(hilo_raw))) == NULL) ||
	((serial = malloc(SERIAL_LENGTH)) == NULL) ||
	((history_id = malloc(sizeof(unsigned short))) == NULL) ||
	((history = malloc(sizeof(curobs_raw))) == NULL) ||
	((history_length = malloc(sizeof(int))) == NULL)
	)
    {
       printf("Unable to allocate memory.\n");
       syslog(LOG_ERR, "Unable to allocate memory.");
       exit(1);
    }

    memset(processed, sizeof(data_processed), 0);
    memset(curobs, sizeof(curobs_raw), 0);
    memset(hilo, sizeof(hilo_raw), 0);
    memset(serial, SERIAL_LENGTH, 0);
    memset(history, sizeof(curobs_raw), 0);
    memset(history_length, sizeof(int), 0);
    
    *history_id = 0;
    
    ptrs[0] = processed;
    ptrs[1] = curobs;
    ptrs[2] = hilo;
    ptrs[3] = serial;
    ptrs[4] = history_id; 
    ptrs[5] = history;
    ptrs[6] = history_length;
    
    pthread_create(&data_thread, NULL, receive_and_process_data, (void *)ptrs);
    pthread_create(&socket_thread, NULL, socket_listen, (void *)processed);
    pthread_create(&socket_thread2, NULL, socket_listen2, (void *)processed);
    pthread_create(&aws_thread, NULL, aws_server, (void *)ptrs);
    pthread_create(&wunderground_thread, NULL, update_wunderground, (void *)processed);
 
    if (setuid(UID) == -1)
    {
 	perror("setuid");
    }
    else
    {
	printf("main dropped to uid %d\n", getuid());
    }
    
    pthread_exit(0);
    
    return 0;
}


