#!/bin/bash

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

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

ACTION=$1
TARGET=$2

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

case $TARGET in
    oracle)
        manage_vm "oracle-linux-8" "Oracle Linux 8"
        ;;
    ubuntu)
        manage_vm "ubuntu-server-22.04" "Ubuntu Server 22.04"
        ;;
    all)
        manage_vm "oracle-linux-8" "Oracle Linux 8"
        manage_vm "ubuntu-server-22.04" "Ubuntu Server 22.04"
        ;;
    *)
        echo "Target không hợp lệ: $TARGET"
        show_usage
        exit 1
        ;;
esac