/*
 * SMS Delete Messages
 *
 * Copyright (C) 2010 by Multi-Tech Systems
 *
 * 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
 *
 */

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

#include "global.h"
#include "log.h"
#include "utils.h"
#include "cmd_options.h"
#include "sms_utils.h"
#include "sms_delete.h"
#include "sms_list.h"
#include "atcmd.h"

static int do_delete(int fd, int argc, char **argv)
{
	int tmp;
	char *arg;
	struct msg_list_info list_info;

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

	if (argc < 1) {
		log_error("msg-status expected");
		return false;
	}
	arg = *argv;
	argc--; argv++;

	if (!strcmp(arg, "unread")) {
		list_info.status = SMS_MSG_REC_UNREAD;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "read")) {
		list_info.status = SMS_MSG_REC_READ;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "unsent")) {
		list_info.status = SMS_MSG_STO_UNSENT;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "sent")) {
		list_info.status = SMS_MSG_STO_SENT;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "all")) {
		list_info.status = SMS_MSG_ALL;
		list_info.cmd_type = CMD_TYPE_CMGL;
	} else if (!strcmp(arg, "index")) {
		if (argc < 1) {
			log_error("index expected");
			return false;
		}
		arg = *argv;
		argc--; argv++;

		list_info.index = atoi(arg);

		list_info.status = SMS_MSG_ALL;
		list_info.cmd_type = CMD_TYPE_CMGR;
	} else {
		log_error("invalid msg-status %s", arg);
		return false;
	}

	if (Global.core.verbose) {
		printf("preparing for delete...\n");
	}

	tmp = sms_list_get(fd, &list_info);
	if (tmp < 0) {
		log_error("failed to get msg list");
		return false;
	}

	struct sms_msg *msg;
	int failed = 0;

	list_for_each_entry(msg, &list_info.msg_list, list) {
		if (Global.core.verbose) {
			printf("deleting message at index %d", msg->index);
		}

		tmp = atcmd_plus_cmgd_write(fd, msg->index);
		if (tmp < 0) {
			printf("deleting message at index %d failed\n", msg->index);
			failed++;
			continue;
		}
	}

	sms_list_free(&list_info);

	return failed ? false : true;
}

static char *short_options = "";
static struct option long_options[] = {
	{NULL, 0, NULL, 0},
};

void sms_delete_help(FILE *out) {
	fprintf(out, "usage: delete STATUS [ ARGUMENTS ]\n");
	fprintf(out, "where  STATUS := { \n");
	fprintf(out, "         index <index>\n");
	fprintf(out, "         unread\n");
	fprintf(out, "         read\n");
	fprintf(out, "         unsent\n");
	fprintf(out, "         sent\n");
	fprintf(out, "         all\n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

int sms_delete(int argc, char **argv)
{
	int i;
	int option_index;
	int ret;
	int fd;

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

		default:
			sms_delete_help(stderr);
			return false;
		}
	}

	if (optind >= argc) {
		sms_delete_help(stderr);
		return false;
	}
	argc -= optind;
	argv += optind;

	fd = sms_device_open();
	if (fd < 0) {
		return false;
	}

	ret = do_delete(fd, argc, argv);

	sms_device_close(fd);

	return ret;
}