#!/usr/bin/php
<?php
# @package      hubzero-metrics
# @file         xsetup_hubmetrics
# @author       Swaroop Shivarajapura <swaroop@purdue.edu>
# @copyright    Copyright (c) 2011-2013 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2011-2013 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#
# =========================================================================
# This script creates HUB metrics database and accounts
#
# USAGE: ./xsetup_databases
#

error_reporting(E_ALL & ~E_NOTICE);
@ini_set('display_errors','1');

if(!defined('__DIR__')) {
	$fPos = strrpos(__FILE__, "/");
	define("__DIR__", substr(__FILE__, 0, $fPos) . "/");
}

require_once(__DIR__.'/../includes/hub_parameters.php');
require_once(__DIR__.'/../includes/db_connect.php');
require_once(__DIR__.'/../includes/func_misc.php');

$debug = 1;

$google_api = 'http://code.google.com/apis/maps/signup.html';

$db_hub = db_connect('db_hub');

$hub_db = str_replace('`','',$hub_db);
$metrics_db = str_replace('`','',$metrics_db);

# Creating metrics database tables
$mysql_cmd = "mysql -u'".$db_user."' -p'".$db_pass."' -D ".$metrics_db." < hub_files/db_create_metrics_tables.sql";
shell_exec($mysql_cmd);

# Creating the missing hub database tables
$mysql_cmd = "mysql -u'".$db_user."' -p'".$db_pass."' -D ".$hub_db." < hub_files/db_missing_hub_tables.sql";
shell_exec($mysql_cmd);

$sql = 'UPDATE '.$hub_db.'.'.$db_prefix.'components SET params = "statsDBDriver=mysql'.n;
$sql .= 'statsDBHost='.$db_host.n;
$sql .= 'statsDBPort='.n;
$sql .= 'statsDBUsername='.$db_user.n;
$sql .= 'statsDBPassword='.$db_pass.n;
$sql .= 'statsDBDatabase='.$metrics_db.n;
$sql .= 'statsDBPrefix='.n;
$sql .= 'mapsApiKey='.$google_api.n;
$sql .= 'stats_path=/site/stats'.n;
$sql .= 'maps_path=/site/stats/maps'.n;
$sql .= 'plots_path=/site/stats/plots'.n;
$sql .= 'charts_path=/site/stats/plots'.n;
$sql .= n;
$sql .= '" WHERE name = "Usage" AND link = "option=com_usage" AND `option` = "com_usage"';
mysql_exec($db_hub, $sql);

$timestamp = date('Y').'-'.date('m').'-'.date('d').'_'.date('H.i.s');

# -------------------------------------------------------------------------------
# Creating the necessary directories and copying files
# -------------------------------------------------------------------------------
$cmd = 'mkdir -p /var/log/apache2/daily'.n;
$cmd .= 'chmod -R 770 /var/log/apache2/daily'.n;
$cmd .= 'chown -R www-data /var/log/apache2/daily'.n;
$cmd .= 'chgrp -R www-data /var/log/apache2/daily'.n;
$cmd .= n;

$cmd = 'mkdir -p /var/log/apache2/imported'.n;
$cmd .= 'chmod -R 770 /var/log/apache2/imported'.n;
$cmd .= 'chown -R www-data /var/log/apache2/imported'.n;
$cmd .= 'chgrp -R www-data /var/log/apache2/imported'.n;
$cmd .= n;

$cmd = 'mkdir -p /var/log/hubzero/daily'.n;
$cmd .= 'chmod -R 770 /var/log/hubzero/daily'.n;
$cmd .= 'chown -R www-data /var/log/hubzero/daily'.n;
$cmd .= 'chgrp -R www-data /var/log/hubzero/daily'.n;
$cmd .= n;

$cmd = 'mkdir -p /var/log/hubzero/imported'.n;
$cmd .= 'chmod -R 770 /var/log/hubzero/imported'.n;
$cmd .= 'chown -R www-data /var/log/hubzero/imported'.n;
$cmd .= 'chgrp -R www-data /var/log/hubzero/imported'.n;
$cmd .= n;

$cmd .= 'mkdir -p '.$hub_dir.'/site/stats/chart_resources'.n;
$cmd .= 'touch '.$hub_dir.'/site/stats/chart_resources/hub.dat'.n;
$cmd .= 'chmod -R 770 '.$hub_dir.'/site/stats/chart_resources'.n;
$cmd .= 'chown -R www-data '.$hub_dir.'/site/stats/chart_resources'.n;
$cmd .= 'chgrp -R www-data '.$hub_dir.'/site/stats/chart_resources'.n;
$cmd .= n;

$cmd .= 'mkdir -p '.$hub_dir.'/site/stats/maps'.n;
$cmd .= 'touch '.$hub_dir.'/site/stats/maps/whoisonline.xml'.n;
$cmd .= 'chmod -R 770 '.$hub_dir.'/site/stats/maps'.n;
$cmd .= 'chown -R www-data '.$hub_dir.'/site/stats/maps'.n;
$cmd .= 'chgrp -R www-data '.$hub_dir.'/site/stats/maps'.n;
$cmd .= n;

#$cmd .= 'cp -p '.$hub_dir.'/plugins/usage/overview.php '.$hub_dir.'/plugins/usage/overview.php.'.$timestamp.n;
#$cmd .= 'cp hub_files/plugins/usage/overview.php '.$hub_dir.'/plugins/usage/'.n;
#$cmd .= 'chown www-data '.$hub_dir.'/plugins/usage/overview.php'.n;
#$cmd .= 'chgrp www-data '.$hub_dir.'/plugins/usage/overview.php'.n;
#$cmd .= 'chmod 660 '.$hub_dir.'/plugins/usage/overview.php'.n;
#$cmd .= n;

#$cmd .= 'cp -p '.$hub_dir.'/plugins/members/usage.php '.$hub_dir.'/plugins/members/usage.php.'.$timestamp.n;
#$cmd .= 'cp hub_files/plugins/members/usage.php '.$hub_dir.'/plugins/members/'.n;
#$cmd .= 'chown www-data '.$hub_dir.'/plugins/members/usage.php'.n;
#$cmd .= 'chgrp www-data '.$hub_dir.'/plugins/members/usage.php'.n;
#$cmd .= 'chmod 660 '.$hub_dir.'/plugins/members/usage.php'.n;
#$cmd .= n;

#$cmd .= 'cp -p '.$hub_dir.'/plugins/resources/usage/views/browse/tmpl/default.php '.$hub_dir.'/plugins/resources/usage/views/browse/tmpl/default.php.'.$timestamp.n;
#$cmd .= 'cp hub_files/plugins/resources/usage/views/browse/tmpl/default.php '.$hub_dir.'/plugins/resources/usage/views/browse/tmpl/'.n;
#$cmd .= 'chown www-data '.$hub_dir.'/plugins/resources/usage/views/browse/tmpl/default.php'.n;
#$cmd .= 'chgrp www-data '.$hub_dir.'/plugins/resources/usage/views/browse/tmpl/default.php'.n;
#$cmd .= 'chmod 660 '.$hub_dir.'/plugins/resources/usage/views/browse/tmpl/default.php'.n;
#$cmd .= n;

#$cmd .= 'cp -p '.$hub_dir.'/language/en-GB/en-GB.plg_resources_usage.ini '.$hub_dir.'/language/en-GB/en-GB.plg_resources_usage.ini.'.$timestamp.n;
#$cmd .= 'cp hub_files/language/en-GB/en-GB.plg_resources_usage.ini '.$hub_dir.'/language/en-GB/en-GB.plg_resources_usage.ini'.n;
#$cmd .= 'chown www-data '.$hub_dir.'/language/en-GB/en-GB.plg_resources_usage.ini'.n;
#$cmd .= 'chgrp www-data '.$hub_dir.'/language/en-GB/en-GB.plg_resources_usage.ini'.n;
#$cmd .= 'chmod 660 '.$hub_dir.'/language/en-GB/en-GB.plg_resources_usage.ini'.n;
#$cmd .= n;

$cmd .= 'cp hub_files/crontab_metrics /etc/cron.d/'.n;
$cmd .= 'chown root /etc/cron.d/crontab_metrics'.n;
$cmd .= 'chgrp root /etc/cron.d/crontab_metrics'.n;
$cmd .= 'chmod 700 /etc/cron.d/crontab_metrics'.n;
$cmd .= n;

$cmd .= 'chown -R www-data ../../metrics/'.n;
$cmd .= 'chgrp -R www-data ../../metrics/'.n;
$cmd .= 'chmod -R 750 ../../metrics/'.n;

shell_exec($cmd);

if ($debug) {
	print '# -------------------------------'.n;
	print '# Executed the following commands'.n;
	print '# -------------------------------'.n.n;
	print $cmd.n;
}

# -------------------------------------------------------------------------------
# Logrotate and installation directory
# -------------------------------------------------------------------------------
$note = '# -------------------------------'.n;
$note .= '# Setup logrotate for apache and cms auth log so they are rotated daily at 11:59PM'.n;
$note .= '# -------------------------------'.n.n;
$note .= 'apache log: /var/log/apache2/'.$hub_dir.'-access.log to /var/log/apache2/daily/'.$hub_dir.'-access.log-YYYYMMDD'.n.n;
$note .= 'cmsauth.log: /var/log/hubzero/cmsauth.log to /var/log/hubzero/daily/cmsauth.log-YYYYMMDD'.n.n;
$note .= '# -------------------------------'.n;
$note .= 'Note: Metrics scripts will delete the following directory: '.$hub_dir.'/installation/'.n.n;
print n.$note;

db_close($db_hub);

?>
