#!/usr/bin/env bash

# Скриптик для установки атрибута @tablet_cell_bundle на всех таблицах в директории
# Перед установкой атрибута выполняется unmount, а после -- mount
# Пример запуска:
#
# sudo YT_PROXY=hahn YT_TOKEN_PATH=/etc/direct-tokens/yt_robot-direct-yt ./set-tablet-cell-bundle.sh '//home/direct/test/santama/mysql-sync/v.15/ppc:1/straight' direct

if [ "$#" -ne 2 ]; then
    >&2 echo "Usage: YT_PROXY=<cluster> YT_TOKEN_PATH=<token_path> $0 <bundle-name>"
    exit 2
fi

# Можно получать таблицы через yt list
BASE_DIR="$1"
TABLES=$(yt list "${BASE_DIR}")
BUNDLE="$2"

while true; do
    read -p "Unmount all dynamic tables from ${BASE_DIR} ? (y,n) " yn
    case $yn in
        [Yy]* )
            for table in ${TABLES[@]}; do
                echo "Unmounting ${BASE_DIR}/${table}.."
                yt unmount-table "${BASE_DIR}/${table}"
            done
        break;;

        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

while true; do
    read -p "Set @tablet_cell_bundle = ${BUNDLE} for all tables ? (y,n) " yn
    case $yn in
        [Yy]* )
            for table in ${TABLES[@]}; do
                echo "Set ${BASE_DIR}/${table}/@tablet_cell_bundle = ${BUNDLE}.."
                yt set "${BASE_DIR}/${table}/@tablet_cell_bundle" ${BUNDLE}
            done
        break;;

        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

while true; do
    read -p "Mount all dynamic tables ? (y,n) " yn
    case $yn in
        [Yy]* )
            for table in ${TABLES[@]}; do
                echo "Mounting ${BASE_DIR}/${table}.."
                yt mount-table "${BASE_DIR}/${table}"
            done
        break;;

        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo "Finished OK"
