<?php

/* 
 * 
 * 
 * 
 */
require_once 'asyncRequst.php';
require_once 'aSynchronousOsRequests.php';

class OSLinux {

    public static function getCmdForStatisticReq($username) {
        $v2 = $_REQUEST['v2'] ?? null;
        $cmd = "php parseUtils.php \"r\" \"getStatistic\" \"$username\" \"$v2\" > /dev/null &";
        return $cmd;
    }

    public static function getCmdForGetPieReq($username, $pieName, $isAdmin) {
        $v2 = $_REQUEST['v2'] ?? null;
        return $cmd = "php StatisticPageEng.php \"r\" \"$username\" \"$pieName\" \"$isAdmin\" \"$v2\" > /dev/null &";
    }

    public static function getCmdForTrendReq($username, $trendName, $isAdmin) {
        $v2 = $_REQUEST['v2'] ?? null;
        $cmd = "php proSentPageEng.php \"r\" \"$username\" \"$trendName\" \"$isAdmin\" \"$v2\" > /dev/null &";
        return $cmd;
    }

    public static function getCmdForSendDocReq($i, $user, $serverIP, $port, $file_id, $isEmail) {
        global $PHP_FOLDER;
        $cmd = "php senddocEngAjx.php $PHP_FOLDER \"$i\" \"$user\" \"$serverIP\" \"$port\" \"$file_id\" \"0\" \"$isEmail\" >  /dev/null &";
        return $cmd;
    }

    public static function getCmdForBackgroundReq($arr, $getmetadata) {
        $cmd = "php background.php $arr[docId] \"$getmetadata\" &";
        return $cmd;
    }

    public static function getCmdForCrawler($twitterBK){
        $cmd = "php tools".DIRECTORY_SEPARATOR."cowitter".DIRECTORY_SEPARATOR."cli_runner.php filter_stream_db_twitter $twitterBK > //dev//null 2>//dev//null &";
        return $cmd;
    }
    
    public static function getCmdForFilter(){
        $cmd = "php tools".DIRECTORY_SEPARATOR."cowitter".DIRECTORY_SEPARATOR."cli_runner.php filter_and_parse_db_ui > //dev//null 2>//dev//null &";
        return $cmd;
    }
    
    public static function getCmdForPhpPage($isBackground, $pageName, $args) {
        $cmd = "php $pageName \"" . join("\" \"", $args) . "\"";
        if ($isBackground) {
            $cmd = "$cmd &";
        }
        return $cmd;
    }

    public static function getSolrCoreCommand($SOLR_COLLECTION) {
        return "sudo -u solr /opt/solr-5.4.1/bin/solr create -c $SOLR_COLLECTION";
    }
	
	public static function wrapCmd($cmd, $add_null = null){
	    $null_pointer = $add_null ? " > /dev/null" : "";
            return "$cmd $null_pointer &";
    }

    public static function getDirectoryIterator($folder) {
        http_response_code(501); //not implemented 
        return die("{ \"errors\" : [\"getDirectoryIterator not yet implemented on linux\"] } ");
    }
    
    public static function getOfficePath(){
        global $sofficePath;
        putenv("LANG=en_US.UTF-8");
        if (is_dir($sofficePath['lin'])){
            return $sofficePath['lin'].'/soffice';
        }
        write_to_log("ERROR", "LibreOffice isn't installed on your linux machine");      
        return null;
        
    }
    
    public function exec_run($CURRENT_SOURCE_FOLDER, $cmd, $letter = null){
        exec($cmd);
    }
    
    public function getCMDPdfConvert(){
        $cmd = "gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=".$outputPath."_new ".$path." 2&1> /dev/null";
        return $cmd;
    }
    
    public function copyFolder($origDir, $destDir){
        $cmd = "cp -R $origDir $destDir";
        exec($cmd);
        
    }
    
    public function rmDir($dir){
        exec("rm -r $dir");
    }
    
}
