<%pre>

#include <setup.h>
#include <tools.h>
#include <users.h>
#include <live.h>

using namespace vdrlive;

</%pre>
<%args>
	std::string lastchannel;
	bool useauth = false;
	std::string login;
	std::string pass;
	std::string times;
	std::string channelGroups;
	std::string scheduleDuration;
	std::string startscreen;
	std::string theme;
	std::string localnetmask;
	std::string showLogo;
	std::string useAjax;
	std::string showInfoBox;
	std::string useStreamdev;
	std::string markNewRec;
	std::string showIMDb;
	std::string showChannelsWithoutEPG;
	std::string streamdevport;
	std::string streamdevtype;
	std::string streamVideoOpt0;
	std::string streamVideoOpt1;
	std::string streamVideoOpt2;
	std::string streamVideoOpt3;
	int authchanged = 0;
</%args>
<%session scope="global">
	bool logged_in(false);
</%session>
<%include>page_init.eh</%include>
<%cpp>
if (!logged_in && LiveSetup().UseAuth() ) return reply.redirect("login.html");
if (!cUser::CurrentUserHasRightTo(UR_EDITSETUP))
		throw HtmlError( tr("Sorry, no permission. Please contact your administrator!") );

#define SELECTIF(x) reply.out() << ( (x) ? "selected=\"selected\"" : "" );
#define CHECKIF(x) reply.out() << ( (x) ? "checked=\"checked\"" : "" );

	std::string message;

	if ( request.getMethod() == "POST") {
		if (authchanged && useauth && (login.empty() || pass.empty()))
			message = tr("Please set login and password!");
		else
		{
			LiveSetup().SetLastChannel(lastchannel != "" ? lexical_cast< int >(lastchannel):0);
			LiveSetup().SetUseAuth(useauth);
			if (useauth) {
				LiveSetup().SetAdminLogin(login);
				if (pass != "")
					LiveSetup().SetAdminPassword(pass);
				LiveSetup().SetLocalNetMask(localnetmask);
				LiveSetup().CheckLocalNet(request.getPeerIp());
			}
			LiveSetup().SetTimes(times);
			LiveSetup().SetChannelGroups(channelGroups);
			LiveSetup().SetScheduleDuration(scheduleDuration);
			LiveSetup().SetStartScreen(startscreen);
			LiveSetup().SetTheme(theme);
			LiveSetup().SetShowLogo(!showLogo.empty());
			LiveSetup().SetUseAjax(!useAjax.empty());
			if (LiveSetup().GetUseAjax()) {
				LiveSetup().SetShowInfoBox(!showInfoBox.empty());
			}
			LiveSetup().SetUseStreamdev(!useStreamdev.empty());
			LiveSetup().SetStreamdevPort(streamdevport.empty() ? 3000 : lexical_cast<int>(streamdevport));
			LiveSetup().SetStreamdevType(streamdevtype.empty() ? "PES" : streamdevtype);
			LiveSetup().SetMarkNewRec(!markNewRec.empty());

			std::string def = "ffmpeg -loglevel warning -f mpegts -analyzeduration 1.2M -probesize 5M "
				"-i <input> -map 0:v -map 0:a:0 -c:v copy -c:a aac -ac 2";
			LiveSetup().SetStreamVideoOpt0(streamVideoOpt0.empty() ? def : streamVideoOpt0);
			LiveSetup().SetStreamVideoOpt1(streamVideoOpt1.empty() ? def : streamVideoOpt1);
			LiveSetup().SetStreamVideoOpt2(streamVideoOpt2.empty() ? def : streamVideoOpt2);
			LiveSetup().SetStreamVideoOpt3(streamVideoOpt3.empty() ? def : streamVideoOpt3);

			LiveSetup().SetShowIMDb(!showIMDb.empty());
			LiveSetup().SetShowChannelsWithoutEPG(!showChannelsWithoutEPG.empty());
			LiveSetup().SaveSetup();

			message = tr("Setup saved.");
		}
	}
	pageTitle = tr("Setup");
	int ilastchannel = LiveSetup().GetLastChannel();
	if (ilastchannel == std::numeric_limits< int >::max())
		lastchannel = "";
	else
		lastchannel = lexical_cast<std::string, int>(ilastchannel);

	login = LiveSetup().GetAdminLogin();
	useauth = LiveSetup().GetUseAuth();
	times = LiveSetup().GetTimes();
	channelGroups = LiveSetup().GetChannelGroups();
	scheduleDuration = LiveSetup().GetScheduleDuration();
	startscreen = LiveSetup().GetStartScreen();
	theme = LiveSetup().GetTheme();
	localnetmask = LiveSetup().GetLocalNetMask();
	showLogo = LiveSetup().GetShowLogo() ? "1" : "";
	useAjax = LiveSetup().GetUseAjax() ? "1" : "";
	showInfoBox = LiveSetup().GetShowInfoBox() ? "1" : "";
	useStreamdev = LiveSetup().GetUseStreamdev() ? "1" : "";
	streamdevport = lexical_cast<std::string, int>(LiveSetup().GetStreamdevPort());
	streamdevtype = LiveSetup().GetStreamdevType();
	markNewRec = LiveSetup().GetMarkNewRec() ? "1" : "";
	streamVideoOpt0 = LiveSetup().GetStreamVideoOpt0();
	streamVideoOpt1 = LiveSetup().GetStreamVideoOpt1();
	streamVideoOpt2 = LiveSetup().GetStreamVideoOpt2();
	streamVideoOpt3 = LiveSetup().GetStreamVideoOpt3();
	showIMDb = LiveSetup().GetShowIMDb() ? "1" : "";
	showChannelsWithoutEPG = LiveSetup().GetShowChannelsWithoutEPG() ? "1" : "";
</%cpp>
<& pageelems.doc_type &>
<html>
	<head>
		<title>VDR-Live - <$ pageTitle $></title>
		<& pageelems.stylesheets &>
		<& pageelems.ajax_js &>
		<script type="text/javascript"><!--
		function initform()
		{
			changeduseauth(document.getElementById("useauth"));
			changeduseajax(document.getElementById("useAjax"));
			changedusestreamdev(document.getElementById("useStreamdev"));
			if (document.getElementById("message").value != "")
				alert(document.getElementById("message").value);
		}
		function changeduseauth(selection)
		{
			document.getElementById("authsection").style.display = (selection.checked ? "block" : "none");
		}
		function setauthchanged(selection)
		{
			document.getElementById("authchanged").value = 1;
		}
		function changeduseajax(selection)
		{
			document.getElementById("ajaxsection").style.display = (selection.checked ? "block" : "none");
		}
		function changedusestreamdev(selection)
		{
			document.getElementById("streamdevsection").style.display = (selection.checked ? "block" : "none");
		}
		//--></script>
	</head>
	<body onload="initform(); adjustHeader()" onresize="adjustHeader()">
		<& pageelems.logo &>
		<& menu active=("setup") &>
		<div class="inhalt">
			<form method="post" action="setup.html">
				<input type="hidden" name="authchanged" value="<$ authchanged $>" id="authchanged"/>
				<input type="hidden" name="message" value="<$ message $>" id="message"/>
				<table class="formular" cellpadding="0" cellspacing="0">
					<tr class="head">
						<td  class="toprow leftcol rightcol" colspan="2"><div class="boxheader"><div><div><$ tr("Setup") $></div></div></div></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Use authentication") $>:</div></td>
						<td class="rightcol">
							<input type="checkbox" name="useauth" id="useauth" value="1" <%cpp> CHECKIF(useauth); </%cpp>onclick="changeduseauth(this)"/>
							<div id="authsection" style="display: none">
							<table border="0" cellpadding="0" cellspacing="0" class="dependent">
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Admin login") $>:</div></td>
								<td><input type="text" name="login" value="<$ login $>" id="login" onchange="setauthchanged(this)" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Admin password") $>:</div></td>
								<td><input  type="password" name="pass" value="<$ pass $>" id="pass" onchange="setauthchanged(this)" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><a href="users.html"><$ tr("User management") $></a></div></td>
								<td/>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Local net (no login required)") $>:</div></td>
								<td><input  type="text" name="localnetmask" value="<$ localnetmask $>" id="localnetmask" /></td>
							</tr>
							</table>
							</div>
						</td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Show live logo image") $>:</div></td>
						<td class="rightcol">
							<input type="checkbox" name="showLogo" id="showLogo" value="1" <%cpp> CHECKIF(!showLogo.empty()); </%cpp>/>
						</td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Use ajax technology") $>:</div></td>
						<td class="rightcol">
							<input type="checkbox" name="useAjax" id="useAjax" value="1" <%cpp> CHECKIF(!useAjax.empty()); </%cpp>onclick="changeduseajax(this)"/>
							<div id="ajaxsection" style="display: none">
							<table border="0" cellpadding="0" cellspacing="0" class="dependent">
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Show dynamic VDR information box") $>:</div></td>
								<td><input type="checkbox" name="showInfoBox" id="showInfoBox" value="1" <%cpp> CHECKIF(!showInfoBox.empty()); </%cpp>/></td>
							</tr>
							</table>
							</div>
						</td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Allow video streaming") $>:</div></td>
						<td class="rightcol">
							<input type="checkbox" name="useStreamdev" id="useStreamdev" value="1" <%cpp> CHECKIF(!useStreamdev.empty()); </%cpp>onclick="changedusestreamdev(this)"/>
							<div id="streamdevsection" style="display: none">
							<table border="0" cellpadding="0" cellspacing="0" class="dependent">
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Web-Streaming h264") $>:</div></td>
								<td><input type="text" name="streamVideoOpt0" id="streamVideoOpt0" size="100" value="<$ streamVideoOpt0 $>" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Web-Streaming HEVC") $>:</div></td>
								<td><input type="text" name="streamVideoOpt1" id="streamVideoOpt1" size="100" value="<$ streamVideoOpt1 $>" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Web-Streaming MPEG2") $>:</div></td>
								<td><input type="text" name="streamVideoOpt2" id="streamVideoOpt2" size="100" value="<$ streamVideoOpt2 $>" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Web-Streaming others") $>:</div></td>
								<td><input type="text" name="streamVideoOpt3" id="streamVideoOpt3" size="100" value="<$ streamVideoOpt3 $>" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Streamdev server port") $>:</div></td>
								<td><input type="text" name="streamdevport" id="streamdevport" value="<$ streamdevport $>" /></td>
							</tr>
							<tr>
								<td class="label"><div class="withmargin"><$ tr("Streamdev stream type") $>:</div></td>
								<td><select name="streamdevtype" size="1" id="streamdevtype">
									<option value="TS" <%cpp> SELECTIF(streamdevtype == "TS") </%cpp>>TS</option>
									<option value="PES" <%cpp> SELECTIF(streamdevtype == "PES") </%cpp>>PES</option>
									<option value="PS" <%cpp> SELECTIF(streamdevtype == "PS") </%cpp>>PS</option>
									<option value="ES" <%cpp> SELECTIF(streamdevtype == "ES") </%cpp>>ES</option>
									<option value="EXT" <%cpp> SELECTIF(streamdevtype == "EXT") </%cpp>>EXT</option>
								</select></td>
							</tr>
							</table>
							</div>
						</td>
					</tr>
                                        <tr>
                                                <td class="label leftcol"><div class="withmargin"><$ tr("Mark new recordings") $>:</div></td>
                                                <td class="rightcol">
                                                        <input type="checkbox" name="markNewRec" id="markNewRec" value="1" <%cpp> CHECKIF(!markNewRec.empty()); </%cpp>/>
                                                </td>
                                        </tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Add links to IMDb") $>:</div></td>
						<td class="rightcol">
							<input type="checkbox" name="showIMDb" id="showIMDb" value="1" <%cpp> CHECKIF(!showIMDb.empty()); </%cpp>/>
						</td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Last channel to display") $>:</div></td>
						<td class="rightcol"><input type="text" name="lastchannel" value="<$ lastchannel $>" id="lastchannel" /></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Additional fixed times in 'What's on?'") $>:</div></td>
						<td class="rightcol"><input type="text" name="times" value="<$ times $>" id="times" />
							<& tooltip.help text=(tr("Format is HH:MM. Separate multiple times with a semicolon")) &></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Channel groups for MultiSchedule") $>:</div></td>
						<td class="rightcol"><input type="text" name="channelGroups" value="<$ channelGroups $>" id="channelGroups" />
							<& tooltip.help text=(tr("Separate channels with a comma ',', separate groups with a semi-colon ';'")) &></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Duration of MultiSchedule in hours") $>:</div></td>
						<td class="rightcol"><input type="text" name="scheduleDuration" value="<$ scheduleDuration $>" id="scheduleDuration" />
							</td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Show channels without EPG") $>:</div></td>
						<td class="rightcol">
							<input type="checkbox" name="showChannelsWithoutEPG" id="showChannelsWithoutEPG" value="1" <%cpp> CHECKIF(!showChannelsWithoutEPG.empty()); </%cpp>/>
						</td>
					</tr>

					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Start page") $>:</div></td>
						<td class="rightcol"><select name="startscreen" size="1" id="startscreen">
							<option value="whatsonnow" <%cpp> SELECTIF(startscreen == "whatsonnow") </%cpp>><$ trVDR("What's on now?") $></option>
							<option value="whatsonnext" <%cpp> SELECTIF(startscreen == "whatsonnext") </%cpp>><$ trVDR("What's on next?") $></option>
							<option value="schedule" <%cpp> SELECTIF(startscreen == "schedule") </%cpp>><$ trVDR("Schedule") $></option>
							<option value="multischedule" <%cpp> SELECTIF(startscreen == "multischedule") </%cpp>><$ tr("MultiSchedule") $></option>
							<option value="recordings" <%cpp> SELECTIF(startscreen == "recordings") </%cpp>><$ trVDR("Recordings") $></option>
							<option value="timers" <%cpp> SELECTIF(startscreen == "timers") </%cpp>><$ trVDR("Timers") $></option>
						</select></td>
					</tr>
					<tr>
						<td class="label leftcol"><div class="withmargin"><$ tr("Theme") $>:</div></td>
						<td class="rightcol"><select name="theme" size="1" id="theme">
<%cpp>
{
	const std::string dirPath(Plugin::GetResourceDirectory() + "/themes");
	cReadDir d(dirPath.c_str());
	struct dirent* e;
	std::string parent("..");
	std::string current(".");
	std::string cvsDir("CVS");
	while ((e = d.Next())) {
		if ((current == e->d_name) || (parent == e->d_name) || (cvsDir == e->d_name)) {
			continue;
		}
		/* Check if entry is a directory: I do not rely on e->d_type
		   here because on some systems it is allways DT_UNKNOWN. Also
		   testing for DT_DIR does not take into account symbolic
		   links to directories.
		*/
		struct stat buf;
		if ((stat((dirPath + "/" + e->d_name).c_str(), &buf) != 0) || (!S_ISDIR(buf.st_mode))) {
			continue;
		}
</%cpp>
							<option value="<$ e->d_name $>" <%cpp> SELECTIF(theme == e->d_name) </%cpp>><$ e->d_name $></option>
<%cpp>
	}
}
</%cpp>
						</select></td>
					</tr>
					<tr>
						<td class="buttonpanel leftcol rightcol bottomrow" colspan="2">
							<div class="withmargin">
							<button class="green" type="submit" name="save" ><$ tr("Save") $></button>
							</div>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</body>
</html>

<%include>page_exit.eh</%include>

