<?php

define('N',"\n");

function muoribene($msg,$ec) {
	echo($msg);
	exit($ec);
}

$inifp='mastostart.ini';
$conf=@parse_ini_file($inifp)
	or muoribene('Non ho potuto aprire «'.$inifp.'».'.N,1);
if (!array_key_exists('instpath',$conf))
	muoribene('«'.$inifp.'»: “instpath” non è definito.'.N,1);

$path=preg_replace('/^'.preg_quote($conf['instpath'],'/').'/','',$_SERVER['REQUEST_URI']);
$ndir=preg_match_all('/\//',$path)-1;
$prepath='';
for ($i=0; $i<$ndir; $i++)
	$prepath.='../';
if (strlen($path)>0) {
	while ($path[strlen($path)-1]=='/') {
		$path=substr($path,0,-1);
		if (strlen($path)<1)
			break;
	}
}
$supplangs=['ca','en','es','fr','it'];
if (preg_match('#/('.implode('|',$supplangs).')$#',$path,$lang)===1) {
	$dlang=$lang[1];
	$path=substr($path,0,-3);
} else {
	$dlang=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
	$dlang=in_array($dlang,$supplangs) ? $dlang : 'en';
}

$menu=array(
	'guide'=>array('liadd'=>null, 'href'=>$conf['instpath'].'/'.$dlang, 'title'=>'Guide', 'selected'=>false, 'submenu'=>null),
	'instances'=>array('liadd'=>null, 'href'=>$conf['instpath'].'/instances/'.$dlang, 'title'=>'Instances', 'selected'=>false, 'submenu'=>null),
	'about'=>array('liadd'=>null, 'href'=>$conf['instpath'].'/about/'.$dlang, 'title'=>'About', 'selected'=>false, 'submenu'=>null),
	'language'=>array('liadd'=>null, 'href'=>null, 'title'=>'Language', 'selected'=>false, 'submenu'=>array(
			'ca'=>array('liadd'=>'onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'Català', 'selected'=>false, 'submenu'=>null),
			'en'=>array('liadd'=>'onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'English', 'selected'=>false, 'submenu'=>null),
			'es'=>array('liadd'=>'onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'Español', 'selected'=>false, 'submenu'=>null),
			'fr'=>array('liadd'=>'onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'Français', 'selected'=>false, 'submenu'=>null),
			'it'=>array('liadd'=>'onmouseover="this.style.cursor=\'pointer\'"', 'href'=>null, 'title'=>'Italiano', 'selected'=>false, 'submenu'=>null)
		)
	)
);

foreach ($supplangs as $lang)
	$menu['language']['submenu'][$lang]['href']=$conf['instpath'].$path.'/'.$lang;

$menu['language']['submenu'][$dlang]['href']=null;
$menu['language']['submenu'][$dlang]['liadd']=null;
$menu['language']['submenu'][$dlang]['selected']=true;

switch ($dlang) {
	case 'ca':
	$menu['guide']['title']='Guia';
	$menu['instances']['title']='Instàncies';
	$menu['about']['title']='Info';
	$menu['language']['title']='Idioma';
	break;

	case 'en':
	$menu['guide']['title']='Guide';
	$menu['instances']['title']='Instances';
	$menu['about']['title']='About';
	$menu['language']['title']='Language';
	break;

	case 'es':
	$menu['guide']['title']='Guía';
	$menu['instances']['title']='Instancias';
	$menu['about']['title']='Info';
	$menu['language']['title']='Idioma';
	break;

	case 'fr':
	$menu['guide']['title']='Guide';
	$menu['instances']['title']='Instances';
	$menu['about']['title']='Infos';
	$menu['language']['title']='Langue';
	break;

	case 'it':
	$menu['guide']['title']='Guida';
	$menu['instances']['title']='Istanze';
	$menu['about']['title']='Info';
	$menu['language']['title']='Lingua';
	break;
}

$data=array(

	'ca.home.atit'=>'Guia',
	'ca.home.desc'=>'A thorough introduction to Mastodon', // needs translation
	'ca.instances.atit'=>'Instàncies',
	'ca.instances.desc'=>'Recommended Mastodon instances', // needs translation
	'ca.about.atit'=>'Info',
	'ca.about.desc'=>'Info about Mastodon Startpage’s authors, contributors, license', // needs translation

	'en.home.atit'=>'Guide',
	'en.home.desc'=>'A thorough introduction to Mastodon',
	'en.instances.atit'=>'Instancias',
	'en.instances.desc'=>'Recommended Mastodon instances',
	'en.about.atit'=>'About',
	'en.about.desc'=>'Info about Mastodon Startpage’s authors, contributors, license',

	'es.home.atit'=>'Guide',
	'es.home.desc'=>'A thorough introduction to Mastodon', // needs translation
	'es.instances.atit'=>'Instances',
	'es.instances.desc'=>'Recommended Mastodon instances', // needs translation
	'es.about.atit'=>'Info',
	'es.about.desc'=>'Info about Mastodon Startpage’s authors, contributors, license', // needs translation

	'fr.home.atit'=>'Guía',
	'fr.home.desc'=>'A thorough introduction to Mastodon', // needs translation
	'fr.instances.atit'=>'Instances',
	'fr.instances.desc'=>'Recommended Mastodon instances', // needs translation
	'fr.about.atit'=>'Infos',
	'fr.about.desc'=>'Info about Mastodon Startpage’s authors, contributors, license', // needs translation

	'it.home.atit'=>'Guida',
	'it.home.desc'=>'Una approfondita introduzione a Mastodon',
	'it.instances.atit'=>'Istanze',
	'it.instances.desc'=>'Istanze Mastodon consigliate',
	'it.about.atit'=>'Info',
	'it.about.desc'=>'Informazioni sugli autori, i collaboratori e la licenza di Mastodon Startpage'

);

switch($path) {

	case '':
	$cont=array(
		'fp'=>'home_'.$dlang.'.php',
		'atit'=>' - '.$data[$dlang.'.home.atit'],
		'desc'=>$data[$dlang.'.home.desc'],
		'js'=>array('shsum','guideanchors','scrolltrack')
	);
	$menu['guide']['liadd']='onclick="shsum()" onmouseover="this.style.cursor=\'pointer\'; this.style.textDecoration=\'underline\'" onmouseout="this.style.textDecoration=\'none\'"';
	$menu['guide']['href']=null;
	$menu['guide']['selected']=true;
	break;

	case '/instances':
	$cont=array(
		'fp'=>'instances_'.$dlang.'.php',
		'atit'=>' - '.$data[$dlang.'.instances.atit'],
		'desc'=>$data[$dlang.'.instances.desc'],
		'js'=>array()
	);
	$menu['instances']['href']=null;
	$menu['instances']['selected']=true;
	break;

	case '/about':
	$cont=array(
		'fp'=>'about_'.$dlang.'.php',
		'atit'=>' - '.$data[$dlang.'.about.atit'],
		'desc'=>$data[$dlang.'.about.desc'],
		'js'=>array()
	);
	$menu['about']['href']=null;
	$menu['about']['selected']=true;
	break;

	default:
	$cont=array(
		'fp'=>'404.php',
		'atit'=>' - 404',
		'desc'=>'Page not found',
		'js'=>array()
	);
	break;
}

/*
 <li><a href="<?php echo($conf['instpath']); ?>">Guide</a></li>
 <li><a href="instances">Instances</a></li>
 <li><a href="about">About us</a></li>
 <li onmouseover="chulsh(this,true)" onmouseout="chulsh(this,false)">Language
  <ul class="ula" onmouseover="ulsh(this,true)" onmouseout="ulsh(this,false)">
  <li><a href="#">English</a></li>
  <li><a href="#">Italiano</a></li>
  </ul>
 </li>
*/

$menuout='';

function buildmenu($menu) {
	global $menuout;
	foreach ($menu as $key=>$arr) {
		$menuout.='<li';
		if (!is_null($arr['liadd'])) $menuout.=' '.$arr['liadd'];
		if (!is_null($arr['submenu'])) $menuout.=' onmouseover="chulsh(this,true)" onmouseout="chulsh(this,false)"';
		if ($arr['selected']) $menuout.=' class="hil"';
		$menuout.='>';
		if (!is_null($arr['href']))
			$menuout.='<a href="'.$arr['href'].'">'.$arr['title'].'</a>';
		else
			$menuout.=$arr['title'];
		if (!is_null($arr['submenu'])) {
// qui bisognerebbe aggiungere che a seconda del "livello" imposta class giusta: ula, oppure ulb per livello > 1
			$menuout.=N.'<ul class="ula" onmouseover="ulsh(this,true)" onmouseout="ulsh(this,false)">'.N;
			buildmenu($arr['submenu']);
			$menuout.='</ul>'.N;
		}
		$menuout.='</li>'.N;
	}
}

buildmenu($menu);

//echo(htmlentities($menuout));

$cjrand=rand(0,999999);

?>
<!DOCTYPE HTML>
<html lang="<?php echo($dlang); ?>">
<head>
<title>Mastodon Startpage<?php echo($cont['atit']); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="<?php echo($cont['desc']); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="icon" type="image/png" href="<?php echo($prepath); ?>imgs/icona-32.png" sizes="32x32">
<link rel="icon" type="image/png" href="<?php echo($prepath); ?>imgs/icona-192.png" sizes="192x192">
<link rel="icon" type="image/png" href="<?php echo($prepath); ?>imgs/icona-512.png" sizes="512x512">
<link rel="apple-touch-icon-precomposed" href="<?php echo($prepath); ?>imgs/icona-180.png">
<link rel="stylesheet" type="text/css" href="<?php echo($prepath); ?>theme.css?v=<?php echo($cjrand); ?>">
<?php
foreach ($cont['js'] as $val)
	echo('<script language="JavaScript" src="js/'.$val.'.js?v='.$cjrand.'"></script>'.N);
?>
<script language="JavaScript">
<!--
function chulsh(el,sh) {
	if (sh)
		el.querySelector('ul').style='display:block';
	else
		el.querySelector('ul').style='display:none';
}
function ulsh(el,sh) {
	if (sh)
		el.style='display:block';
	else
		el.style='display:none';
}
//-->
</script>
</head>
<?php
require($cont['fp']);
?>
</html>
