# VAULT Configuration
export VAULT_ADDR=http://127.0.0.1:8200
export VAULT_TOKEN=hvs.h95bU6tIFpVGob2OBYVN1LSm

vault secrets disable my_test > /dev/null
vault secrets enable -path /my_test -version=2 kv > /dev/null
vault kv put /my_test/1 data="59af70fb2f9f9e0acbff9af9c81dec91" > /dev/null
vault kv put /my_test/2 data="32913cf9ff0afb47c9b531f218bbec48" > /dev/null


# Set Basedir 

BASEDIR=/test/mtest/MDEV-30847/MD111125-mariadb-12.2.0-linux-x86_64-dbg

for i in `seq 1 2`; do
  data="${BASEDIR}/data${i}"
  ${BASEDIR}/bin/mariadb-admin -uroot -S${data}/socket.sock shutdown
done

MYEXTRA="--no-defaults --sql_mode= --innodb_encrypt_tables=ON --innodb_encrypt_temporary_tables=ON --innodb_encrypt_log=ON --innodb_encryption_threads=4 --innodb_encryption_rotate_key_age=1 --plugin-load-add=hashicorp_key_management.so --hashicorp-key-management --hashicorp-key-management-vault-url=http://127.0.0.1:8200/v1/my_test --hashicorp-key-management-token=hvs.h95bU6tIFpVGob2OBYVN1LSm --hashicorp-key-management-check-kv-version=on --deadlock-timeout-short=10 --deadlock-timeout-long=10 --deadlock-search-depth-short=10 --deadlock-search-depth-long=33"

for i in `seq 1 2`; do
  PORT=$((4400 + i))
  tmp="${BASEDIR}/tmp${i}"
  data="${BASEDIR}/data${i}"
  rm -Rf ${data} ${tmp}
  mkdir ${tmp}
  
  ${BASEDIR}/scripts/mariadb-install-db --no-defaults --force --auth-root-authentication-method=normal --tmpdir=${BASEDIR}/tmp  --basedir=${BASEDIR} --datadir=${data} 2>&1 
  ${BASEDIR}/bin/mariadbd ${MYEXTRA} --core-file --basedir=${BASEDIR} --tmpdir=${tmp} --datadir=${data}   --socket=${data}/socket.sock --port=$PORT --log-error=${data}/master.err  2>&1 &
  sleep 5
done


for i in `seq 1 2`; do
  data="${BASEDIR}/data${i}"
  ${BASEDIR}/bin/mariadb -A -uroot -S${data}/socket.sock test -e"CREATE OR REPLACE TABLE t1 (c1 INT) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=1;CREATE OR REPLACE TABLE t2 (c1 INT) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=2;" 2>&1 
done

for i in `seq 1 100`; do
${BASEDIR}/bin/mariadb -A -uroot -S${data}/socket.sock test <<EOF
  CREATE OR REPLACE TABLE t2 (c1 INT) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=1;  
  FLUSH HASHICORP_KEY_MANAGEMENT_CACHE;  
  CREATE TABLE t3 (c1 INT) ENGINE=InnoDB ROW_FORMAT=COMPRESSED ENCRYPTED=NO;  
  CREATE OR REPLACE TABLE t4 (c1 INT) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=2;  
  FLUSH HASHICORP_KEY_MANAGEMENT_CACHE;
EOF
done

