#!/bin/bash
# should be ran as ./me.sh db_username db_dbname

if [ $# -lt 2 ]; then
   echo "Run this command as: "
   echo "$0 <db_username> <db_dbname>"
   exit
fi

DBUNAME=$1
DBNAME=$2


read -s -p "Enter a database password here: " DBPASS

echo "Trying to ro run sql scripts..."
echo "username: $DBUNAME"
echo "database: $DBNAME"

# Migrate instructions

echo  export all players to users
mysql -u$DBUNAME -p$DBPASS $DBNAME < export-players-to-users.sql

echo sort and copy games to webchess_games 
# : order by gameID ASC
mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-games-to-webchess_games.sql

echo sort and copy history to webchess_history
# : order by timeOfMove ASC
mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-history-to-webchess_history.sql

echo sort and copy messages to webchess_messages
# : order by msgID ASC
mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-messages-to-webchess_messages.sql

echo  sort and copy pieces to webchess_pieces
# : order by gameID ASC
mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-pieces-to-webchess_pieces.sql

# sort and copy preferences to webchess_preferences
# : order by uid ASC
# mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-preferences-to-webchess_preferences.sql

echo sort and copy statistics to webchess_statistics
# : order by playerID/uid ASC
mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-statistics-to-webchess_statistics.sql

echo  copy tbl_updates to webchess_tbl_updates
# : order by uid ASC
mysql -u$DBUNAME -p$DBPASS $DBNAME < sort-and-copy-tbl_updates-to-webchess_tbl_updates.sql
