#!/bin/sh

export THIS_DIR="$(cd "$( dirname "$0" )" && pwd )"
. $THIS_DIR/../../sysbase/tools/export_php_config_to_shell.sh

#create database
API_DB_NAME=data_center
echo "create database IF NOT EXISTS $API_DB_NAME" | mysql -f -h $API_DB_HOST -u $API_DB_USER --password=$API_DB_PASSWD 2>&1 | grep -v 'password on the command line'

#create table
mysql -h $API_DB_HOST -u $API_DB_USER --password=$API_DB_PASSWD $API_DB_NAME < "$THIS_DIR/mysql/create_tables.sql" 2>&1 | grep -v 'password on the command line'
php $THIS_DIR/mysql/alter_tables.php

#建立log資料夾
if [ ! -d /var/log/api/org_tree_surrogate/ ];then
    mkdir -p /var/log/api/org_tree_surrogate/
fi
chown apache:apache /var/log/api/org_tree_surrogate/

# To recursively give directories read&execute privileges:
find /var/log/api/org_tree_surrogate -type d -exec chmod 755 {} +


org2ApiPid=`ps -afeww |grep -v grep | grep portal_user_dept_query_convert | awk {'print $2'}`
if [ -n "$org2ApiPid" ];then
    ps -afeww |grep -v grep | grep portal_user_dept_query_convert | awk {'print $2'} | xargs kill
fi