#!/usr/bin/php -q
<?php
require (dirname(__FILE__)."/../../appcore/app.php");
$app = new AppApp( dirname(__FILE__)."/../app.config.php");

$lid = $argv[1];
if ( $lid < 1 ) { print " Please enter a lineup id as an argument\n";exit;}
	
$svr = readline("Please Enter Source Server", "www.uptele.com");
$epgadminPath = readline("enter relatvie epgadmin path", "/epgadmin");
$remoteFsPath = readline("enter abs filesystem path on remote for epgdata", "/opt/epgdata");
$remoteWwwPath = readline("enter abs www path on remote for epgdata", "/epgdata");




$u = "http://$svr"."$epgadminPath/api/synclineup?id=$lid";
print "fetching: $u\n";
$re = json_decode(file_get_contents($u),true);


$lr = $re['lineups'];
$r = app()->selectOne("select * from lineups where id='$lid'");
if ( $r[id]==$lid ) { 
	print "lineup #$lid exists\n";
} else { 
	app()->query("insert lineups set id='$lid'");
	$lr['resellerCsv'] = "";
	app()->update("lineups", $lr);
}


$lcs = $re['lineupchannels'];
foreach($lcs as $lcr) { 
	$cid = $lcr[channelId];
	$chNo = $lcr[chNo];
	$cr = app()->selectOne("select * from lineupchannels where lineupId='$lid' and (chNo='$chNo' )");
	if ( $cr[id] > 0 ) { 
		print "ch No #$chNo  exists\n";
	} else { 
		$sql = "delete from lineupchannels where lineupId='$lid' and (chNo='$chNo' )";
		app()->query($sql);
		$sql = "INSERT lineupchannels set lineupId='$lid', chNo='$chNo', channelId='$cid'";
		app()->query($sql);
		print "$sql\n";
	}
}

exit;


function readline( $prompt , $def )
{
    echo $prompt." ($def): ";
    $r = rtrim(fgets(STDIN), "\n");
    if ( strlen($r ) < 1 ) { $r = $def;}
    return $r;
}
?>
