#!/bin/sh
set -e

if [ ! -d Application_Form ]; then
	echo "Missing 'Application_Form' directory"
	exit 1
fi
for f in registrations.csv reviews.csv
do
	if [ ! -f $f ]; then
		echo "Missing $f"
		exit 1
	fi
done

./fix-reviews.sh
rm -rf Selection
mkdir Selection
cd Selection
python3 ../create-evaluation-files.py ../registrations.csv ../reviews.csv ../assign-[0-2].csv
cd ..
mv Selection/students.csv .

perl sohpc-assign.pl slots.csv weights-pref.csv  students.csv project-preferences.csv > select-by-pref.txt
#perl sohpc-assign.pl slots.csv weights-score.csv students.csv project-preferences.csv > select-by-score.txt

# apply tentative selections
if true ; then
	sed -n -e '1aID,selected' -e 's/\([0-9\.]\{4,\}\)[: ]\{1,\}\([1-9][0-9]\{0,\}\).*/-\2,\1/p' select-by-pref.txt > assign-t.csv
	rm -rf Selection
	mkdir Selection
	cd Selection
	python3 ../create-evaluation-files.py ../registrations.csv ../reviews.csv ../assign-[0-2t].csv
	cd ..
	mv Selection/students.csv .
fi

awk '{ if ($2 == "slots,") { $3 = $3 + 1 }; print $0 }' project-preferences.csv > project-preferences-reserves.csv
perl sohpc-assign.pl slots-reserves.csv weights-pref.csv students.csv project-preferences-reserves.csv > select-reserves-by-pref.txt
for i in $(seq 2201 2222) ; do grep ^$i select-reserves-by-pref.txt | tail -n 1 ; done > select-reserves.txt

mkdir Selection/scripts
cp prepare-selection.sh create-evaluation-files.py evaluation-detail-format.html registrations.csv reviews.csv Selection/scripts
cp sohpc-assign.pl slots*.csv weights*.csv students.csv project-preferences.csv select*.txt assign-*.csv Selection/scripts
rm -f Selection.zip
zip -q -r Selection Selection