/*
	Derived from ZAP utility
	Only CA-driving functions

	Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
	Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include "poll.h"


static void signal_handler(int _signal);

static bool quit_app = false;

void usage(void)
{
	static const char *_usage = "\n"
		" ca: Allows to enable the Common Access module, for encrypted channels, e.g. Pay-TV.\n\n"
		" Copyright (C) 2004, 2005, 2006 Manu Abraham (manu@kromtek.com)\n"
		" Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)\n\n"
		" Copyright (C) 2008 Ben Bucksch \n\n"
		" usage: ca <options> as follows:\n"
		" -h			show this help text\n"
		" -servideid <id>	service ID, PAT/PMT from DVB stream, identifies the channel to decrypt (mandatory)\n"
		" -adapter <id>		adapter to use (default 0)\n"
		" -caslotnum <id>	ca slot number to use (default 0)\n"
		" -nomoveca		Do not attempt to move CA descriptors from stream to programme level\n";
	fprintf(stderr, "%s\n", _usage);
}

int main(int argc, char *argv[])
{
	int adapter_id = 0;
	int caslot_num = 0;
	int moveca = 1;
	int service_id = -1;
	int argpos = 1;

	while(argpos != argc) {
		if (!strcmp(argv[argpos], "-h")) {
			usage();
		} else if (!strcmp(argv[argpos], "-adapter")) {
			if ((argc - argpos) < 2)
				usage();
			if (sscanf(argv[argpos+1], "%i", &adapter_id) != 1)
				usage();
			argpos+=2;
		} else if (!strcmp(argv[argpos], "-caslotnum")) {
			if ((argc - argpos) < 2)
				usage();
			if (sscanf(argv[argpos+1], "%i", &caslot_num) != 1)
				usage();
			argpos+=2;
		} else if (!strcmp(argv[argpos], "-serviceid")) {
			if ((argc - argpos) < 2)
				usage();
			if (sscanf(argv[argpos+1], "%i", &service_id) != 1)
				usage();
			argpos+=2;
		} else if (!strcmp(argv[argpos], "-nomoveca")) {
			moveca = 0;
			argpos++;
		} else {
			fprintf(stderr, "Warning: '%s' not understood\n", argv[argpos]);
			argpos++;
		}
	}

	if (service_id == -1)
	{
		usage();
		return 1;
	}

	// setup the signals, which allow to end the app gratiously
	signal(SIGINT, signal_handler);
	signal(SIGPIPE, SIG_IGN);

	try {
		ca_start(adapter_id, service_id, &quit_app);
	} catch (char* msg) {
		fprintf(stderr, "%s (from ca_start())", msg);
		return 2;
	} catch (...) {
		fprintf(stderr, "Exception (from ca_start())");
		return 2;
	}

	// the most advanced UI - hurray!
	//printf(stdout, "Press Ctrl-C to quit");
	while(!quit_app)
		sleep(1);

	return 0;
}

static void signal_handler(int _signal)
{
	ca_stop(&quit_app);
}
