From d680a61c04daab76d1626a877f54055bade2e237 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <benjamin.renard@akka.eu>
Date: Mon, 11 Sep 2017 11:28:28 +0200
Subject: [PATCH] Add UpdateInfoVI

---
 CMakeLists.txt                  |   1 +
 scripts/UpdateInfoVI.sh         |   8 ++++++++
 src/DATA/MANAGER/AddLocalVI.php |   4 ++--
 src/DATA/MANAGER/UpdateInfo.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 119 insertions(+), 2 deletions(-)
 create mode 100755 scripts/UpdateInfoVI.sh
 create mode 100644 src/DATA/MANAGER/UpdateInfo.php

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1cddb3d..a2391b4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -58,6 +58,7 @@ add_subdirectory(tests)
 install(FILES "scripts/StartServer" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 install(FILES "scripts/DDServer.env" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 install(FILES "scripts/AddLocalVI.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
+install(FILES "scripts/UpdateInfoVI.sh" DESTINATION . PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
 install(DIRECTORY "src/CALLEXT/" DESTINATION bin/CALLEXT)
 install(DIRECTORY "src/DATA/MANAGER/" DESTINATION bin/DATAMANAGER)
 
diff --git a/scripts/UpdateInfoVI.sh b/scripts/UpdateInfoVI.sh
new file mode 100755
index 0000000..323bb8b
--- /dev/null
+++ b/scripts/UpdateInfoVI.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+BASEDIR=$(dirname "$0")
+
+. ${BASEDIR}/DDServer.env
+
+php ${DATAMANAGER}/UpdateInfo.php $@
+
diff --git a/src/DATA/MANAGER/AddLocalVI.php b/src/DATA/MANAGER/AddLocalVI.php
index 81c8299..b7b22cc 100644
--- a/src/DATA/MANAGER/AddLocalVI.php
+++ b/src/DATA/MANAGER/AddLocalVI.php
@@ -87,9 +87,9 @@
   if (!is_dir(location)) mkdir(location, 0775, true);
   echo location."\n";
   chdir(location);
-   if (!file_exists($times)) system("TimesUpdate -r ".$times." ".brief."_0*.nc");
+   if (!file_exists($times)) system(DDBASEBIN."/TimesUpdate -r ".$times." ".brief."_[0-9]*.nc");
    if (!file_exists($info_xml)) makeInfoXml();
-   if (!file_exists($info)) system("infoLocal2nc ".$info_xml." ".$info);
+   if (!file_exists($info)) system(DDBASEBIN."/infoLocal2nc ".$info_xml." ".$info);
    if (!file_exists("clean")) {
            copy (DDLIB."/Cash.template", $cache);         
            $SED = "sed 's/NAME/".brief."/g' ".DDLIB."/Clean.template > clean";
diff --git a/src/DATA/MANAGER/UpdateInfo.php b/src/DATA/MANAGER/UpdateInfo.php
new file mode 100644
index 0000000..c28edbe
--- /dev/null
+++ b/src/DATA/MANAGER/UpdateInfo.php
@@ -0,0 +1,108 @@
+<?php
+/** @file    updateInfo.php 
+*   @brief   Stand-Alone application Updates Start - Stop Info in  DD Base info files in workng directory
+*   @arg     full  prefix of info and times files
+*   
+*     
+*   @version  $Id: UpdateInfo.php 100 2016-04-20 08:26:17Z elena $
+*/ 
+
+
+date_default_timezone_set('UTC');
+
+ function days2MonthDay($oldDate)
+ { 
+      $year = substr($oldDate, 0, 4);
+      $day =  substr($oldDate, 4, 3);
+      $hour = substr($oldDate, 7, 2);
+      $min =  substr($oldDate, 9, 2);
+      $sec =  substr($oldDate, 11, 2);
+      $msec = substr($oldDate, 13, 3);
+      $newDate = date("Y-m-d",
+          strtotime("+$day days",strtotime("$year-01-01")))."T".$hour.":".$min.":".$sec.".".$msec."Z";
+
+  return $newDate;
+ } 
+
+ function updateTag($tagName, $tagValue)
+ {
+
+    global $dom;
+
+     $vi = $dom->getElementsByTagName("VI");
+ 
+     $Old = $vi->item(0)->getElementsByTagName($tagName)->item(0);
+     $New = $dom->createElement($tagName, $tagValue);
+     $vi->item(0)->replaceChild($New, $Old); 
+ }
+
+/*
+*           MAIN 
+*/
+            if($argc == 2) $infoXml = "$argv[1]_info.xml";
+            else exit(" info FULL prefix should be specified");
+
+            global $dom;        
+            
+            define("DDBASEBIN",getenv("DDBASEBIN")); 
+            $dom = new DomDocument();
+  
+            $path_parts = pathinfo($argv[1]);
+            $path = $path_parts['dirname']."/";
+            $currDir = getcwd();
+
+            if (!$dom->load($infoXml)) {             
+                chdir($path);
+                $dom->loadXML(file_get_contents($path_parts['basename']."_info.xml"));  // NFS connection doesn't load 
+                chdir($currDir);    
+            }
+ 
+// Read *_times.nc          
+            $res = system(DDBASEBIN."/StartStopLocal $argv[1]_times.nc");  
+            $Time = explode("-",$res);
+ // Start   
+            $Start = days2MonthDay($Time[0]);
+            updateTag('GlobalStart', $Start);
+            updateTag('LocalStart', $Start);        
+                             
+// Stop              
+            $Stop = days2MonthDay($Time[1]);
+            updateTag('GlobalStop', $Stop);
+            updateTag('LocalStop', $Stop);     
+                 
+            $dom->save($infoXml);
+ 
+// make *_info.nc                      
+            system(DDBASEBIN."/infoLocal2nc $infoXml $argv[1]_info.nc"); 
+                          
+// check if there is TimeRestriction
+	  
+            if (file_exists($path."TimeRestriction")) {
+                    chdir($path); 
+                    $restr = file("./TimeRestriction", FILE_IGNORE_NEW_LINES);
+   
+    //                 $days = $restr[0];
+                    $n = sscanf($restr[0], "%d-%d-%d", $yy, $mm, $dd);
+ 
+                    $infoXmlRestr = $restr[1].".xml";
+                    $infoNcRestr = $restr[1].".nc";
+                 
+                    if ($n == 1) {
+                        $restrStop = date("Y-m-d", strtotime("-$yy days"))."T23:59:59.999Z";
+                    }
+                    else {
+                        $restrStop = $restr[0]."T23:59:59.999Z";
+                    }
+
+                    if ($restrStop <  $Stop) {
+                        updateTag('GlobalStop', $restrStop);
+                        updateTag('LocalStop', $restrStop); 
+                    } 
+                
+                    $dom->save($infoXmlRestr);
+ 
+                    system(DDBASEBIN."/infoLocal2nc $infoXmlRestr $infoNcRestr");
+                    chdir($currDir);
+                }    
+        
+    ?>
--
libgit2 0.21.2