<?php

// release dir:
$release_protocol = "https";
// change below base and uri
//$release_base = "/home/happyman/public_html/gpx_map/";
//$release_uri = "tile.happyman.idv.tw/~happyman/gpx_map/";
// release page
$release_base = "/home/mountain/public_html/gpx_map/";
$release_uri = "map.happyman.idv.tw/~mountain/gpx_map/";
$release_webpath = sprintf("%s://%s",$release_protocol,$release_uri);

$releases = glob($release_base ."*/*.md5");
$outformat = "html";
if ((isset($argv[1])&& $argv[1] == 'json') ||(isset($_GET['type']) && $_GET['type'] == 'json')) {
	$outformat = "json";
}
$latest=1;
if (count($releases)>0) {
	krsort($releases);
	foreach($releases as $fname){
		if (preg_match("/.*\/(\w+)\/Happyman.map.md5/",$fname,$mat)){
			$version = $mat[1];
			$md5sum = ` cut -d " " -f 1 $fname`;
			$url = $release_webpath . $version ."/" . str_replace('md5','zip',basename($fname));
			$data[] = array("version"=>$version,"md5"=>trim($md5sum), "url"=>$url, "size"=> filesize(str_replace(".md5","",$fname)));
			if ($latest==1){
				$latest_map = str_replace('.zip','',$url);
				$latest_zip =  $url;
				$latest=0;
			}
		}
	}
}

if ($outformat == 'json') {
	echo json_encode($data);
	exit(0);
}
if (isset($_GET['type'])&& $_GET['type'] == 'locus') {
header("Content-type: application/xhtml+xml");
	printf("<?xml version='1.0' encoding='UTF-8'?>
<locusActions>
  <download>
    <source><![CDATA[%s ]]></source>
    <dest><![CDATA[/mapsVector/%s ]]></dest>
    <after>extract|deleteSource|refreshMap</after>
  </download>
</locusActions>\n\n",$latest_zip, basename($latest_zip));
	exit(0);
}
if (isset($_GET['type'])&& $_GET['type'] == 'zip') {
	header("Location: $latest_zip");
	exit(0);
}
if (isset($_GET['type'])&& $_GET['type'] == 'map') {
	header("Location: $latest_map");
	exit(0);
}
?>
<html>
<head>
<title>地圖產生器 GPX 離線圖資下載</title>
<style>
body {
font-family: sans-serif;
font-size: 1.5em;
}
</style>
</head>
<body>
<div style="width: 100%">
<div style="float:left;width: 50%">
<p>Happyman.map (mapsforge) 圖資來自<a href='https://twmap.happyman.idv.tw/map/'>地圖產生器</a>所蒐集之行跡檔，離線圖資的產出，要特別感謝魯地圖 Rudy 協助。本圖資與魯地圖共用 style 檔(2019-03-14之後版本)，並且可與魯地圖使用多地圖套疊。
<p>使用請先下載<a href='https://rudymap.tw/'>魯地圖</a>,並套用好style，再與本圖做套疊。
<h3>本圖資所提供的行跡山友貢獻，路線僅供參考之用，登山前請做好萬全準備，登山安全自行負責。不同意【請勿】下載。</h3>
<table>
<tr>
<td>
<a href='orux-map://<?php printf("%s",str_replace("https://","",$latest_zip)); ?>'>
<img src='https://rudymap.tw/images/OruxMaps.jpeg' title='Oruxmaps(歐魯妹) '/></a>
		<br>oruxmaps
<td>
<a href='gts-map://<?php printf("%s",str_replace("https://","",$latest_zip)); ?>'>
<img src='https://rudymap.tw/images/GTS.png' title='綠野遊蹤' /></a>
		<br>綠野遊蹤

<td>
<a href='wadi-map://<?php printf("%s",str_replace("https://","",$latest_zip)); ?>'>
<img src='https://rudymap.tw/images/wadi.png' width='128px' title='蛙弟'/></a>
		<br>蛙弟

<td>
<a href='locus-actions://<?php printf("https//%s?type=locus",$release_uri); ?>'>
<img src='https://rudymap.tw/images/Locus.jpeg' /></a>
<br>Locus (pro才能疊圖)
</tr>
<tr>
<td colspan=4 style='text-align: center'><img src='https://rudymap.tw/images/hj.png' />
							<br>
IOS 山林日誌 請複製以下網址: <?php echo $latest_zip; ?>
</tr>
</table>
<ul>
<?php
//printf("<li>最新版本: <a href='%s' target=_blank>Happyman.map</a> (壓縮檔: <a href='%s' target=_blank>Happyman.map.zip</a>)", $latest_map, $latest_zip);
foreach($data as $d){
	printf("<li>版本: %s (%s bytes) <a href='%s' target=_blank>下載</a>\n",$d['version'],$d['size'],$d['url']);
}

?>
</ul><hr>
<a href='https://www.youtube.com/playlist?list=PLhDHBZwRvarRzR3-tSeceQZqhVEflsUC4&fbclid=IwAR1KCz3jH8zQboiOOvt52WUKaZZ9sQ_7HR6r3iH6C2s_jvFuq0wdsu7QJos' target=_blank>圖資套疊參考</a>,<a href='https://www.facebook.com/groups/taiwan.topo/permalink/1217865971702347/' target=_blank>魯地圖社群協助</a>,機讀列表: <a href='<?php echo $release_webpath;?>?type=json'>release</a>,最新版本下載連結: <a href='<?php echo $release_webpath;?>?type=zip'>壓縮檔</a>,直接下載:<a href='<?php echo $release_webpath;?>?type=map'>Happyman.map</a>

</div>
<div style="float:right;">
<img src="oruxmaps.jpg" width=400px alt='image' />
</div>
</div>
</body></html>
