#!/usr/bin/env bash
if [ x$BASH = x ] || [ ! $BASH_VERSINFO ] || [ $BASH_VERSINFO -lt 4 ]; then
  echo "Error: Must use bash version 4+." >&2
  exit 1
fi
set -ue

INITIAL_BARCODES_DEFAULT=20

Usage="Usage: \$ $(basename $0) [barcodes to try]"

function main {
  if [[ $# -ge 1 ]] && [[ $1 == '-h' ]]; then
    fail "$Usage"
  fi

  initial_barcodes="$INITIAL_BARCODES_DEFAULT"
  if [[ $# -ge 1 ]]; then
    initial_barcodes=$1
  fi

  cat border-families.txt | paste - - | shuf --random-source=border-families.txt \
      | head -n $initial_barcodes | while read count1 barcode order count2 rest; do
    echo -ne "$barcode\t$count1\t$count2\t"
    read_name=$(grep -B 1 $barcode barcodes.fq | head -n 1 | tail -c +2)
    echo "$read_name"
    samtools view -f 256 barcodes.bam | awk '$1 == '$read_name' && $5 > 25 {print $3}' | while read read_name2; do
      barcode=$(grep -A 1 -E '^@'$read_name2'$' barcodes.fq | tail -n 1)
    done
  done

}

function fail {
  echo "$@" >&2
  exit 1
}

main "$@"
