#!/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/staging/staged_external_sequencing_data.

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

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

sed -i \"s|<REF-S3-URI>|$reference_s3_uri|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/staging/staged_external_sequencing_data}"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
python3 "${script_dir}/daylily-analysis-samples-to-manifest-new.py" "$ts" "$stage_target"
