#!/bin/bash

if [ "$#" == "0" ]; then
echo "Error: No arguments" >&2; exit 1;
fi

if [ "$#" == "1" ]; then
        if [ "$1" == "-v" ] || [ "$1" == "--version" ]; then
                docker run docker-tellsort cat /tellysis/tellsort/version.txt; exit 1;
        fi
fi

while (( "$#" )); do
  case "$1" in
    -r1|--read1)
      r1=$2
      shift 2
      ;;
    -r2|--read2)
      r2=$2
      shift 2
      ;;
    -i1|--i1)
      i1=$2
      shift 2
      ;;
    -o|--output)
      output_path=$2
      shift 2
      ;;
    -r|--reference)
      r=$2
      shift 2
      ;;
    -p|--prefix)
      pref=$2
      shift 2
      ;;
    -g|--genome)
      genome_dir=$2
      shift 2
      ;;
    -b|--bed)
      bed=$2
      shift 2
      ;;
    -v|--vcf)
      vcf=$2
      shift 2
      ;;
    -t|--threads)
      threads=$2
      shift 2
      ;;
    -*|--*=|*) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      exit 1
      ;;
  esac
done

mkdir -p $output_path
cd $output_path

#get host system's directories for volume mapping
r2_vol=""
r2_opt=""

i1_path=`readlink -f $(dirname $i1)`
r1_path=`readlink -f $(dirname $r1)`
if [ "$r2" != '' ]; then
	r2_path=`readlink -f $(dirname $r2)`
	r2_vol="-v $r2_path:$r2_path"
fi

r_vol=""
if [ "$r" != '' ]; then
	r_path=`readlink -f $(dirname $r)`
	r_vol=" -v $r_path:$r_path"
fi

i1_f=`readlink -f $i1`
r1_f=`readlink -f $r1`
if [ "$r2" != '' ]; then
        r2_f=`readlink -f $r2`
        r2_opt="--read2 $r2_f"
fi

if [ "$r" != '' ]; then
	r_f=`readlink -f $r`
	r_opt="--ref $r_f"
fi

if [ "$threads" == '' ]; then
  threads=30
fi

genome_name=""
v_vol=""
b_vol=""
g_vol=""
b_f="/tellysis/tellsort/grch38.protein_coding_genes_variants.bed"
v_f="/tellysis/tellsort/GRCh38_GIAB_highconf.NA12878.vcf.gz"
if [ "$bed" != '' ]; then
	b_path=`readlink -f $(dirname $bed)`
  b_f=`readlink -f $bed`
  b_vol="-v $b_path:$b_path"
fi

if [ "$vcf" != '' ]; then
	v_path=`readlink -f $(dirname $vcf)`
  v_f=`readlink -f $vcf`
  v_vol="-v $v_path:$v_path"
fi

if [ $genome_dir != '' ]; then
  g_path=`readlink -f $(dirname $genome_dir)`
  genome_name=`basename $genome_dir`
  g_vol="-v $g_path:$g_path"
fi

#get host user for mapping
uid=$(id -u ${USER})

#get output directory for mapping
outp=`readlink -f $output_path`
mkdir -p $outp

echo "docker run -v $i1_path:$i1_path -v $r1_path:$r1_path $r2_vol $r_vol $b_vol $v_vol $g_vol -v $outp:$output_path docker-tellsort bash -c "
echo "adduser --uid $uid --disabled-password myuser; chown -R myuser:myuser $output_path; su myuser -c 'cp -r $genome_dir /tellysis/tellsort; /tellysis/tellsort/StartTellSortPipeline --read1 $r1_f $r2_opt --barcode $i1_f $r_opt --output /output_path --prefix $pref --threads $threads; /tellysis/tellsort/StartTellSortStats /output_path/$pref.phase $b_f $v_f'"

#run tellsort docker image
docker run \
	-v $i1_path:$i1_path \
	-v $r1_path:$r1_path $r2_vol $r_vol $b_vol $v_vol $g_vol\
	-v $outp:$output_path \
	-w $output_path \
  docker-tellsort \
	bash -c "cp -r $genome_dir /tellysis/tellsort; adduser --uid $uid --disabled-password myuser >> /dev/null 2>&1; chown -R myuser:myuser $output_path; su myuser -c \
          '/tellysis/tellsort/StartTellSortPipeline \
          --read1 $r1_f $r2_opt --barcode $i1_f $r_opt \
          --output $output_path --prefix $pref --threads $threads --genome $genome_name \
          2>&1 | tee $output_path/$(basename $output_path).log'"


