/* $Id$ */
/******************************************************************
 *         author : Olivier Bonenfant
 *	            David Gümbel
 *         email : olivier.bonenfant@5nine.net
 *		   david.guembel @ gmx.net
 *******************************************************************/
/****************************************************************************
 *                                                                          *
 *   libwsp library is a C++ implementation of the WAP-WSP standard.        *
 *   Copyright (C) 2000 by www.5nine.net                                    *
 *                                                                          *
 *   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 <iostream>
#include <ostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <list>

#include "CSessionCL.h"
#include "CWspMethod.h"
#include "typecode.h"

#include "OctStream.h"

struct arguments
{
	struct sockaddr_in gateway_address;
	char* url;	
};


struct arguments* parse_arguments (int argc, char** argv);


int main(int argc, char** argv)
{
	struct arguments *args;
	CSessionCL *session;
	CWspMethod *method;
	CWspMethod *replyMethod;
	OctStream *o;
	list <string> headers;
	string header;
	int err;

	// Parse arguments
	args = parse_arguments (argc, argv);
	if (args == NULL)
		exit(-1);

	
	// Create pseudo-session
	session = new CSessionCL ((struct sockaddr*) &(args->gateway_address));

	// Create HTTP headers
	header = "User-Agent: wapget";
	headers.push_back (header);
	header = "Accept: application/vnd.wap.wmlc, text/vnd.wap.wml";
	headers.push_back (header);


	// Create WSP Method
	method = new CWspMethod (0, GET, args->url, strlen (args->url), NULL, 0, &headers);


	// Send WSP Method
	err = session->s_invoke (method);
	if (err)
	{
		cerr << "wapget: s_invoke failed!" << endl;
		exit(-1);
	}
	//clog << "wapget: s_invoke success!" << endl;


	// Wait for reply
	//clog << "wapget: waiting for reply..." << endl;
	replyMethod = session->receiveMethod (false);
	if (replyMethod == NULL)
		clog << "wapget: Received NULL Reply" << endl;
	else
		//clog << "wapget: Received Reply" << endl;

	printf("huii!\n");
	// Output of response on stdout
	o = new OctStream(replyMethod->getBody(),replyMethod->getBodyLen());
	cout << *o->toString();
	return 0;
}





struct arguments* parse_arguments (int argc, char** argv)
{
	struct arguments *args = (struct arguments*) malloc  (sizeof (struct arguments));
	char *gateway;
	char *url;
	char *ip;
	char *port;
	int port_len = 0;
	int i = 0;
	int err;
	if (argc < 3)
		goto parse_error;


	// Setup address
	memset (&(args->gateway_address), 0, sizeof (struct sockaddr_in));
	args->gateway_address.sin_family = AF_INET;
	

	// Parse IP
	gateway = argv[1];
	//clog << "gateway = " << gateway << endl;
	while (gateway[i] != ':' && gateway[i] != '\0')
		i++;

	if (gateway[i] == '\0')
		goto parse_error;
	else
	{	
		gateway[i] = '\0';
		ip = gateway;
	}
	
	//clog << "ip = " << ip << endl;
	err = inet_aton (ip, &(args->gateway_address.sin_addr));
	if (err == 0)
		goto parse_error;

	// Parse port
	i++;
	while (gateway[i] != '\0')
	{
		i++;
		port_len++;
	}
	port = gateway+=(i - port_len);
	//clog << "port = " << port << endl;
	args->gateway_address.sin_port = htons (atoi (port));
	

	// Parse URL
	url = argv[2];
	//clog << "url = " << url << endl;
	args->url = (char*) malloc (strlen (url) + 1);
	strcpy (args->url, url);

	return args;

parse_error:
	cerr << "wapget: Usage: wapget <gateway_ip:port> <URL>" << endl;
	return NULL;
}









