#include <ctype.h>
#include <vdr/eitscan.h>

#include "status.h"


cSbStatus::cSbStatus()
:	bits(0)
{
	for (int i = 0; i < MAXDEVICES; i++)
	{
		channels[i] = 0;
		recordings[i] = 0;
	}
}

void cSbStatus::ChannelSwitch(const cDevice *Device, int ChannelNumber)
{
	//printf("ChannelSwitch %d %d\n", Device->CardIndex(), ChannelNumber);
	if (ChannelNumber > 0)
	{
		channels[Device->DeviceNumber()] = ChannelNumber;
		bits = 0;
		for (int i = 0; i < 4; i++)
		{
			if (recordings[i] > 0)
			{
				bits |= 128;
				bits |= 1 << (3 - i);
				if (channels[i] > 0)
				{
					cChannel * channel = Channels.GetByNumber(channels[i]);
					if (channel)
					{
						if ((channel->Source() & cSource::st_Mask) == cSource::stSat)
						{
							if (toupper(channel->Polarization()) == 'V')
							{
								bits |= 64;
							}
							if (toupper(channel->Polarization()) == 'H')
							{
								bits |= 32;
							}
						}
						if ((channel->Source() & cSource::st_Mask) == cSource::stCable)
						{
							bits |= 16;
						}
						if ((channel->Source() & cSource::st_Mask) == cSource::stTerr)
						{
							bits |= 16;
						}
					}
				}
			}
			cDevice * device = cDevice::GetDevice(i);
			if (device)
			{
				if (EITScanner.Active())
				{
					if (EITScanner.UsesDevice(device))
					{
						bits |= 1 << (3 - i);
					}
				}
				if (device == cDevice::PrimaryDevice())
				{
					bits |= 1 << (3 - i);
				}
				if (device == cDevice::ActualDevice())
				{
					bits |= 1 << (3 - i);
				}
			}
		}
		//printf("%d\n", bits);
		Fork();
	}
}

void cSbStatus::Recording(const cDevice *Device, const char *Name)
{
	//printf("Recording %d %s\n", Device->CardIndex(), Name);
	if (Name)
	{
		recordings[Device->DeviceNumber()]++;
	}
	else
	{
		recordings[Device->DeviceNumber()]--;
	}
	bits = 0;
	for (int i = 0; i < 4; i++)
	{
		if (recordings[i] > 0)
		{
			bits |= 128;
			bits |= 1 << (3 - i);
			if (channels[i] > 0)
			{
				cChannel * channel = Channels.GetByNumber(channels[i]);
				if (channel)
				{
					if ((channel->Source() & cSource::st_Mask) == cSource::stSat)
					{
						if (toupper(channel->Polarization()) == 'V')
						{
							bits |= 64;
						}
						if (toupper(channel->Polarization()) == 'H')
						{
							bits |= 32;
						}
					}
					if ((channel->Source() & cSource::st_Mask) == cSource::stCable)
					{
						bits |= 16;
					}
					if ((channel->Source() & cSource::st_Mask) == cSource::stTerr)
					{
						bits |= 16;
					}
				}
			}
		}
		cDevice * device = cDevice::GetDevice(i);
		if (device)
		{
			if (EITScanner.Active())
			{
				if (EITScanner.UsesDevice(device))
				{
					bits |= 1 << (3 - i);
				}
			}
			if (device == cDevice::PrimaryDevice())
			{
				bits |= 1 << (3 - i);
			}
			if (device == cDevice::ActualDevice())
			{
				bits |= 1 << (3 - i);
			}
		}
	}
	//printf("%d\n", bits);
	Fork();
}

const char * ProgName = "echo";

bool cSbStatus::Fork(void)
{
/*
	int pid;

	pid = fork();
	if (pid == -1)
	{
		esyslog("ERROR: statusbits fork failed: (%d) %s", errno, strerror(errno));
			return false;
	}
	if (pid == 0)
	{
		// child
		dsyslog("statusbits: child started (pid=%d)", getpid());

		char cmd[256];
		snprintf(cmd, sizeof(cmd), "%s %u", ProgName, bits);
		execle("/bin/sh", "sh", "-c", cmd, 0, environ);
		esyslog("ERROR: statusbits exec failed for %s: (%d) %s", cmd, errno, strerror(errno));
		exit(127);
	}
*/
	char cmd[256];
	snprintf(cmd, sizeof(cmd), "%s %u", ProgName, bits);
	system(cmd);

	return true;
}

