changeset 238:885ef4bdb0de gmap2

Rename variables, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 17 Mar 2014 01:23:20 +0200
parents d858383547c7
children 7fe37bb4faf9
files tools/makegmaps.php
diffstat 1 files changed, 187 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- a/tools/makegmaps.php	Mon Mar 17 01:21:24 2014 +0200
+++ b/tools/makegmaps.php	Mon Mar 17 01:23:20 2014 +0200
@@ -1,22 +1,25 @@
 #!/usr/bin/php
 <?php
 
+$gmapsConfig = "config.php";
+
+
 // Paths and files
-$binConvert = "convert";
-$binMercurial = "hg";
-$binMake = "make";
+$cfg = array();
+$cfg["binConvert"] = "convert";
+$cfg["binMercurial"] = "hg";
+$cfg["binMake"] = "make";
 
-$pathMapUtils = "maputils/";
+$cfg["pathMapUtils"] = "maputils/";
 $pathImageCache = "cache/";
 $pathMarkerData = "../";
 $pathTileData = "../tiles/";
 
-$pathRawMaps = $pathMapUtils."maps/";
-
-$rawSuffix = ".new";
-$rawAltSuffix = ".map";
 
 $fontFile = "./lucon.ttf";
+
+
+// Internal data and settings
 $tileDim = 256;
 
 $minZoom = 1;
@@ -28,18 +31,9 @@
 $fontSize[16] = 13;
 $fontSize[32] = 26;
 
-
-/* Internal data and settings
- */
-$locPath = $pathMapUtils."maps/";
-$worldJS = $pathMarkerData."world.js";
-$tradelaneOut = $pathMarkerData."tradelane.json";
-$tradelaneOverlay = $pathMarkerData."trlines.json";
-$binMkLoc = $pathMapUtils."mkloc";
-
 $modes = array(
   "xml"     => array("batclient.xml"     , 0, 0),
-  "json"    => array("markers.json"       , 0, 0),
+  "json"    => array("markers.json"      , 0, 0),
 );
 
 $mapPalette = array();
@@ -104,89 +98,201 @@
 }
 
 
-/*
- * Create htaccess files
- */
-function makeHtAccessFile($surlprefix, $sprefix, $spath)
+function stOutputToJSONFile($qfilename, $qdata)
+{
+  stOutputToFile($qfilename, json_encode($qdata));
+}
+
+
+// Calculate worldmap coordinates from continent coordinates
+function stGetWorldCoords($cont, $xp, $yp, &$xc, &$yc)
+{
+  global $worldMap, $continentList;
+
+  if (!isset($continentList[$cont]))
+    return FALSE;
+
+  $xc = $worldMap["ox"] + $continentList[$cont][1] + $xp - 1;
+  $yc = $worldMap["oy"] + $continentList[$cont][2] + $yp - 1;
+  return TRUE;
+}
+
+
+// Get worldmap coordinates for a given tradelane waypoint
+function stGetWaypointCoords($waypoint, &$xc, &$yc)
 {
-  $sfile = $sprefix.$spath.".htaccess";
-  if (!file_exists($sfile) && file_exists($sprefix.$spath."sea.png"))
+  global $tradelanePoints;
+  
+  if (!isset($tradelanePoints[$waypoint]))
+    return FALSE;
+
+  return stGetWorldCoords($tradelanePoints[$waypoint][0],
+    $tradelanePoints[$waypoint][1], $tradelanePoints[$waypoint][2], $xc, $yc);
+}
+
+
+function stYesNoPrompt($msg, $default = FALSE)
+{
+  echo $msg." [".($default ? "Y/n" : "y/N")."]? ";
+  $sprompt = strtolower(trim(fgets(STDIN)));
+
+  if ($default)
+    return ($sprompt == "n");
+  else
+    return ($sprompt == "y");
+}
+
+
+function stInputPrompt($msg, $default = FALSE, $validate = null)
+{
+  $valid = FALSE;
+  while (!$valid)
   {
-    if (!isset($surlprefix))
-    {
-      $sdone = FALSE;
-      while (!$sdone)
-      {
-        echo "Enter URL prefix for tiles:\n";
-        $surlprefix = trim(fgets(STDIN));
-        if (substr($surlprefix, 0, 7) != "http://" &&
-            substr($surlprefix, 0, 8) != "https://")
-        {
-          echo "Prefix must start with http:// or https://\n";
-        }
-        else
-        if (substr($surlprefix, -1) != "/")
-        {
-          echo "Prefix must end with /\n";
-        }
-        else
-        if (preg_match("/^https?:\/\/[a-zA-Z0-9]+[a-zA-Z0-9\/\.\:\-]*?\//", $surlprefix) === FALSE)
-        {
-          echo "Malformed URL (or atleast this silly regexp does not want it.\n";
-        }
-        else
-        {
-          echo "The URL prefix to be used is \"".$surlprefix."\", e.g.\n".
-            "htaccess to be created would be: \"".$surlprefix."tiles/".$spath."sea.png\"\n".
-            "Is this correct? [y/N]? ";
-          
-          $sprompt = strtolower(trim(fgets(STDIN)));
-          if ($sprompt == "y")
-            $sdone = TRUE;
-          
-          echo "\n";
-        }
-      }
-    }
-    
-    
+    echo $msg;
+    $sprompt = strtolower(trim(fgets(STDIN)));
+
+    if ($sprompt == "")
+      $sprompt = ($default !== FALSE ? $default : "");
+
+    $valid =  !is_callable($validate) || call_user_func($validate, $sprompt);
+  }
+  return $sprompt;
+}
+
+
+function stValidateURLPrefix($sprefix)
+{
+  if (substr($sprefix, 0, 7) != "http://" &&
+      substr($sprefix, 0, 8) != "https://")
+  {
+    echo "Prefix must start with http:// or https://\n";
+    return FALSE;
+  }
+  else
+  if (substr($sprefix, -1) != "/")
+  {
+    echo "Prefix must end with /\n";
+    return FALSE;
+  }
+  else
+  if (preg_match("/^https?:\/\/[a-zA-Z0-9]+[a-zA-Z0-9\/\.\:\-]*?\//", $sprefix) === FALSE)
+  {
+    echo "Malformed URL (or atleast this silly regexp does not want it.\n";
+    return FALSE;
+  }
+  else
+    return TRUE;
+}
+
+
+function stMakeHtAccessFile($urlprefix, $sprefix, $spath)
+{
+  global $firstRun;
+  $sfile = $sprefix.$spath.".htaccess";
+  if (($firstRun || !file_exists($sfile)) && file_exists($sprefix.$spath."sea.png"))
+  {
     echo "Creating ".$sfile."\n";
-    outputToFile($sfile, "ErrorDocument 404 ".$surlprefix."tiles/".$spath."sea.png\n");
+    stOutputToFile($sfile, "ErrorDocument 404 ".$urlprefix.$spath."sea.png\n");
   }
 }
 
-makeHtAccessFile(&$urlTilePrefix, $pathTileData, "");
-for ($i = $minZoom; $i <= $maxZoom; $i++)
-  makeHtAccessFile(&$urlTilePrefix, $pathTileData, $i."/");
 
 
-if (!file_exists($pathMapUtils))
+//
+// Check for first run
+//
+echo
+  "===========================================================\n".
+  "GMaps TNG bootstrap and update script by Ggr & Jeskko\n".
+  "===========================================================\n";
+  
+if (file_exists($gmapsConfig))
+{
+  $firstRun = FALSE;
+  include $gmapsConfig;
+}
+else
+{
+  $firstRun = TRUE;
+  echo
+    "It seems you are running this for the first time ..\n".
+    "You will be asked some information this time, which will be\n".
+    "and saved for later use in file '".$gmapsConfig."'\n";
+
+  $sdone = FALSE;
+  while (!$sdone)
+  {
+    $cfg["urlTilePrefix"] = stInputPrompt("Enter URL prefix for tiles:\n", FALSE, "stValidateURLPrefix");
+    $sdone = stYesNoPrompt("The URL prefix to be used is \"".$cfg["urlTilePrefix"]."\", e.g.\n".
+      "htaccess to be created would be: \"".$cfg["urlTilePrefix"].$spath."sea.png\"\n".
+      "Is this correct?");
+    echo "\n";
+  }
+
+  stOutputToFile($gmapsConfig, "<?\n\$cfg = ".var_export($cfg, TRUE)."\n?>");
+}
+
+
+//
+// Set rest of the paths etc
+//
+$pathRawMaps = $cfg["pathMapUtils"]."maps/";
+$pathLocFiles = $cfg["pathMapUtils"]."maps/";
+$worldConfig = $cfg["pathMapUtils"]."www/world.inc.php";
+
+$worldJS = $pathMarkerData."world.js";
+$tradelaneOut = $pathMarkerData."tradelane.json";
+$tradelaneOverlay = $pathMarkerData."trlines.json";
+$binMkLoc = $cfg["pathMapUtils"]."mkloc";
+
+$rawSuffix = ".new";
+$rawAltSuffix = ".map";
+
+
+//
+// Include continent and tradelane configuration 
+//
+if (!file_exists($worldConfig))
+  die("Required continent/tradelane configuration file '".$worldConfig."' not found.\n");
+
+require $worldConfig;
+
+
+//
+// Create htaccess files
+//
+stMakeHtAccessFile($cfg["urlTilePrefix"], $pathTileData, "");
+for ($i = $minZoom; $i <= $maxZoom; $i++)
+  stMakeHtAccessFile($cfg["urlTilePrefix"], $pathTileData, $i."/");
+
+
 //
 // Build maputils and fetch latest map data
 //
+if (!file_exists($cfg["pathMapUtils"]))
 {
   // If maputils does not exist, clone the repository
-  $tmp = $binMercurial." clone http://pupunen.net/hg/maputils/ ".escapeshellarg($pathMapUtils);
+  $tmp = $cfg["binMercurial"]." clone http://pupunen.net/hg/maputils/ ".escapeshellarg($cfg["pathMapUtils"]);
   echo "* $tmp\n";
   passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 
   // Clone th-libs
-  $tmp = $binMercurial." clone http://tnsp.org/hg/th-libs/ ".escapeshellarg($pathMapUtils."th-libs/");
+  $tmp = $cfg["binMercurial"]." clone http://tnsp.org/hg/th-libs/ ".escapeshellarg($cfg["pathMapUtils"]."th-libs/");
   echo "* $tmp\n";
   passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 }
 else
 {
-  $tmp = "cd ".escapeshellarg($pathMapUtils)." && ".$binMercurial." pull && ".$binMercurial." update";
+  $tmp = "cd ".escapeshellarg($cfg["pathMapUtils"])." && ".$cfg["binMercurial"]." pull && ".$cfg["binMercurial"]." update";
   echo "* $tmp\n";
   passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 
-  $tmp = "cd ".escapeshellarg($pathMapUtils."th-libs/")." && ".$binMercurial." pull && ".$binMercurial." update";
+  $tmp = "cd ".escapeshellarg($cfg["pathMapUtils"]."th-libs/")." && ".$cfg["binMercurial"]." pull && ".$cfg["binMercurial"]." update";
   echo "* $tmp\n";
   passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 }
 
-$tmp = "cd ".escapeshellarg($pathMapUtils)." && ".$binMake." ".escapeshellarg(basename($binMkLoc));
+$tmp = "cd ".escapeshellarg($cfg["pathMapUtils"])." && ".$cfg["binMake"]." ".escapeshellarg(basename($binMkLoc));
 echo "* $tmp\n";
 passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 
@@ -194,21 +300,11 @@
 if (!file_exists($binMkLoc))
   die($binMkLoc." not found. Maputils package not built, or some other error occured.\n");
 
-$tmp = "cd ".escapeshellarg($pathMapUtils)." && ".$binMake;
-passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
-
-$tmp = "cd ".escapeshellarg($pathRawMaps)." && ".$binMake." fetch 2> /dev/null";
+$tmp = "cd ".escapeshellarg($cfg["pathMapUtils"])." && ".$cfg["binMake"];
 passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 
-
-/*
- * Include continent and tradelane configuration 
- */
-$config = $pathMapUtils."www/world.inc.php";
-if (!file_exists($config))
-  die("Required continent/tradelane configuration file '$config' not found.\n");
-
-require $config;
+$tmp = "cd ".escapeshellarg($pathRawMaps)." && ".$cfg["binMake"]." fetch 2> /dev/null";
+passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
 
 
 //
@@ -253,7 +349,7 @@
     {
       // has a map
       $tmp .=
-      "-l ".escapeshellarg($locPath.$name.".loc")." ".
+      "-l ".escapeshellarg($pathLocFiles.$name.".loc")." ".
       "-c ".escapeshellarg($data[0])." ".
       "-x ".escapeshellarg($worldMap["ox"] + $data[1] + $mdata[1])." ".
       "-y ".escapeshellarg($worldMap["oy"] + $data[2] + $mdata[2])." ";
@@ -264,46 +360,11 @@
 }
 
 
-/* Calculate worldmap coordinates from continent coordinates
- */
-function getWorldCoords($cont, $xp, $yp, &$xc, &$yc)
-{
-  global $worldMap, $continentList;
-
-  if (!isset($continentList[$cont]))
-    return FALSE;
-
-  $xc = $worldMap["ox"] + $continentList[$cont][1] + $xp - 1;
-  $yc = $worldMap["oy"] + $continentList[$cont][2] + $yp - 1;
-  return TRUE;
-}
-
-
-/* Get worldmap coordinates for a given tradelane waypoint
- */
-function getWaypointCoords($waypoint, &$xc, &$yc)
-{
-  global $tradelanePoints;
-  
-  if (!isset($tradelanePoints[$waypoint]))
-    return FALSE;
-
-  return getWorldCoords($tradelanePoints[$waypoint][0],
-    $tradelanePoints[$waypoint][1], $tradelanePoints[$waypoint][2], $xc, $yc);
-}
-
-
-function outputToJSONFile($qfilename, $qdata)
-{
-  outputToFile($qfilename, json_encode($qdata));
-}
-
-
 //
 // Export tradelane waypoint data
 //
 if (!isset($tradelanePoints))
-  die("PHP array \$tradelanePoints not set, '$config' is old or incompatible.\n");
+  die("PHP array \$tradelanePoints not set, '$worldConfig' is old or incompatible.\n");
 
 echo "\nCreating tradelane waypoint data '".$tradelaneOut."' ...\n";
 
@@ -313,7 +374,7 @@
 {
   $html = "<b>TRADELANE WPT</b><br>".htmlentities($name);
 
-  if (!getWorldCoords($data[0], $data[1], $data[2], $xc, $yc))
+  if (!stGetWorldCoords($data[0], $data[1], $data[2], $xc, $yc))
     die("Invalid tradelane waypoint '".$name."', continent '".$data[0]."' not defined.\n");
 
   $qdata[] = array(
@@ -335,7 +396,7 @@
 //
 echo "\nCreating tradelane polyline data '".$tradelaneOverlay."' ...\n";
 if (!isset($tradelaneDefs))
-  die("PHP array \$tradelaneDefs not set, '$config' is old or incompatible.\n");
+  die("PHP array \$tradelaneDefs not set, '".$worldConfig."' is old or incompatible.\n");
 
 $qdata = array();
 foreach ($tradelaneDefs as $index => $points)
@@ -344,7 +405,7 @@
 
   foreach ($points as $point)
   {
-    if (!getWaypointCoords($point, $xc, $yc))
+    if (!stGetWaypointCoords($point, $xc, $yc))
       die("Invalid tradelane definition #$index: waypoint '".$point."' not defined.\n");
 
     $qline[] = array(
@@ -493,7 +554,7 @@
     echo "* ".$inFilename." -> ".$outFilename.": ";
     if ($inMtime > $outMtime)
     {
-      $tmp = escapeshellcmd($binConvert)." ".escapeshellarg($inFilename)." -scale ".escapeshellarg($scale."%")." -type Palette ".escapeshellarg($outFilename);
+      $tmp = escapeshellcmd($cfg["binConvert"])." ".escapeshellarg($inFilename)." -scale ".escapeshellarg($scale."%")." -type Palette ".escapeshellarg($outFilename);
       passthru($tmp) == 0 or die("Error executing: ".$tmp."\n");
       echo "OK\n";
     }