/*
 * PDU Encoder tool
 *
 * Copyright (C) 2010 by James Maki
 *
 * Author: James Maki <jmaki@multitech.com>
 *
 * 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
 *
 */

#define __MAIN_FILE_C	1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "global.h"
#include "cmd_options.h"
#include "utils.h"
#include "sms_utils.h"
#include "sms_send.h"
#include "sms_list.h"
#include "sms_delete.h"
#include "xprintf.h"
#include "pdu_encode.h"
#include "pdu_decode.h"

static void print_version(const char *name)
{
	printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name);
	printf("Copyright (C) 2010 by Multi-Tech Systems\n");
	printf(
"This program is free software; you may redistribute it under the terms of\n"
"the GNU General Public License version 2 or (at your option) any later version.\n"
"This program has absolutely no warranty.\n");
}

static void usage(FILE *out)
{
	fprintf(out, "Usage: pdu-encoder [ OPTIONS ... ] [ <recipient-addr> ]\n");
	fprintf(out, "where  OPTIONS := { \n");
	fprintf(out, "         --alphabet { seven-bit | eight-bit } |\n");
	fprintf(out, "         -f, --file <input-file> (default is to read from stdin) |\n");
	fprintf(out, "         --smsc-addr <smsc-addr> |\n");
	fprintf(out, "         --verbose\n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

static char *short_options =
	__CMD_OPT_FILE
	__CMD_OPT_VERBOSE;
static struct option long_options[] = {
	{"alphabet", 1, NULL, CMD_OPT_ALPHABET},
	{"file", 1, NULL, CMD_OPT_FILE},
	{"smsc-addr", 1, NULL, CMD_OPT_SMSC_ADDR},
	{"verbose", 0, NULL, CMD_OPT_VERBOSE},
	{"version", 0, NULL, CMD_OPT_VERSION},
	{"help", 0, NULL, CMD_OPT_HELP},
	{0, 0, 0, 0},
};

int main(int argc, char *argv[])
{
	int i;
	int option_index;
	int alphabet = PDU_ALPHABET_DEFAULT;
	int in_fd = fileno(stdin);
	char *smsc_addr = NULL;

	xprintf_init();

	while ((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
		switch (i) {
		case 0:
			break;

		case CMD_OPT_ALPHABET:
			if (!strcmp(optarg, "seven-bit")) {
				alphabet = PDU_ALPHABET_DEFAULT;
			} else if (!strcmp(optarg, "eight-bit")) {
				alphabet = PDU_ALPHABET_EIGHT;
			}
			break;

		case CMD_OPT_FILE:
			in_fd = open(optarg, O_RDONLY);
			if (in_fd < 0) {
				fprintf(stderr, "failed to open %s for reading\n", optarg);
				return 1;
			}
			break;

		case CMD_OPT_SMSC_ADDR:
			smsc_addr = optarg;
			break;

		case CMD_OPT_VERBOSE:
			Global.core.verbose = true;
			break;

		case CMD_OPT_VERSION:
			print_version("pdu-encoder");
			exit(0);
			break;

		case CMD_OPT_HELP:
			usage(stdout);
			exit(0);
			break;

		default:
			usage(stderr);
			exit(1);
		}
	}

	argc -= optind;
	argv += optind;

	struct pdu_info pdu;
	char pdu_str[PDU_BUFFER_SIZE];

	memset(&pdu, 0, sizeof(pdu));

	pdu.type.msg_type = PDU_MTI_SUBMIT;
	pdu.type.validity_period_format = PDU_VPF_RELATIVE;
	pdu.validity_period = PDU_VPF_RELATIVE_2DAYS;
	pdu.data_coding.general.alphabet = alphabet;

	if (smsc_addr) {
		pdu_addr_fill(&pdu.smsc_addr, smsc_addr, SMS_ADDR_UNSPEC);
	}

	if (argc) {
		pdu_addr_fill(&pdu.addr, *argv, SMS_ADDR_UNSPEC);
		argc++;
		argv--;
	}

	pdu_user_data_read(in_fd, &pdu);

	pdu_encode(pdu_str, sizeof(pdu_str), &pdu);

	printf("pdu: %s\n", pdu_str);

	return 0;
}