#!/usr/bin/env bash

brt_extract() {
  local archive="${1:-}"
  local dest="${2:-.}"

  if [[ -z "$archive" ]]; then
    brt_error "Usage: brt -extract <file.zip|file.tar.gz|file.tgz> [destination]"
    exit 1
  fi

  if [[ ! -f "$archive" ]]; then
    brt_error "File not found: ${archive}"
    exit 1
  fi

  mkdir -p "$dest"
  brt_info "Extracting ${archive} to ${dest}..."

  case "$archive" in
    *.tar.gz|*.tgz) tar -xzf "$archive" -C "$dest" ;;
    *.tar.bz2|*.tbz2) tar -xjf "$archive" -C "$dest" ;;
    *.tar.xz|*.txz) tar -xJf "$archive" -C "$dest" ;;
    *.tar) tar -xf "$archive" -C "$dest" ;;
    *.zip) unzip -q "$archive" -d "$dest" ;;
    *.gz) gunzip -k "$archive" ;;
    *)
      brt_error "Unsupported archive format: ${archive}"
      exit 1
      ;;
  esac

  brt_ok "Extracted to ${dest}"
}
