#!/bin/sh
# Used on metadata.swamid.se and metadata.lab.swamid.se

update()
{
	mkdir -p .time

	# Remove old entitys from DB
	ls swamid-edugain/*.xml swamid-2.0/*.xml swamid-testing/*.xml | sed 's@swamid-.*/\(.*\).xml@.time/\1.validate@' | sort > /tmp/$$.xml
	ls .time/*.validate > /tmp/$$.time
	comm -13 /tmp/$$.xml /tmp/$$.time | while read file; do
		docker exec swamid-metadata-sp /var/www/scripts/removeEntity.bash /opt/metadata/$file
	done
	rm /tmp/$$.xml /tmp/$$.time

	# Update/Import "new" xml-files
	make -f Makefile.validate -s

	# Revalidae entiies in prod not laidated for teh last nr of days
	docker exec swamid-metadata-sp php /var/www/scripts/revalidate.php 14 10

	# check URL:s 
	docker exec swamid-metadata-sp php /var/www/scripts/checkURLs.php

	# update TestResults from release-check -> DB
	docker exec swamid-metadata-sp php /var/www/scripts/updateTestResults.php

	# Cleanup Pending-queue
	docker exec swamid-metadata-sp php /var/www/scripts/cleanupPending.php
}

cd /opt/metadata || exit 1
update
# to be able to show non-swamid entities on web-page
wget -qO swamid-2.0.xml https://mds.swamid.se/md/swamid-2.0.xml