#!/bin/bash

show_usage() {
    echo "Sử dụng: $0 [start|stop|status|destroy] [single|rac|all]"
    echo "Ví dụ:"
    echo "  $0 start single    # Khởi động Oracle DB 12c"
    echo "  $0 stop rac        # Dừng Oracle RAC"
    echo "  $0 status all      # Kiểm tra trạng thái tất cả"
}

if [ $# -ne 2 ]; then
    show_usage
    exit 1
fi

ACTION=$1
TARGET=$2

manage_db() {
    local db_dir=$1
    local db_name=$2
    
    cd $db_dir
    case $ACTION in
        start)
            echo "Khởi động $db_name..."
            vagrant up
            ;;
        stop)
            echo "Dừng $db_name..."
            vagrant halt
            ;;
        status)
            echo "Trạng thái $db_name:"
            vagrant status
            ;;
        destroy)
            echo "Xóa $db_name..."
            vagrant destroy -f
            ;;
        *)
            echo "Hành động không hợp lệ: $ACTION"
            exit 1
            ;;
    esac
    cd ..
}

case $TARGET in
    single)
        manage_db "oracle-db-12c" "Oracle DB 12c"
        ;;
    rac)
        manage_db "oracle-rac-12c" "Oracle RAC 12c"
        ;;
    all)
        manage_db "oracle-db-12c" "Oracle DB 12c"
        manage_db "oracle-rac-12c" "Oracle RAC 12c"
        ;;
    *)
        echo "Target không hợp lệ: $TARGET"
        show_usage
        exit 1
        ;;
esac