#!/usr/bin/bash
# =====================================================
# ts-bootc - TencentOS Bootable Container Management Tool
#
# A simplified bootc-compatible CLI for managing A/B partition
# based container image deployments. No OSTree dependency.
#
# Usage:
#   ts-bootc <command> [options]
#
# Commands:
#   install            Install a container image to a target disk
#   install-to-qcow2   Convert a container image to a bootable qcow2 disk
#   install-to-iso     Convert a container image to a bootable ISO installer
#   upgrade            Download and deploy an updated container image
#   rollback           Switch boot target to the previous deployment
#   status             Display system deployment status
#   help               Print this help message
#
# This tool is distributed as an RPM package (ts-bootc).
# =====================================================

set -euo pipefail

TS_BOOTC_VERSION="1.0.0"
TS_BOOTC_LIBDIR="/usr/lib/ts-bootc"
TS_BOOTC_BINDIR="/usr/libexec/ts-bootc"

# ===== Help =====

usage() {
    cat <<EOF
ts-bootc ${TS_BOOTC_VERSION} - TencentOS Bootable Container Management

Deploy and manage bootable container images with A/B partition scheme.

USAGE:
    ts-bootc <COMMAND> [OPTIONS]

COMMANDS:
    install            Install a container image to a target block device
    install-to-qcow2   Convert a container image to a bootable qcow2 disk
    install-to-iso     Convert a container image to a bootable ISO installer
    upgrade            Download and queue an updated container image to apply
    rollback           Change the boot target to the previous deployment
    status             Display current system deployment status
    help               Print this message or the help of a subcommand

OPTIONS:
    -h, --help       Print help
    -V, --version    Print version

Run 'ts-bootc <COMMAND> --help' for more information on a specific command.
EOF
}

version() {
    echo "ts-bootc ${TS_BOOTC_VERSION}"
}

# ===== Main Dispatch =====

if [ $# -eq 0 ]; then
    usage
    exit 0
fi

COMMAND="$1"
shift

case "$COMMAND" in
    install|install-to-disk|init)
        exec "${TS_BOOTC_BINDIR}/ts-bootc-install.sh" "$@"
        ;;
    install-to-qcow2|to-qcow2)
        exec "${TS_BOOTC_BINDIR}/ts-bootc-install-to-qcow2.sh" "$@"
        ;;
    install-to-iso|to-iso)
        exec "${TS_BOOTC_BINDIR}/ts-bootc-install-to-iso.sh" "$@"
        ;;
    upgrade|update)
        exec "${TS_BOOTC_BINDIR}/ts-bootc-upgrade.sh" "$@"
        ;;
    rollback)
        exec "${TS_BOOTC_BINDIR}/ts-bootc-rollback.sh" "$@"
        ;;
    status)
        exec "${TS_BOOTC_BINDIR}/ts-bootc-status.sh" "$@"
        ;;
    -h|--help|help)
        if [ $# -gt 0 ]; then
            # ts-bootc help <subcommand>
            subcmd="$1"
            case "$subcmd" in
                install|upgrade|rollback|status)
                    exec "${TS_BOOTC_BINDIR}/ts-bootc-${subcmd}.sh" --help
                    ;;
                install-to-qcow2)
                    exec "${TS_BOOTC_BINDIR}/ts-bootc-install-to-qcow2.sh" --help
                    ;;
                install-to-iso)
                    exec "${TS_BOOTC_BINDIR}/ts-bootc-install-to-iso.sh" --help
                    ;;
                *)
                    echo "Error: unknown subcommand '$subcmd'"
                    usage
                    exit 1
                    ;;
            esac
        fi
        usage
        exit 0
        ;;
    -V|--version)
        version
        exit 0
        ;;
    *)
        echo "Error: unknown command '$COMMAND'"
        echo ""
        usage
        exit 1
        ;;
esac
