#! /bin/bash

delete=0
TeX=0
directory="."

if [[ $1 == "" ]]
then
    echo "usage: redo-dominant-pairs -n <max-order> -d <directory> -B -D -P -T -U"
    echo "       -B format output ready as braid programme switches"
    echo "       -d copy seed biquandles from <directory> (defaults to .)"
    echo "       -D delete all existing input and output files from current directory"
    echo "       -P display rows as permutations"
    echo "       -U swap the weld conditions, so weld crossings are welded to the underside of the plane"
    echo "       -T display matrix output in TeX format"
else
	while getopts ":Bn:Dd:PST" opt; 
	do
		case $opt in
			B)
				braid=$opt
				;;
			n)
				max_order=$OPTARG
				;;
			d)
				directory=$OPTARG
				;;
			D)
				delete=1
				;;
			P)
				TeX=1
				format+="P"
				;;
			T)
				TeX=1
				format+="T"
				;;
			U)
				underside=$opt
				;;
			\?)
				echo "Invalid option: -$OPTARG" >&2
				exit 1
				;;
			:)
				echo "Option -$OPTARG requires an argument." >&2
				exit 1
				;;
		esac
	done
	
	echo "max_order = $max_order, delete = $delete, directory = $directory, underside = $underside, TeX = $TeX, format = $format"
	
	if (($delete))
	then
		echo "deleting all existing input and output"
		rm dominant-input-*
		rm essential-pairs-*
		rm weld-pairs-*
		rm virtual-pairs-*
	fi
	
	for i in {2..6}
	do
		if (($i <= $max_order))
		then
			if (($i < 5))
			then
				if (($TeX))
				then
					cat $directory/biquandle-$i-not-quandle-T.txt $directory/quandle-$i-T.txt > dominant-input-$i.txt
				else
					cat $directory/biquandle-$i-not-quandle.txt $directory/quandle-$i.txt > dominant-input-$i.txt
				fi
			else
				if (($TeX))
				then
					cat $directory/quandle-related-biquandle-$i-not-quandle-T.txt $directory/quandle-$i-T.txt > dominant-input-$i.txt
				else
					cat $directory/quandle-related-biquandle-$i-not-quandle.txt $directory/quandle-$i.txt > dominant-input-$i.txt
				fi
			fi
			
			/home/bart/source/biquandle/dominant-pairs -n=$i$braid$underside$format dominant-input-$i.txt 

			if (($TeX))
			then
				mv virtual-pairs-$i.txt virtual-pairs-$i-$format.txt
				mv weld-pairs-$i.txt weld-pairs-$i-$format.txt
				mv essential-pairs-$i.txt essential-pairs-$i-$format.txt
			fi
		fi
	done	
fi

