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

usage() {
    cat <<'USAGE'
Usage: daylily-stage-analysis-samples-headnode <analysis_samples.tsv> [stage_target]

Generate /fsx staging directories and config/{samples.tsv,units.tsv} from an analysis_samples TSV.
The optional stage_target defaults to /fsx/staged_sample_data.

EXAMPLE <analysis_samples.tsv> can be found in:
  $(daylily-ec resources-dir)/etc/analysis_samples_template.tsv

You will need to replace <REF-BUCKET-NAME> with your actual S3 reference bucket name. However, in production you can use any s3 bucket you have read access to.

sed -i \"s|<REF-BUCKET-NAME>|$reference_bucket|g\"  ./etc/analysis_samples_template.tsv


USAGE
}

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
    usage
    exit 0
fi

if [[ $# -lt 1 || $# -gt 2 ]]; then
    usage
    exit 1
fi

ts="$1"
if [[ ! -f "$ts" ]]; then
    echo "Error: TSV file '$ts' not found" >&2
    exit 1
fi

stage_target="${2:-/fsx/staged_sample_data}"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
python3 "${script_dir}/daylily-analysis-samples-to-manifest-new.py" "$ts" "$stage_target"
