#!/bin/bash

save () {
	echo "Saving the Ehrhart polynomial file and the LattE file that generated it."
	echo "Saved file savedfiles/ehrhart".$3.$2
	echo "Save code: "$2

	cp ehrhart.$3.txt savedfiles/ehrhart.$3.$2
	cp $1 savedfiles/latte.$3.$2
	clean $3
	exit 0
}

clean () {
	rm -f input.$1.txt ehrhart.$1.txt ehrhart.matlabroots.txt ehrhart.mapleroots.txt ehrhart3.matlabCommands.txt ehrhart3.mapleCommands.txt
}

if [[ $1 =~ options ]]
then
	echo "-m: uses matlab to calculate the roots of the ehrhart polynomial and appends it to the ehrhart file"
	echo "-M: uses maple to calculate the roots of the ehrhart polynomial and appends it to the ehrhart file"
	echo "-r: equivalent to -mM"
	echo "-s: saves the ehrhart and latte files if a coefficient of the ehrhart polynomial is negative"
	echo "-R: invokes the -r option and saves the latte and ehrhart files if the real part of a root is positive"
	echo "-S: forces a save of the ehrhart and latte files"
	echo "All files are saved in ./savedfiles"
	exit 0
fi

if [[ $# -lt 2 ]]
then
	echo "Usage: ehrhart2 dim file [options] [comment string]"
	echo "Type ehrhart2 -options for option descriptions"
	exit 0
fi

matlab=0
maple=0
save=0
rootsave=0
forcesave=0
dateStamp=`date | sed s/\ /./g | sed s/:/-/g`

if [[ $# -gt 2 ]]
then
	if [[ $3 =~ r ]]
	then
		matlab=1
		maple=1
	fi
	if [[ $3 =~ R ]]
	then
		matlab=1
		maple=1
		rootsave=1
	fi
	if [[ $3 =~ s ]]
	then
		save=1
	fi
	if [[ $3 =~ S ]]
	then
		forcesave=1
	fi
fi

echo $1 > input.$dateStamp.txt
echo "calling ehrhart "$1 $2 

./ehrhart $1 $2 2> /dev/null | cut -dt -f1 >> input.$dateStamp.txt

echo "Finding polynomial to"
cat input.$dateStamp.txt

if [ $? -eq 0 ]
then
	./ehrhart3 < ./input.$dateStamp.txt > ehrhart.$dateStamp.txt
else
	echo "problem in ehrhart (probably a segfault or interrupt)"
	rm -f input.$dateStamp.txt
	exit 0
fi
 
if [[ $matlab -eq 1 ]]
then
	matlab < ehrhart3.matlabCommands.txt > /dev/null
	echo "*************" >> ehrhart.$dateStamp.txt
	echo "MATLAB ROOTS:" >> ehrhart.$dateStamp.txt
	cat ehrhart.matlabroots.txt >> ehrhart.$dateStamp.txt
fi

if [[ $maple -eq 1 ]]
then

	maple < ehrhart3.mapleCommands.txt > /dev/null
	echo "************" >> ehrhart.$dateStamp.txt
	echo "MAPLE ROOTS:" >> ehrhart.$dateStamp.txt
	cat ehrhart.mapleroots.txt >> ehrhart.$dateStamp.txt
fi

cat ehrhart.$dateStamp.txt 

echo $4 >> ehrhart.$dateStamptxt

if [[ forcesave -eq 1 ]]
then
	save $2 always $dateStamp
fi

if [[ save -eq 1 ]]
then
	if [[ `head -2 ehrhart.$dateStamp.txt | tail -1 |  grep [1-9]` != "" ]]
	then
		save $2 negCoeff $dateStamp
	fi
fi

if [[ rootsave -eq 1 ]]
then
	if [[ `cat ehrhart.matlabroots.txt | grep ^[0-9]` != "" ]]
	then
		save $2 posRoot $dateStamp
	fi
fi


clean $dateStamp
# when saving, the file gets appended with the date (sans spaces and colons) and the option which caused the save
# S > s > R is the precedence of option tags (a forced save overrides all and, arbitrarily, having a negative
# coefficient in the Ehrhart polynomial is more important than having a positive real part of a root
