#!/bin/sh

if [ "$(id -gn)" != "rsmc" ]; then exec sudo -g rsmc $0; fi

wget -qN --no-check-certificate https://rsmc.io/api/bamboo/result/RSMC-MOD0/?format=json -O RSMC-versions.json
url=$(tr ',' '\n' < RSMC-versions.json | awk 'BEGIN{FS="\""};/RSMC-JAR/{print $6}' | head -n 1)
file=$(echo $url | tr '/' '\n' | tail -n 1)
if [ -e mods/$file ]; then
	echo "RSMC is up to date"
else
	olddir=$(pwd)
	cd mods
	oldfile=RSMC-*
	wget -qN --no-check-certificate $url
	if [ "$oldfile" != "RSMC-*" ]; then rm -f $oldfile; fi
	cd $olddir
	echo "RSMC has been updated."
	kill -USR1 $(cat rsmc.tentacle)
fi
