<?php
/*
* env.php
* http://tasuke.dna.affrc.go.jp/
*
* Author: Ryutaro Itoh
*
* Copyright (c) 2013 National Institute of Agrobiological Sciences   
*
*/

require '../conf/config.php';
require 'globalVars.php';
require './commonDB.php';

//////////////////////////////////////////////////////////
$dbmode = "svcf";
if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
} else {
    $dbmode = getDbMode($conn);
}

// Only at first launch, create temporary directory for TASUKE
if(isset($tempdir) && !empty($tempdir) && !file_exists($tempdir)) {
	if(mkdir($tempdir, 0700)) { chmod($tempdir, 0700); };
}

$str= "{\"backend\":\"".$backend
    ."\",\"threadnum\":\"".$threadnum
//    ."\",\"threadnumTrack\":\"".$threadnumTrack
    ."\",\"eff_col\":\"".$EFFcol
    ."\",\"ann_col\":\"".$ANNcol
//    ."\",\"min_chr_switch_to_search_form\":\"".$minChrSwitchToSeacrhForm
    ."\",\"reference_name\":\"".$reference
    ."\",\"chromosome_name\":\"".$chromosome_name
    ."\",\"external_link\":\"".$external_link
    ."\",\"external_link_tag_gff\":\"".$external_link_tag_gff
    ."\",\"external_link_suffix\":\"".$external_link_suffix
    ."\",\"nuc_max\":\"".$nuc_max
    ."\",\"cstm_track\":\"".$customTrack
    ."\",\"cstm_max\":\"".$cstm_max
    ."\",\"cstm_min\":\"".$cstm_min
    ."\",\"cstm_name\":\"".$customTrackName
    ."\",\"cstm_kind\":\"".$customTrackKindofData
    ."\",\"cstm_norm\":\"".$customTrackNormByRead
    ."\",\"default_cond\":\"".$defaultCond
    ."\",\"snp_max_col\":\"".$snp_max_col
    ."\",\"snp_min_col\":\"".$snp_min_col
    ."\",\"indel_max_col\":\"".$indel_max_col
    ."\",\"indel_min_col\":\"".$indel_min_col
    ."\",\"depth_max_col\":\"".$depth_max_col
    ."\",\"depth_min_col\":\"".$depth_min_col
    ."\",\"cstm_max_col\":\"".$cstm_max_col
    ."\",\"cstm_min_col\":\"".$cstm_min_col
    ."\",\"accession_cvs_width\":\"".$accession_cvs_width
    ."\",\"title_id_width_rate\":\"".$title_id_width_rate
    ."\",\"color_acc_group\":\"".$color_acc_group
    ."\",\"cstm_max_col_para1\":\"".$cstm_max_col_para1
    ."\",\"cstm_min_col_para1\":\"".$cstm_min_col_para1
    ."\",\"cstm_max_col_para2\":\"".$cstm_max_col_para2
    ."\",\"cstm_min_col_para2\":\"".$cstm_min_col_para2
    ."\",\"cstm_max_col_para3\":\"".$cstm_max_col_para3
    ."\",\"cstm_min_col_para3\":\"".$cstm_min_col_para3
    ."\",\"depth_max\":\"".$depth_max
    ."\",\"depth_min\":\"".$depth_min
    ."\",\"snp_min\":\"".$snp_min
    ."\",\"indel_min\":\"".$indel_min
    ."\",\"snp_100k\":\"".$snp_100k
    ."\",\"snp_90k\":\"".$snp_90k
    ."\",\"snp_80k\":\"".$snp_80k
    ."\",\"snp_70k\":\"".$snp_70k
    ."\",\"snp_60k\":\"".$snp_60k
    ."\",\"snp_50k\":\"".$snp_50k
    ."\",\"snp_40k\":\"".$snp_40k
    ."\",\"snp_30k\":\"".$snp_30k
    ."\",\"snp_20k\":\"".$snp_20k
    ."\",\"snp_10k\":\"".$snp_10k
    ."\",\"snp_9k\":\"".$snp_9k
    ."\",\"snp_8k\":\"".$snp_8k
    ."\",\"snp_7k\":\"".$snp_7k
    ."\",\"snp_6k\":\"".$snp_6k
    ."\",\"snp_5k\":\"".$snp_5k
    ."\",\"snp_4k\":\"".$snp_4k
    ."\",\"snp_3k\":\"".$snp_3k
    ."\",\"snp_2k\":\"".$snp_2k
    ."\",\"snp_1k\":\"".$snp_1k
    ."\",\"snp_900b\":\"".$snp_900b
    ."\",\"snp_800b\":\"".$snp_800b
    ."\",\"snp_700b\":\"".$snp_700b
    ."\",\"snp_600b\":\"".$snp_600b
    ."\",\"snp_500b\":\"".$snp_500b
    ."\",\"snp_400b\":\"".$snp_400b
    ."\",\"snp_300b\":\"".$snp_300b
    ."\",\"snp_200b\":\"".$snp_200b
    ."\",\"snp_100b\":\"".$snp_100b
    ."\",\"snp_90b\":\"".$snp_90b
    ."\",\"snp_80b\":\"".$snp_80b
    ."\",\"snp_70b\":\"".$snp_70b
    ."\",\"snp_60b\":\"".$snp_60b
    ."\",\"snp_50b\":\"".$snp_50b
    ."\",\"snp_40b\":\"".$snp_40b
    ."\",\"snp_30b\":\"".$snp_30b
    ."\",\"snp_20b\":\"".$snp_20b
    ."\",\"snp_10b\":\"".$snp_10b
    ."\",\"snp_9b\":\"".$snp_9b
    ."\",\"snp_8b\":\"".$snp_8b
    ."\",\"snp_7b\":\"".$snp_7b
    ."\",\"snp_6b\":\"".$snp_6b
    ."\",\"snp_5b\":\"".$snp_5b
    ."\",\"snp_4b\":\"".$snp_4b
    ."\",\"snp_3b\":\"".$snp_3b
    ."\",\"snp_2b\":\"".$snp_2b
    ."\",\"snp_1b\":\"".$snp_1b
    ."\",\"indel_100k\":\"".$indel_100k
    ."\",\"indel_90k\":\"".$indel_90k
    ."\",\"indel_80k\":\"".$indel_80k
    ."\",\"indel_70k\":\"".$indel_70k
    ."\",\"indel_60k\":\"".$indel_60k
    ."\",\"indel_50k\":\"".$indel_50k
    ."\",\"indel_40k\":\"".$indel_40k
    ."\",\"indel_30k\":\"".$indel_30k
    ."\",\"indel_20k\":\"".$indel_20k
    ."\",\"indel_10k\":\"".$indel_10k
    ."\",\"indel_9k\":\"".$indel_9k
    ."\",\"indel_8k\":\"".$indel_8k
    ."\",\"indel_7k\":\"".$indel_7k
    ."\",\"indel_6k\":\"".$indel_6k
    ."\",\"indel_5k\":\"".$indel_5k
    ."\",\"indel_4k\":\"".$indel_4k
    ."\",\"indel_3k\":\"".$indel_3k
    ."\",\"indel_2k\":\"".$indel_2k
    ."\",\"indel_1k\":\"".$indel_1k
    ."\",\"indel_900b\":\"".$indel_900b
    ."\",\"indel_800b\":\"".$indel_800b
    ."\",\"indel_700b\":\"".$indel_700b
    ."\",\"indel_600b\":\"".$indel_600b
    ."\",\"indel_500b\":\"".$indel_500b
    ."\",\"indel_400b\":\"".$indel_400b
    ."\",\"indel_300b\":\"".$indel_300b
    ."\",\"indel_200b\":\"".$indel_200b
    ."\",\"indel_100b\":\"".$indel_100b
    ."\",\"indel_90b\":\"".$indel_90b
    ."\",\"indel_80b\":\"".$indel_80b
    ."\",\"indel_70b\":\"".$indel_70b
    ."\",\"indel_60b\":\"".$indel_60b
    ."\",\"indel_50b\":\"".$indel_50b
    ."\",\"indel_40b\":\"".$indel_40b
    ."\",\"indel_30b\":\"".$indel_30b
    ."\",\"indel_20b\":\"".$indel_20b
    ."\",\"indel_10b\":\"".$indel_10b
    ."\",\"indel_9b\":\"".$indel_9b
    ."\",\"indel_8b\":\"".$indel_8b
    ."\",\"indel_7b\":\"".$indel_7b
    ."\",\"indel_6b\":\"".$indel_6b
    ."\",\"indel_5b\":\"".$indel_5b
    ."\",\"indel_4b\":\"".$indel_4b
    ."\",\"indel_3b\":\"".$indel_3b
    ."\",\"indel_2b\":\"".$indel_2b
    ."\",\"indel_1b\":\"".$indel_1b
    ."\"";
    if(isset($external_sites)) {
        $str .= ",\"external_sites\":". json_encode($external_sites);
    }
    if(isset($autoDetectSerialTable)) {
        $str .= ",\"autoDetectSerialTable\":". $autoDetectSerialTable;
    }
    if(isset($distanceMatrixPath) && file_exists($distanceMatrixPath)) {
        $str .= ",\"useSystemTree\":\"1\"";
    } else {
	$str .= ",\"useSystemTree\":\"0\"";
    }
/*
    if(isset($newickPath) && file_exists($newickPath)) {
	$str .= ",\"useSystemTreeLW\":\"1\"";
    } else {
	$str .= ",\"useSystemTreeLW\":\"0\"";
    }
*/
    if(isset($systreeDefaultOn)) {
        $str .= ",\"systreeDefaultOn\":\"". $systreeDefaultOn. "\"";
    } else {
	$str .= ",\"systreeDefaultOn\":\"1\"";
    }
    if(isset($systreeAutoCollapseThreshold)) {
	$str .= ",\"systreeAutoCollapseThreshold\":\"". $systreeAutoCollapseThreshold. "\"";
    }
    if(isset($systreeAutoCollapseType)) {
        $str .= ",\"systreeAutoCollapseType\":\"". $systreeAutoCollapseType. "\"";
    }
    if(isset($systreeCorrection)) {
        $str .= ",\"systreeCorrection\":\"". $systreeCorrection. "\"";
    }
    if(isset($systreeSort)) {
        $str .= ",\"systreeSort\":\"". $systreeSort. "\"";
    }
    if(isset($systreeSortType)) {
	$str .= ",\"systreeSortType\":\"". $systreeSortType. "\"";
    }
    if(isset($systreeWidth)) {
        $str .= ",\"systreeWidth\":\"". $systreeWidth. "\"";
    }
    if(isset($useTblastx)) {
        $str .= ",\"useTblastx\":\"". $useTblastx. "\"";
    }
    if(isset($systreeProgressChar)) {
	$str .= ",\"systreeProgressChar\":\"". $systreeProgressChar. "\"";
    } else {
	$str .= ",\"systreeProgressChar\":\"". DEF_SYSTREE_PROGRESS_CHAR. "\"";
    }
    if(isset($systreeProgressCharLen)) {
	$str .= ",\"systreeProgressCharLen\":\"". $systreeProgressCharLen. "\"";
    } else {
	$str .= ",\"systreeProgressCharLen\":\"". DEF_SYSTREE_PROGRESS_CHAR_LEN. "\"";
    }

    // for placeholderStr for search form
    $str .= ",\"placeholder\":{\"topsearch\":\"";
    if(isset($placeholder_topsearch)) $str .= $placeholder_topsearch;
    $str .= "\",\"possearch\":\"";
    if(isset($placeholder_possearch)) $str .= $placeholder_possearch;
    $str .= "\",\"kwsearch\":\"";
    if(isset($placeholder_kwsearch)) $str .= $placeholder_kwsearch;
    $str .= "\"}";

    if(isset($default_position)) $str .= ",\"_default_position\":". json_encode($default_position);
    if(isset($color_acc_group_preset)) $str .= ",\"color_acc_group_preset\":". json_encode($color_acc_group_preset);
    if(isset($color_acc_group_color)) $str .= ",\"color_acc_group_color\":". json_encode($color_acc_group_color);

    $str .= ",\"dbmode\":\"". $dbmode. "\"";

    $newick = "";
    if(isset($newickPath) && file_exists($newickPath)) {
	$str .= ",\"useSystemTreeLW\":\"1\"";

	$newick = trim(file_get_contents($newickPath));
	$newick = preg_replace("/,\s/", ",", $newick);
//	$newick = preg_replace("/\s/g", "", $newick);	// This is fine if you don't allow spaces in AccessionID.
    } else {
	$str .= ",\"useSystemTreeLW\":\"0\"";
    }
    $str .= ",\"systreeNewickLW\":\"". $newick. "\"";

    if(isset($accmanager_list_type)) {
	$str .= ",\"accmanager_list_type\":\"". $accmanager_list_type. "\"";
    } else {
	$str .= ",\"accmanager_list_type\":\"standard\"";
    }

    $phy_tools = array('dnadist' => False, 'neighbor' => False, 'makeBinaryDistanceMatrix' => False);
    if(isset($phylipDir) && is_dir($phylipDir)) {
	if(is_file($phylipDir. "/exe/dnadist")) $phy_tools['dnadist'] = True;
	if(is_file($phylipDir. "/exe/neighbor")) $phy_tools['neighbor'] = True;
    }
    if(is_file($makeBinaryDistanceMatrixPath)) $phy_tools['makeBinaryDistanceMatrix'] = True;
    $str .= ",\"phyTools\":". json_encode($phy_tools);

    $str .= ",\"enableExportVariantDensFile\":";
    if(isset($enableExportVariantDensFile)) {
	$str .= $enableExportVariantDensFile;
    } else {
	$str .= "1";
    }

    $str .= ",\"pcaGraphW\":";
    if(isset($pcaGraphW)) {
        $str .= $pcaGraphW;
    } else {
        $str .= "420";
    }
    $str .= ",\"pcaGraphH\":";
    if(isset($pcaGraphH)) {
        $str .= $pcaGraphH;
    } else {
        $str .= "460";
    }
    $str .= ",\"enableExportPcaFiles\":";
    if(isset($enableExportPcaFiles)) {
        $str .= $enableExportPcaFiles;
    } else {
        $str .= "1";
    }

    $str .= ",\"enableExportAdmixtureFiles\":";
    if(isset($enableExportAdmixtureFiles)) {
        $str .= $enableExportAdmixtureFiles;
    } else {
        $str .= "1";
    }

    $str .= ",\"validMacroViewerMenu\":";
    if(	(isset($variantDensPath) && file_exists($variantDensPath)) ||
	(isset($admixtureIdListPath) && file_exists($admixtureIdListPath) && 
		isset($admixtureK2Path) && count($admixtureK2Path)>0) ||
        (isset($pcaVecPath) && file_exists($pcaVecPath))
    ) {
	$str .= "1";
    } else {
	$str .= "0";

    }

    $str .= "}";

    echo $str;

?>

