function _log() {
    echo "[Log] $@"
}

root_path=`pwd`
file_name=${1}
branch_name=`git branch | awk  '$1 == "*"{print}' | awk -F/ '{print $2}' | awk -F')' '{print $1}'`

version_sql_path=$root_path/global/sql
if [ ! -d $version_sql_path ];
then
    mkdir -p $version_sql_path
fi

cat >> test.lua << EOF
local dbconf = require("$file_name")
print("database", dbconf.mysql_gamedb.database)
print("confdatabase", dbconf.mysql_confdb.database)
print("host", dbconf.mysql_gamedb.host)
print("port", dbconf.mysql_gamedb.port)
print("user", dbconf.mysql_gamedb.user)
print("password", dbconf.mysql_gamedb.password)
EOF

ret=(`lua test.lua | awk -F" " '{print $2}'`)
db_gamedata=${ret[0]}
db_gameconf=${ret[1]}
host=${ret[2]}
port=${ret[3]}
user=${ret[4]}
password=${ret[5]}

MYSQL="mysql -u$user -p$password -h$host -P$port"

notchange="nothing to commit, working tree clean"
rm -rf test.lua

cd ${version_sql_path}
files=$(ls)
sql_name=$version_sql_path/${branch_name}.sql

if [ ! -f "$sql_name" ]; then
    echo "USE \`${db_gamedata}\`;" > ${sql_name}
fi

#排序后遍历文件
for file in ${files[@]}
do
    _log "执行 sql文件:" $file
    ${MYSQL} --force $db_gamedata < $file 2>/dev/null
done

rm -rf out.txt 
