#!/bin/bash

mysqlcon=( mysql -hlocalhost -u${MYSQL_REPL_USER} -p${MYSQL_REPL_PASSWORD} )
mysql0con=( mysql -h'mysql0' -u${MYSQL_REPL_USER} -p${MYSQL_REPL_PASSWORD} )
mysql1con=( mysql -h'mysql1' -u${MYSQL_REPL_USER} -p${MYSQL_REPL_PASSWORD} )
mysql2con=( mysql -h'mysql2' -u${MYSQL_REPL_USER} -p${MYSQL_REPL_PASSWORD} )

while true;do
    if echo 'SELECT 1' | "${mysql2con[@]}" &> /dev/null; then
                        break
                  fi
                   echo "wait for mysql start"
                        sleep 3
done


main(){
    if [ -f /var/lib/mysql/last_time ];then
        start_group_replication  
        for ((i=0;i<=3;i++))
        do
            if [ $(check_self_status) = "ONLINE" ];then
                break
            else
                start_group_replication
            fi
        done
    else
         first_start_group_replication
    fi 
    "${mysqlcon[@]}" -e 'SET GLOBAL read_only=OFF;'
   exec /opt/mysql_config/generate_last_time.sh &> /dev/null &
    check_group_replication
}



boot_sql=
read -r -d '' boot_sql <<-EOSQL
         SET GLOBAL group_replication_bootstrap_group=ON;
         START GROUP_REPLICATION;
         SET GLOBAL group_replication_bootstrap_group=OFF;
EOSQL

first_start_group_replication(){
    while true
    do
        if "${mysql0con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null;then
          echo "CHANGE MASTER TO MASTER_USER='${MYSQL_REPL_USER}', MASTER_PASSWORD='${MYSQL_REPL_PASSWORD}' FOR CHANNEL 'group_replication_recovery';" | ${mysql2con[@]}
          echo "START GROUP_REPLICATION" | ${mysql2con[@]}
          break
        else
                   echo "wait for mysql0 start group replication "
                        sleep 5
        fi
    done
}


#first_start_group_replication(){
#    if "${mysql0con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null || "${mysql1con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null;then
#        echo "CHANGE MASTER TO MASTER_USER='${MYSQL_REPL_USER}', MASTER_PASSWORD='${MYSQL_REPL_PASSWORD}' FOR CHANNEL 'group_replication_recovery';" | ${mysql2con[@]}
#        echo "START GROUP_REPLICATION" | ${mysql2con[@]}

#    else
#       "${mysql2con[@]}" <<-EOSQL
#       ${boot_sql}
#EOSQL
#    fi
#}

get_last_time(){
     while true;do
         LAST_TIME=$(ssh -o StrictHostKeyChecking=no root@${1} 'cat /var/lib/mysql/last_time')
         if [ "$?" = "0" ];then
               sed -i /${1}/d /var/lib/mysql/last_time_list
               echo "${1} ${LAST_TIME}"  >> /var/lib/mysql/last_time_list 
               break
         else
               sleep 5
               continue
         fi
     done
}

start_group_replication(){
    if "${mysql0con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null || "${mysql1con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null;then
        echo "CHANGE MASTER TO MASTER_USER='${MYSQL_REPL_USER}', MASTER_PASSWORD='${MYSQL_REPL_PASSWORD}' FOR CHANNEL 'group_replication_recovery';" | ${mysql2con[@]}
        echo "START GROUP_REPLICATION" | ${mysql2con[@]}
    else
        for HOST in mysql0 mysql1 mysql2
        do
           get_last_time ${HOST}
        done
        LAST_HOST=$(sort -r -t ' ' -k 2 /var/lib/mysql/last_time_list|head -n 1|cut -f1 -d ' ') 
        
        if [ ${LAST_HOST} = $(hostname) ];then 
            "${mysql2con[@]}" <<-EOSQL
            ${boot_sql}
EOSQL
        else
            while true;do
                if "${mysql0con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null || "${mysql1con[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" &> /dev/null;then
        echo "CHANGE MASTER TO MASTER_USER='${MYSQL_REPL_USER}', MASTER_PASSWORD='${MYSQL_REPL_PASSWORD}' FOR CHANNEL 'group_replication_recovery';" | ${mysql2con[@]}
        echo "START GROUP_REPLICATION" | ${mysql2con[@]}
                    break
                else
                   sleep 5
                   continue
               fi
            done
        fi
    fi
}

check_group_replication(){
    while true;do
           sleep 60
        ONLINE_NUM=$("${mysqlcon[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' |grep "ONLINE" 2> /dev/null | wc -l)
        if [ ${ONLINE_NUM} -lt 2 ];then
            "${mysqlcon[@]}" -e 'SET GLOBAL super_read_only=ON;' 
        else
           if [ $(${mysqlcon[@]} -e "show variables like 'super_read_only'\G" 2> /dev/null|grep Value|awk '{print $2}') = "ON" ];then
              "${mysqlcon[@]}" -e 'SET GLOBAL super_read_only=OFF;SET GLOBAL read_only=OFF;'
           fi
        fi
    done
}

check_self_status(){
    echo $("${mysqlcon[@]}" -e 'SELECT * FROM performance_schema.replication_group_members;' 2> /dev/null |grep $(hostname)|awk '{print $5}')
}

main
