/*******************************************************************************
	SQL to adjust the EPG data for various problems.
*******************************************************************************/

DROP PROCEDURE IF EXISTS one_transponder_dvbs2_fix;

DELIMITER $$
CREATE PROCEDURE one_transponder_dvbs2_fix(transponder VARCHAR(10))
BEGIN
	DECLARE p_finished INTEGER DEFAULT 0;
	DECLARE i_iptvid smallint(6) unsigned;
	DECLARE v_iptvid smallint(6) unsigned;
	DECLARE i_url text;
	DECLARE v_url text;

	DECLARE p CURSOR FOR
		SELECT `iptvid`,`url` FROM iptv_channel
			WHERE `url` LIKE CONCAT("%freq=", transponder, "&%")
			order by `iptvid`;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET p_finished = 1;

	SELECT CONCAT('Transponder ',transponder) AS Heading;

	SELECT * FROM iptv_channel
		WHERE `url` LIKE CONCAT("%freq=", transponder, "&%")
		order by `iptvid`;

	OPEN p;
	channel_loop: LOOP
		FETCH p INTO i_iptvid, i_url;
		SET v_iptvid=i_iptvid;
		SET v_url=i_url;
		SET v_url=REPLACE(v_url, 'msys=dvbs&', 'msys=dvbs2&');
		SET v_url=REPLACE(v_url, 'fec=34&', 'fec=23&');
		SET v_url=REPLACE(v_url, 'mtype=qpsk&', 'mtype=8psk&');
		SELECT v_iptvid, v_url;
		UPDATE iptv_channel SET `url`=v_url WHERE `iptvid`=v_iptvid;
		IF p_finished != 0 THEN
			LEAVE channel_loop;
		END IF;
	END LOOP;
	CLOSE p;

	SELECT * FROM iptv_channel
		WHERE `url` LIKE CONCAT("%freq=", transponder, "&%")
		order by `iptvid`;

END$$
DELIMITER ;

CALL one_transponder_dvbs2_fix('12581');
CALL one_transponder_dvbs2_fix('12608');
CALL one_transponder_dvbs2_fix('12644');
CALL one_transponder_dvbs2_fix('12671');
DROP PROCEDURE IF EXISTS one_transponder_dvbs2_fix;
