#! /bin/bash

BASEDIR=$PWD

scripts_path=$1
config_file=$2
DIR1=${BASEDIR}/$3
DIR2=${BASEDIR}/$4
DIR3=${BASEDIR}/$5
output_lib_name_prefix=$6

mkdir working_${output_lib_name_prefix}
cd working_${output_lib_name_prefix}
WORKDIR=$PWD

mkdir output_libraries
mkdir log
cd output_libraries

for dir1_file in $DIR1/*; do
	for dir2_file in $DIR2/*; do

		# Returns just file name without full dir
		file_1="$(basename $dir1_file)"
		file_2="$(basename $dir2_file)"

		job="$file_1-$file_2"

		# returns each file's HAC
		hac_1="$(echo $file_1 | cut -d'_' -f4)"
		hac_2="$(echo $file_2 | cut -d'_' -f4)"
		skeleton_hac=8
		smallest_hac_3=7

		# HAC of both files plus skeleton HAC
		total_hac="$(($hac_1 + $hac_2 + $smallest_hac_3 + $skeleton_hac))"

		if [ $total_hac -lt $((33)) ]
		then
			# echo "$total_hac"
			array_length=$( ls $DIR3/ | wc -l )
			qsub -N h3_enum_$job -o ${WORKDIR}/log/$job.log -e ${WORKDIR}/log/$job.err -t 1-$array_length ${scripts_path}/combine_line_3reagents.sh ${scripts_path}/enumerate_product.py ${BASEDIR}/$config_file $dir1_file $dir2_file $DIR3 $output_lib_name_prefix $WORKDIR
		fi
	
	done
done
