#!/bin/sh
#File: /home/www/shell/oa.sh
#Desc: database auto create
# basic vars
#DB_HOST="192.168.16.200"
DB_PORT="3306"
DB_USER="root"
DB_PASS="123456"
DB_NAME="test_item"
BIN_DIR="/usr/local/mysql/bin"
BCK_DIR="/home/wwwroot/test.backstage.com/protected/data"
DATE=`date +%F`
# TODO
#now_time_stamp=`date +%s`
#yesday_time_stamp=$(($now_time_stamp-86400))
apply_check_sql="select concat(uid,':',company,':',name) as dblist from db_enterprise where is_check='2' and is_create_db='0'"
apply_list=$(${BIN_DIR}/mysql -uroot -p123456 -D ${DB_NAME} -e "${apply_check_sql}")
# db list
i=0
for db in ${apply_list}; do
if(($i>0));then
uid=`echo ${db} | cut -d':' -f1`
com=`echo ${db} | cut -d':' -f2`
usn=`echo ${db} | cut -d':' -f3`
sign=$($BIN_DIR/mysql -u${DB_USER} -p${DB_PASS} -e "select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='dev_${com}'")
if test -z "${sign}";then
create_db_sql="create database IF NOT EXISTS dev_${com}"
insert_db_sql="INSERT INTO db_member(id,uid,username,division_id,post,sex,pic,mobile,qq,email,is_del,update_time,create_time) VALUES(1,'${uid}','${usn}',1,'',3,'','','','',1,UNIX_TIMESTAMP(NOW()),UNIX_TIMESTAMP(NOW()))"
$BIN_DIR/mysql -u${DB_USER} -p${DB_PASS} -e "${create_db_sql}"
$BIN_DIR/mysql -u${DB_USER} -p${DB_PASS} -D dev_${com} < $BCK_DIR/oa_init.sql
$BIN_DIR/mysql -u${DB_USER} -p${DB_PASS} -D dev_${com} -e "${insert_db_sql}"
$BIN_DIR/mysql -u${DB_USER} -p${DB_PASS} -D ${DB_NAME} -e "update db_enterprise set is_create_db='1' where uid='${uid}'"
echo "the db dev_${com} create success."
else
echo "the db dev_${com} has exist.";
fi
fi
i=1
done