<?php
require('includes/global.php');
$ettvd->init();

if(isset($_POST['broadcast'])) {
   $error = array();
   if(empty($_POST['ip']))         $error['ip'] = ERROR;
   if(empty($_POST['serverpass'])) $error['serverpass'] = ERROR;
   if(empty($_POST['ettvpass']))   $error['ettvpass'] = ERROR;
   if(empty($_POST['match']))      $error['match'] = ERROR;

   if(empty($error)) {
      $ettvd->broadcast(trim($_POST['ip']), trim($_POST['serverpass']), trim($_POST['ettvpass']), str_replace(';', ',', trim($_POST['match'])), intval($_POST['autodisc']), $_POST['recmode'], intval($_POST['readynum']), intval($_POST['slaves']), $_POST['replayer']);
      send('index.php');
   } else {
      $tpl->assign('error', $error);
   }
}
if(isset($_POST['append'])) {
   if(isset($_POST['recorder']) && isset($_POST['replayer'])) {
      $ettvd->livecast($_POST['recorder'], $_POST['replayer']);
      send('index.php');
   } else {
      send('broadcast.php?append');
   }
}



if(isset($_GET['append'])) {
   $ettvd->fetch_servers();

   $replayers = '';
   $tpl->assign('name', 'replayer[]');
   foreach($ettvd->replayers AS $rep) {
      $tpl->assign('value', $rep['num']);
      $replayers .= $tpl->fetch('bits/checkbox.tpl');
   }
   if(empty($replayers)) $replayers = 'No replayers running';
   $tpl->assign('replayers', $replayers);

   $recorders = '';
   $tpl->assign('name', 'recorder');
   foreach($ettvd->recorders AS $rec) {
      $tpl->assign('value', $rec['num']);
      $recorders .= $tpl->fetch('bits/radiobutton.tpl');
   }
   if(empty($recorders)) $recorders = 'No recorders running';
   $tpl->assign('recorders', $recorders);

   $tpl->assign('area', 'Append replayers to a recorder');
   $tpl->assign('body', $tpl->fetch('append.tpl'));
   require('finish.php');
}

$ettvd->fetch_replayers();
$replayerselect = '';
foreach($ettvd->replayers AS $repid => $server) {
   $tpl->assign('name', 'replayer[]');
   $tpl->assign('value', $repid);
   $replayerselect .= $tpl->fetch('bits/checkbox.tpl');
}
$tpl->assign('replayers', $replayerselect);
$tpl->assign('area', 'Broadcast Match');
$tpl->assign('body', $tpl->fetch('broadcast.tpl'));
require('finish.php');
?>