#!/bin/sh -e

pkg=picard-tools

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi

cd $AUTOPKGTEST_TMP

cp -a /usr/share/doc/${pkg}/unmapped.sam .
cp -a /usr/share/doc/${pkg}/test.* .
cp -a /usr/share/doc/${pkg}/mini.vcf* .
gunzip -r *

PicardCommandLine ViewSam PF_STATUS=All ALIGNMENT_STATUS=All INPUT=unmapped.sam

PicardCommandLine CollectSequencingArtifactMetrics I=test.sam O=test R=test.fasta

o1="test.bait_bias_detail_metrics"
o2="test.bait_bias_summary_metrics"
o3="test.error_summary_metrics"
o4="test.pre_adapter_detail_metrics"
o5="test.pre_adapter_summary_metrics"

if [ -e "$o1" ] && [ -e "$o2" ] && [ -e "$o3" ] && [ -e "$o4" ] && [ -e "$o5" ]; then
  echo "All output files generated" >&2
else
  echo "Some files are not generated" >&2
  exit 1
fi

PicardCommandLine SortVcf I=mini.vcf O=mini_sorted.vcf

out_vcf="mini_sorted.vcf"

if [ -e "$out_vcf" ]; then
  echo "sorted vcf generated" >&2
else
  echo "sorted vcf haven't been generated" >&2
  exit 1
fi

PicardCommandLine SplitVcfs I=mini.vcf SNP_OUTPUT=snp.vcf INDEL_OUTPUT=indel.vcf

out_snp="snp.vcf"
out_indel="indel.vcf"

if [ -e "$out_snp" ] && [ -e "$out_indel" ]; then
  echo "snp and indel vcfs generated" >&2
else
  echo "not all output files have been generated" >&2
  exit 1
fi
