#!/usr/bin/env bash
set -euo pipefail

print_help() {
    cat <<EOF
Usage: $(basename "$0") [OPTIONS] "<SQL_QUERY>"

Run a single‐column SQL query via qdb-cli, strip header and quotes, and copy the results to the clipboard.

OPTIONS:
  -h, --help    Show this help message and exit

EXAMPLE:
  $(basename "$0") "SELECT bid_price FROM your_instrument WHERE spread_mark < 0"
EOF
}

# If no arguments given, show help
if [[ $# -eq 0 ]]; then
    print_help
    exit 1
fi

# Parse options
while [[ $# -gt 0 ]]; do
    case "$1" in
    -h | --help)
        print_help
        exit 0
        ;;
    --) # end of options
        shift
        break
        ;;
    -*) # unknown option
        echo "Unknown option: $1" >&2
        print_help
        exit 1
        ;;
    *) # first non-option argument is the query
        break
        ;;
    esac
done

# Join all remaining args into a single SQL query
query="$*"

# Execute the query, strip header row and quotes, then copy to clipboard
qdb-cli exp "$query" |
    tail -n +2 |
    tr -d '"' | # strip quotes
    tr -d '\r' |
    pbcopy
