#!/bin/sh

## Feeding CSV as follows:
##	user,team
#	j2100,Teacher

## This script can be run repeatedly in order to change team allocation.

DB=${1:-users.sq3}
CSV=${2:-newgrp.csv}
sqlite3 ${DB} <<-EOF
	.mode csv
	CREATE TEMPORARY TABLE newgrp(user, team);
	.read inittbl.sql
	.import $CSV newgrp
	DELETE FROM newgrp WHERE user LIKE 'user';
	INSERT INTO teams SELECT distinct team FROM newgrp
			  WHERE team NOT IN (SELECT team FROM teams);
	PRAGMA foreign_keys=off;		-- To avoid score deletion
	UPDATE users SET team=(SELECT team FROM newgrp WHERE user=users.user)
	WHERE user IN (SELECT user FROM newgrp);
EOF
