#include <stdio.h>
#include <linux/dvb/frontend.h>
#include "dump-btv.h"



static int pg_number = 0;

/* not used by BTV(dvbstream)
static const char *inv_name [] = {
	"INVERSION_OFF",
	"INVERSION_ON",
	"INVERSION_AUTO"
};
*/

static const char *fec_name [] = {
	"",
	" -cr 1_2",
	" -cr 2_3",
	" -cr 3_4",
	" -cr 4_5",
	" -cr 5_6",
	" -cr 6_7",
	" -cr 7_8",
	" -cr 8_9",
	" -cr N"
};


static const char *qam_name [] = {
	"",
	" -qam 16",
	" -qam 32",
	" -qam 64",
	" -qam 128",
	" -qam 256",
	""
};


static const char *bw_name [] = {
	" -bw 8",
	" -bw 7",
	" -bw 6",
	""
};


static const char *mode_name [] = {
	" -tm 2",
	" -tm 8",
	""
};

static const char *guard_name [] = {
	" -gi 32",
	" -gi 16",
	" -gi 8",
	" -gi 4",
	""
};

/* not used by BTV(dvbstream) */
static const char *hierarchy_name [] = {
	"HIERARCHY_NONE",
	"HIERARCHY_1",
	"HIERARCHY_2",
	"HIERARCHY_4",
	"HIERARCHY_AUTO"
};


void btv_dump_dvb_parameters (FILE *f, fe_type_t type, struct dvb_frontend_parameters *p, char polarity, int sat_number)
{

	switch (type) {
	case FE_QPSK:
		fprintf (f, "_%i_GROUP-%i_PROG_~/OSD.bat.btv & dvbstream ", ((pg_number%10) +1),pg_number / 10 );
		fprintf (f, " -f %i ", p->frequency / 1000);	/* channels.conf wants MHz */
		fprintf (f, " -p %c ", polarity);
		fprintf (f, " -d %d ", sat_number);
		fprintf (f, " -s %i ", p->u.qpsk.symbol_rate / 1000); /* channels.conf wants kBaud */
		/*fprintf (f, "%s", fec_name[p->u.qpsk.fec_inner]);*/
		break;

	case FE_QAM:
		fprintf (f, "_%i_GROUP-%i_PROG_~/OSD.bat.btv & dvbstream ", ((pg_number%10) +1),pg_number / 10 );
		fprintf (f, " -f %i ", p->frequency);
/*		fprintf (f, "%s ", inv_name[p->inversion]);*/
		fprintf (f, " -s %i ", p->u.qpsk.symbol_rate);
		fprintf (f, "%s ", fec_name[p->u.qpsk.fec_inner]);
		fprintf (f, "%s ", qam_name[p->u.qam.modulation]);
		break;

	case FE_OFDM:
		fprintf (f, "_%i_GROUP-%i_PROG_~/OSD.bat.btv & dvbstream ", ((pg_number%10) +1),pg_number / 10 );
		fprintf (f, " -f %i ", p->frequency);
/*		fprintf (f, "%s ", inv_name[p->inversion]);*/
		fprintf (f, "%s ", bw_name[p->u.ofdm.bandwidth]);
		fprintf (f, "%s ", fec_name[p->u.ofdm.code_rate_HP]);
		fprintf (f, "%s ", fec_name[p->u.ofdm.code_rate_LP]);
		fprintf (f, "%s ", qam_name[p->u.ofdm.constellation]);
		fprintf (f, "%s ", mode_name[p->u.ofdm.transmission_mode]);
		fprintf (f, "%s ", guard_name[p->u.ofdm.guard_interval]);
		fprintf (f, "%s ", hierarchy_name[p->u.ofdm.hierarchy_information]);
		break;

	default:
		;
	};
}

void btv_dump_service_parameter_set (FILE *f, 
				 const char *service_name,
				 fe_type_t type,
				 struct dvb_frontend_parameters *p,
				 char polarity,
				 int sat_number,
				 uint16_t video_pid,
				 uint16_t *audio_pid,
				 uint16_t service_id)
{

	fprintf (f, "_%i_GROUP-%i_NAME_echo -n \"%i - %s\" > ~/channel.osd.btv &\n", ((pg_number%10) +1), (pg_number / 10), pg_number, service_name );
	btv_dump_dvb_parameters (f, type, p, polarity, sat_number);
	fprintf (f, " -v %i:1 -a %i:2 ", video_pid, audio_pid[0]);
	fprintf (f, " & ^MPLAYER-MP2^ >> /dev/null &\n");

	if(((pg_number + 1) % 10) == 0) 
	    	fprintf (f, "\n_CUT-THIS-ROW-FOR-LIRCRC_config = echo  \"GROUP-%i\"  |  tee ~/satgroup.ud.btv | osd_cat -i %i   -l 15 -o 0 -c magenta -O 5 -u black -d 2 -s 0 -f -*-*-bold-r-*-*-32 -p top &\n\n", (pg_number / 10), ((pg_number% 10) * 80) );

	pg_number ++;
}


void btv_dump_header(FILE *f)
{
const char *btv_header = "Budget DVB TV - programm list part, generated by './scan -t 1 [-s 1/0] -o btv ./dvb-s/INITIAL-TUNING-DATA-FILE > CHANNEL.conf.BTV'\n\
Author: Ivan Gerasimov intek'replace_here'vestel.bg\n\
Date:   Nov.2003\n\
Used:   - dvbstream from CVS(> 03.2003), modifyed for DiSEQ for steaming over RTP or STDOUT  from www.linuxstb.org\n\
    - latest DVB-drivers from www.linuxtv.org\n\
    - MPlayer for watching and playing from www.MPlayerHQ.hu\n\
    - XOSD for displaying On Scrren > Version: 2.2.5 from www.ignavus.net/software.html\n\
    - LIRC for controlling all operations from www.lirc.org\n\
    - tee, egrep, cut, cat, echo, cp, sed, exec and chmod\n\
COMMENT: \"-d 1/0\" of dvbstream is the DiSEQ switch added from me. With unmodifyed version you must delete him!\n\
\n\
+++++++++++++++++++++++++++++++++++++++++++++++++\n\
+++++++++++++++++++++++++++++++++++++++++++++++++\n\
_STOP_THE_OLD_echo \"STOP pressed. Picture is OFF\"  > ~/channel.osd.btv\n\
_STOP_THE_OLD_killproc mplayer\n\
_STOP_THE_OLD_killproc ts2ps\n\
_STOP_THE_OLD_killproc dumprtp\n\
_STOP_THE_OLD_killproc dvbstream\n\
_PLEASE-FIRST-SELECT-GROUP__INIT-NUMBER-GROUP-GROUPLIST_echo -nn \"Wellcome! Press 12 to select a group.\" > ~/channel.osd.btv & echo -n \"1\" | tee ~/abs.ud.btv > ~/cnt.ud.btv &\n\
\n\
===============================LNB/DiSEQ===FREQUENCY==POL==SRATE==mapVpid1===mapApid2===play-command-from-SHOW.bat.btv========\n\n\
";

	fprintf (f, "%s", btv_header);
}

void btv_dump_footer(FILE *f)
{
const char *btv_footer = "\n_STOP_THE_OLD_~/OSD.bat.btv\n";

	fprintf (f, "%s", btv_footer);
}
