#!/usr/bin/php
<?php
/*
    tools/yapp
    YeAPF 0.8.60-153 built on 2018-06-26 07:22 (-3 DST)
    Copyright (C) 2004-2018 Esteban Daniel Dortta - dortta@yahoo.com
    2018-06-19 23:48:48 (-3 DST)
*/

  $mydir=dirname($_SERVER['SCRIPT_FILENAME']);
  $cmRequired=false;
  (@include_once "$mydir/yclilib.php") or die("yclilib.php not found\n");
  if (file_exists(".config/yeapf.config"))
    (@include_once ".config/yeapf.config") or die("Error loading '.config/yeapf.config'");

  echo basename("tools/yapp")."\nYeAPF 0.8.60 tools\nCopyright (C) 2004-2018 Esteban Daniel Dortta - dortta@yahoo.com\n\n";

  $args = new ArgumentsProcessor();
  $appFolder=$args->getSrc(0);
  $toHelp   =$args->argValue('help',__FALSE__)==__TRUE__;
  $toForce  =$args->argValue('force',__FALSE__)==__TRUE__;
  $toUpdate =$args->argValue('update',__FALSE__)==__TRUE__;
  $appType  =$args->argValue('appType','');
  $template =$args->argValue('template','');
  $toCreate =$args->argValue('create','');
  $YFolder  =$args->argValue('source',$__yeapfPath);

  /* Since 0.8.60 yapp saves the location of the projetcs in __yeapfPath/.projects folder.
     This is like this in order to help yapp to update/list all the projects at a time */

  if (!is_dir("$__yeapfPath/.projects")) {
    if (!mkdir("$__yeapfPath/.projects"))
      die("You have not enough rights to create '$__yeapfPath/.projects'\n");
  }

  if (trim($appFolder)=='')
    $appFolder="./";

  if (!is_writable(dirname("$appFolder"))) {
    $appHolder = dirname($appFolder);
    die("You have not enough rights to write in '$appHolder'\n");
  }

  if (is_dir("$appFolder/.config"))
    if (!is_writable("$appFolder/.config"))
      die("You don't have enough rights to write in '$appFolder/.config'\n");

  $yeapfAppIniFilename="$appFolder/.config/yeapf-tools.ini";

  /*  backguard compatibility
      transform yTemplate.txt in yeapf-tools.ini
    */
  if (file_exists("$appFolder/.config/yTemplate.txt")) {
    if (($appType=='') || ($appType=='webApp')) {
      /* if yTemplate.txt exists, it´s a webApp */
      $appType='webApp';
      $templateUsed=preg_replace('/[\x00-\x1F\x80-\xFF]/', '', join('',file("$appFolder/.config/yTemplate.txt")));

      /* upgrade file structure */
      $yeapfAppIni="[app]\nappType=webApp\ntemplate=$templateUsed\n";
      if (file_put_contents("$yeapfAppIniFilename",$yeapfAppIni))
        if (!unlink("$appFolder/.config/yTemplate.txt"))
          die("Was not possible to delete '$appFolder/.config/yTemplate.txt'\n");
      else
        die("Was not possible to create '$yeapfAppIniFilename' file\n");

      /* the new template need to be equals to the old one in order to update */
      if (($template>'') && ($templateUsed!=$template))
        die("Your folder is configured to use '$templateUsed' template.\n\n");
      else {
        $template=$templateUsed;
      }
    } else {
      die("This folder is a webApp");
    }
  }

  /* load current configuration */
  if (($appType=='') && ($template=='')) {
    if (file_exists($yeapfAppIniFilename)) {
      $yeapfAppIni=parse_ini_file($yeapfAppIniFilename);
      $appType=$yeapfAppIni['appType'];
      $template=$yeapfAppIni['template'];
      if (!is_writeable($yeapfAppIniFilename))
        die("You have not enough rights to write '$yeapfAppIniFilename'\n");
    }
  }

  $toHelp = $toHelp || (!(($toUpdate) || ($toCreate)));

  if (($appType=='') || ($toHelp) || ($appFolder=='')) {
    echo "simple usage:\n\t".basename("tools/yapp")." [app-folder] [options]\n\n";
    echo "options:\n";
    echo "\t--create\n";
    echo "\t--force\n";
    echo "\t--update\n";
    echo "\t--source <YeAPF source folder>\n\t\tDefaults to '$__yeapfPath'\n";
    echo "\t--appType <app-type-name>\n";
    if (is_dir("$YFolder/skel")) {
      $d=dir("$YFolder/skel");
      while ($entry=$d->read()) {
        if (substr($entry,0,1)!='.')
          echo "\t\t$entry\n";
      }
    } else {
      echo "\t\tYour source of distribution in '$YFolder' does not contain any skeleton\n";
    }
    echo "\t--template <template-name>\t(applies to webApp)\n";
    if (is_dir("$YFolder/skel/webApp/templates")) {
      $d=dir("$YFolder/skel/webApp/templates");
      while ($entry=$d->read()) {
        if (substr($entry,0,1)!='.')
          echo "\t\t$entry\n";
      }
    } else {
      echo "\t\tYour source of distribution in '$YFolder/skel/webApp/templates' does not contain any template\n";
    }

    echo "\n* app-folder defaults to current folder if .config exists\n";

    if ($appFolder=='') {
      echo "\nYou need to declare an 'app-folder' name.\nIf it not exists, we will create it\n";

      if ($template=='')
        echo "\nYou need to chose a template\n";
    } else {
      echo "\nCurrent config:\n\tapp-folder:\t$appFolder\n\tappType:\t$appType\n\ttemplate:\t$template\n";
    }
    die("\n\n");
  }

  /* grant app folders */
  if (!is_dir($appFolder))
    if (!mkdir($appFolder,0777))
      die("You have not enough rights to create '$appFolder'\n");
  chmod($appFolder, 0777);

  if (!is_dir("$appFolder/.config"))
    if (!mkdir("$appFolder/.config", 0777, true))
      die("\nWas not possible to create '$appFolder/.config'\n");

  if (!is_writable($appFolder))
    die("'$appFolder' is not writable\n");

  /* clean current environment */
  $environmentFiles=array("yeapf.php", "configure.md5", "WebSocketEmptyImplementation.php");
  foreach ($environmentFiles as $key => $fileName) {
    if (file_exists("$appFolder/$fileName"))
      if (!unlink("$appFolder/$fileName"))
        die("You cannot delete '$appFolder/$fileName'\n");
  }

  /* granting personalized yeapf.db.ini */
  if (!file_exists("$appFolder/yeapf.db.ini")) {
    if (file_exists("$YFolder/skel/webApp/yeapf.db.ini")) {
      $aux=join("",file("$YFolder/skel/webApp/yeapf.db.ini"));
      $aux=str_replace('%(appFolder)',$appFolder,$aux);

      $newAppRegistry=md5(uniqid('YeAPF-app',true));
      $aux=str_replace('%(newAppRegistry)',$newAppRegistry,$aux);

      $f=fopen("$appFolder/yeapf.db.ini","w");
      fwrite($f, $aux);
      fclose($f);
      echo "\n+------------------------------------";
      echo "\n| Adjust database connection in \n| '$appFolder/yeapf.db.ini' first\n| then navigate to 'configure.php'";
      echo "\n+------------------------------------\n";
    } else
      echo "$YFolder/skel/webApp/yeapf.db.ini not found in skel/webApp folder!\n";
  } else
    echo "yeapf.db.ini not touched\n";

  $exts   = array("css", "xml", "php",   "html", "js",  "json");
  $folders= array("css", "js",  "fonts", "img",  "lib", "www" );

  if ($toCreate)
    echo "Installing:\n";
  else {
    if ($toForce)
      echo "Forcing update:\n";
    else
      echo "Updating:\n";
  }

  /* Update/Install files */
  foreach($exts as $e) {
    updateFiles("$YFolder/skel/$appType", "*.$e", "$appFolder", ($toCreate || $toForce));
  }

  /* Update/Install support folders */
  foreach($folders as $folderName) {
    updateFiles("$YFolder/skel/$appType/$folderName", "*", "$appFolder/$folderName", ($toCreate || $toForce));
  }

  if (is_dir("$appFolder/includes")) {
    updateFiles("$YFolder/includes", "*", "$appFolder/includes", ($toCreate || $toForce));
  }

  if (is_dir("$appFolder/YeAPF")) {
    updateFiles("$YFolder", "*", "$appFolder/YeAPF", ($toCreate || $toForce));
  }

  if (!file_exists("$appFolder/search.path"))
    $mySearchPath[]="$YFolder";
  else {
    $mySearchPath=file("$appFolder/search.path");
    foreach($mySearchPath as $i=>$path)
      if (strpos($path, "YeAPF"))
        $mySearchPath[$i]=$YFolder;
  }

  echo "Updating $appFolder/search.path\n";
  $f=fopen("$appFolder/search.path","w");
  foreach($mySearchPath as $i=>$path)
    fputs($f, $path);
  fclose($f);

  /* update/install template */
  if ($template>'') {
    updateFiles("$YFolder/skel/webApp/templates/$template", "*", "$appFolder/", ($toCreate || $toForce));
  }

  /* save application tools environment */
  $yeapfAppIni="[app]\nappType=$appType\ntemplate=$template\nfolder=".getcwd()."\ndate=".date("Y-m-dTH:i:s")."\nyeapfVersion=0.8.60-153\n";
  if (!file_put_contents("$yeapfAppIniFilename",$yeapfAppIni)) {
    echo "\nWARNING!\n\tWas not possible to write '$yeapfAppIniFilename'\n";
  }

  copy($yeapfAppIniFilename, "$__yeapfPath/.projects/".str_replace("/", "_", getcwd()).".ini");

  if ($toCreate) {
    echo "YeAPF app ($appType) called '$appFolder' created.\nModify 'yeapf.db.ini' and then run 'configure.php' from your browser\n";
  } else if ($toUpdate) {
    echo "YeAPF app ($appType) updated at $appFolder\n";
    if ( (isset($yeapfConfig)) && (isset($yeapfConfig['myself'])) ) {
      $cfgURL="http://localhost/".$yeapfConfig['myself']."/configure.php";
      echo "Calling '$cfgURL' ...\n";
      file_get_contents($cfgURL);
      echo "Ok\nThis folder was updated to YeAPF v0.8.60-153\n";
    } else {
      echo "Remember to call 'configure.php' from your navigator\n";
    }
  }

?>
