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

DATA_DIR=/home/ddpanchenko/MPRA/TOMSK_HEK293/data
OUT_DIR=/home/ddpanchenko/MPRA/TOMSK_HEK293/alignments
REF_DIR=/home/ddpanchenko/MPRA/TOMSK_HEK293/data/references
THREADS=8

SAMPLES=(HEK293  HEK293T  Viral_production_cell)
REFERENCES=(hg38  hg38_add_seq)


REMOTE_HOST=gdev
REMOTE_BASE=/home/ddpanchenko/ftp_link/side_tasks/TOMSK_HEK293
REMOTE_REF_DIR="${REMOTE_BASE}/references"

ssh "${REMOTE_HOST}" "mkdir -p '${REMOTE_REF_DIR}' ${REMOTE_BASE}/{$(IFS=,; echo "${SAMPLES[*]}")}"

for sample in "${SAMPLES[@]}"; do
  echo "== Sample: ${sample}"

  LOCAL_SAMPLE_DIR="${OUT_DIR}/${sample}"
  REMOTE_SAMPLE_DIR="${REMOTE_BASE}/${sample}"

  for ref in "${REFERENCES[@]}"; do
    basename="${sample}.${ref}.q30"

    for ext in bam bam.bai bw; do
      src="${LOCAL_SAMPLE_DIR}/${basename}.${ext}"
      dst="${REMOTE_SAMPLE_DIR}/"

      if [[ -f "${src}" ]]; then
        echo "  → scp ${src}  ->  ${REMOTE_HOST}:${dst}"
        scp "${src}" "${REMOTE_HOST}:${dst}"
      else
        echo "  ⚠️  no file: ${src}" >&2
      fi
    done
  done
done

for ref in "${REFERENCES[@]}"; do
  reference_dir="${REF_DIR}/${ref}"
  ref_fa="${reference_dir}/${ref}.fna"

  for ext in "" ".fai"; do
    src="${ref_fa}${ext}"
    if [[ -f "${src}" ]]; then
      echo "REF → scp ${src} -> ${REMOTE_HOST}:${REMOTE_REF_DIR}"
      scp "${src}" "${REMOTE_HOST}:${REMOTE_REF_DIR}"
    else
      echo "  ⚠️  no ref file: ${src}" >&2
    fi
  done
done