#!/bin/bash -c

tidb_slave_host="192.168.1.12"
tidb_slave_user="root"
tidb_slave_password=""

tidb_password_str=""
if [[ -n "${tidb_slave_password}" ]];
then
    tidb_password_str="-p${tidb_slave_password}"
fi

tables=("payments" "refunds" "sub_payments" "transfers" "compensatable_transfers")

end_position=$(date "+%Y-%m-%d %H:%M:%S" --date "300 seconds ago")

echo "Starting incremental sync diff..."
for table in "${tables[@]}"; do
     last_checkpoint=$( /usr/bin/mysql -h"${tidb_slave_host}" -P4000 -u"${tidb_slave_user}" ${tidb_password_str} -Ne \
     "SELECT MAX(checkpoint) FROM incremental_sync_diff.summary WHERE table_name=\"${table}\";" )
     if [[ -z ${last_checkpoint} || ${last_checkpoint} == "NULL" ]];
     then
        start_position=$(date "+%Y-%m-%d %H:%M:%S" --date "${end_position} 7 days ago")
        last_checkpoint=${start_position}
     else
        start_position=$(date "+%Y-%m-%d %H:%M:%S" --date "${last_checkpoint} 30 seconds ago")
     fi
     echo "SELECT COUNT(updated_at) FROM paylite_payment.${table} WHERE updated_at >= \"${start_position}\" AND updated_at < \"${end_position}\";"
     new_row_count=$(/usr/bin/mysql -h"${tidb_slave_host}" -P4000 -u"${tidb_slave_user}" ${tidb_password_str} -Ne \
        "SELECT COUNT(updated_at) FROM paylite_payment.${table} WHERE updated_at >= \"${start_position}\" AND updated_at < \"${end_position}\";")
     echo ${new_row_count}
     if [[ "${new_row_count}" > 0 ]];
     then
        sed "s/{{ starting_checkpoint }}/${start_position}/g; s/{{ ending_checkpoint }}/${end_position}/g;" /home/ec2-user/deploy/scripts/config_${table}.toml.j2 > /home/ec2-user/deploy/scripts/config_${table}.toml

        /home/ec2-user/deploy/bin/sync_diff_inspector --config=/home/ec2-user/deploy/scripts/config_${table}.toml 2>&1 >> sync_diff.log
        state=$(/usr/bin/mysql -h"${tidb_slave_host}" -P4000 -u"${tidb_slave_user}" ${tidb_password_str} -Ne \
            "SELECT state FROM sync_diff_inspector.summary WHERE \`schema\`=\"paylite_payment\" and \`table\`=\"${table}\"")
        result_details=$(/usr/bin/mysql -h"${tidb_slave_host}" -P4000 -u"${tidb_slave_user}" ${tidb_password_str} -Ne \
            "SELECT * FROM sync_diff_inspector.summary WHERE \`schema\`=\"paylite_payment\" and \`table\`=\"${table}\"")
        echo ${state}
        echo ${result_detail}

        if [[ ${state} == "success" ]];
        then
            next_checkpoint=${end_position}
        else
            next_checkpoint=${last_checkpoint}
        fi
        /usr/bin/mysql -h"${tidb_slave_host}" -P4000 -u"${tidb_slave_user}" ${tidb_password_str} -e \
           "INSERT INTO incremental_sync_diff.summary (checkpoint, table_name, state, result_details) \
           VALUES (\"${next_checkpoint}\", \"${table}\", \"${state}\", \"${result_details}\");"
     else
        next_checkpoint=${end_position}
        /usr/bin/mysql -h"${tidb_slave_host}" -P4000 -u"${tidb_slave_user}" ${tidb_password_str} -e \
           "INSERT INTO incremental_sync_diff.summary (checkpoint, table_name, state, result_details) \
           VALUES (\"${next_checkpoint}\", \"${table}\", \"No Data\", \"\");"
     fi
done

echo "Incremental sync diff completed."