#!/bin/sh
# @function install MySQL
function InstallMySQL()
{
    local cnf=$1

    ShowResult "[green]正在安装MySQL服务器... [/green]" 1
    
    # check if not exists ($? = 1)
    id mysql > /dev/null 2>&1
    if [ $? -eq 1 ]
    then
        # create mysql user & group
        eval /usr/sbin/groupadd mysql ${_SYS_SHOW_INSTALL_DETAILS_}
        eval /usr/sbin/useradd -g mysql mysql ${_SYS_SHOW_INSTALL_DETAILS_}
    fi
    
    # prepare
    eval cd ${_PATH_COMPILATION_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval rm -rf ./mysql-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval tar zxvf ${_PATH_SOFTWARE_}/mysql-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cd mysql-* ${_SYS_SHOW_INSTALL_DETAILS_}
    # install
    eval /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=${_PATH_MYSQL_} \
        -DMYSQL_DATADIR=${_PATH_DB_DATA_} \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_MEMORY_STORAGE_ENGINE=1 \
        -DWITH_MYISAM_STORAGE_ENGINE=1 \
        -DMYSQL_TCP_PORT=${_DB_PORT_} \
        -DMYSQL_UNIX_ADDR=${_PATH_DB_DATA_}/mysql.sock \
        -DMYSQL_USER=mysql \
        -DWITH_DEBUG=0 \
        -DSYSCONFDIR=/etc/ \
        -DWITH_SSL=yes \
        -DDEFAULT_CHARSET=utf8 \
        -DDEFAULT_COLLATION=utf8_general_ci \
        -DWITH_READLINE=on \
        -DCMAKE_EXE_LINKER_FLAGS="-ltcmalloc_minimal" \
        -DWITH_SAFEMALLOC=OFF \
        ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # link liberaries
    eval ln -s ${_PATH_MYSQL_}/lib/lib* /usr/local/lib/ ${_SYS_SHOW_INSTALL_DETAILS_}
    # link include
    eval ln -s ${_PATH_MYSQL_}/include/mysql* /usr/local/include ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # chmod & set configuration file
    eval chmod +w ${_PATH_MYSQL_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chown -R mysql:mysql ${_PATH_MYSQL_} ${_SYS_SHOW_INSTALL_DETAILS_}
    
    if [ ${cnf} == 'min' ];then
        eval \cp -f ${_PATH_CONFIGS_}/my-min.cnf /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
        eval chown mysql:mysql /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
    else
        eval \cp -f ${_PATH_CONFIGS_}/my.cnf /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
        eval chown mysql:mysql /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
    fi
    
    # install db
    eval ${_PATH_MYSQL_}/scripts/mysql_install_db --user=mysql --basedir=${_PATH_MYSQL_} \
        --datadir=${_PATH_DB_DATA_} ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # remove my.cnf in mysql install path (major for MySQL 5.6 create a my.cnf file after 
    # mysql_install_db execution that will overwrite sql_mode ever after)
    eval rm -rf ${_PATH_MYSQL_}/my.cnf
    
    ShowStatus $? 1
    
    # set as a service
    eval \cp -f ${_PATH_MYSQL_}/support-files/mysql.server /etc/init.d/mysqld ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chmod 755 /etc/init.d/mysqld ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chkconfig --add mysqld ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chkconfig mysqld on ${_SYS_SHOW_INSTALL_DETAILS_}
    eval /etc/init.d/mysqld start ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # set mysql root password
    eval ${_PATH_MYSQL_}/bin/mysqladmin -u root password "${_DB_ROOT_PWD_}" ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $?
}

function InstallMySQL57()
{
    local cnf=$1

    ShowResult "[green]正在安装MySQL服务器... [/green]" 1
    
    # check if not exists ($? = 1)
    id mysql > /dev/null 2>&1
    if [ $? -eq 1 ]
    then
        # create mysql user & group
        eval /usr/sbin/groupadd mysql ${_SYS_SHOW_INSTALL_DETAILS_}
        eval /usr/sbin/useradd -g mysql mysql ${_SYS_SHOW_INSTALL_DETAILS_}
    fi
    
    # prepare
    eval cd ${_PATH_COMPILATION_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval rm -rf ./mysql-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval tar zxvf ${_PATH_SOFTWARE_}/mysql-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cd mysql-* ${_SYS_SHOW_INSTALL_DETAILS_}
    # install
    eval /usr/bin/cmake -DCMAKE_INSTALL_PREFIX=${_PATH_MYSQL_} \
        -DMYSQL_DATADIR=${_PATH_DB_DATA_} \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_MEMORY_STORAGE_ENGINE=1 \
        -DWITH_MYISAM_STORAGE_ENGINE=1 \
        -DMYSQL_TCP_PORT=${_DB_PORT_} \
        -DMYSQL_UNIX_ADDR=${_PATH_DB_DATA_}/mysql.sock \
        -DMYSQL_USER=mysql \
        -DWITH_DEBUG=0 \
        -DSYSCONFDIR=/etc/ \
        -DWITH_SSL=yes \
        -DDEFAULT_CHARSET=utf8 \
        -DDEFAULT_COLLATION=utf8_general_ci \
        -DWITH_READLINE=on \
        -DCMAKE_EXE_LINKER_FLAGS="-ltcmalloc_minimal" \
        -DWITH_SAFEMALLOC=OFF \
        -DWITH_BOOST=boost \
        ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # link liberaries
    eval ln -s ${_PATH_MYSQL_}/lib/lib* /usr/local/lib/ ${_SYS_SHOW_INSTALL_DETAILS_}
    # link include
    eval ln -s ${_PATH_MYSQL_}/include/mysql* /usr/local/include ${_SYS_SHOW_INSTALL_DETAILS_}
    
    # chmod & set configuration file
    eval chmod +w ${_PATH_MYSQL_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chown -R mysql:mysql ${_PATH_MYSQL_} ${_SYS_SHOW_INSTALL_DETAILS_}
    
    if [ ${cnf} == 'min' ];then
        eval \cp -f ${_PATH_CONFIGS_}/my-min.cnf /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
        eval chown mysql:mysql /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
    else
        eval \cp -f ${_PATH_CONFIGS_}/my.cnf /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
        eval chown mysql:mysql /etc/my.cnf ${_SYS_SHOW_INSTALL_DETAILS_}
    fi
    
    # install db
    eval ${_PATH_MYSQL_}/bin/mysqld --initialize-insecure --user=mysql --basedir=${_PATH_MYSQL_} \
        --datadir=${_PATH_DB_DATA_} ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $? 1
    
    # remove my.cnf in mysql install path (major for MySQL 5.6 create a my.cnf file after 
    # mysql_install_db execution that will overwrite sql_mode ever after)
    eval rm -rf ${_PATH_MYSQL_}/my.cnf
    
    ShowStatus $? 1
    
    # set as a service
    eval \cp -f ${_PATH_MYSQL_}/support-files/mysql.server /etc/init.d/mysqld ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chmod 755 /etc/init.d/mysqld ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chkconfig --add mysqld ${_SYS_SHOW_INSTALL_DETAILS_}
    eval chkconfig mysqld on ${_SYS_SHOW_INSTALL_DETAILS_}
    eval /etc/init.d/mysqld start ${_SYS_SHOW_INSTALL_DETAILS_}

    # set mysql root password
    eval ${_PATH_MYSQL_}/bin/mysqladmin -u root password "${_DB_ROOT_PWD_}" ${_SYS_SHOW_INSTALL_DETAILS_}
    
    ShowStatus $?
}

# @function install MyDumper
function InstallMyDumper()
{
    if [ -z "$(command -v ${_PATH_MYSQL_}/bin/mysql)" ]; then
        return
    fi

    ShowResult "[green]正在安装MyDumper... [/green]" 1

    declare -a needle_=()
    local iota=0

    if [ $(yum list installed | grep -c 'zlib-devel') -eq 0 ]; then
        needle_[iota++]=zlib-devel
    fi
    if [ $(yum list installed | grep -c 'glib2-devel') -eq 0 ]; then
        needle_[iota++]=glib2-devel
    fi
    if [ $(yum list installed | grep -c 'pcre-devel') -eq 0 ]; then
        needle_[iota++]=pcre-devel
    fi
    if [ ! -z "$(echo "${needle_[*]}")" ]; then
        eval yum install -y $(echo "${needle_[*]}") ${_SYS_SHOW_INSTALL_DETAILS_}
    fi

    # install
    eval cd ${_PATH_COMPILATION_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval rm -rf ./mydumper-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval tar zxvf ${_PATH_SOFTWARE_}/mydumper-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cd mydumper-* ${_SYS_SHOW_INSTALL_DETAILS_}
    eval cmake . ${_SYS_SHOW_INSTALL_DETAILS_}
    ShowStatus $? 1
    eval make ${_SYS_MAKE_EXT_} ${_SYS_SHOW_INSTALL_DETAILS_}
    eval make install ${_SYS_SHOW_INSTALL_DETAILS_}
    ShowStatus $? 1

    ln -s /usr/local/bin/mydumper /usr/bin/ && ln -s /usr/local/bin/myloader /usr/bin/
    ShowStatus $?
}