#!/bin/bash

mkdir -p "$BENCH_DIR/bench_output/HDD_IO_performance_(canonical_workload)"
. `dirname "$0"`/DESCRIPTION_RUN > "$BENCH_DIR/bench_output/HDD_IO_performance_(canonical_workload)/DESCRIPTION_RUN"

if [ $DATABASE == "rethinkdb" ]; then
    . `dirname "$0"`/DESCRIPTION > "$BENCH_DIR/bench_output/HDD_IO_performance_(canonical_workload)/DESCRIPTION"
fi

if [ $DATABASE == "mysql" ]; then
    sudo ./raidconfig hdd single

    # Remove bench databases
    rm -rf /usr/local/mysql/var/bench

    # delete_database_files
    rm -f /tmp/raidtest/ib_logfile?
    
    # delete_dangling database_files
    rm -f /tmp/ssd_mount/mysql_data/ib_logfile?
    
    # Initialize InnoDB RAW tablespace for SSD based tests again
    /usr/local/mysql/libexec/mysqld $MYSQL_COMMON_FLAGS $MYSQL_BUFFER_FLAGS $MYSQL_DURABILITY_FLAGS $MYSQL_SSD_CREATE_FLAGS --port=9874 &
    # Wait for server to finish initialization and shut it down
    while ! /usr/local/mysql/bin/mysqladmin shutdown -u root --port=9874 2> /dev/null; do
        sleep 5
    done
fi

if [ $DATABASE == "membase" ]; then
    sudo umount /tmp/raid_mount
    
    sudo ./raidconfig hdd single
fi
