#!/bin/bash

if [ $EUID != 0 ] ; then
	echo "installer must be run as root"
	exit
fi

if [ ! -f /etc/redhat-release ]; then
	echo "dplus installer only runs on redhat based distributions (CentOS/AlmaLinux)"
	exit
fi
read releasename misc </etc/redhat-release
if [ $releasename == "AlmaLinux" ]; then
	echo "Checking dplus on $releasename based system"
elif [ $releasename == "CentOS" ]; then
	echo "Checking dplus on $releasename based system"
else 
	echo "unknown release $releasename"
	exit
fi

cd /tmp

if [ -f /dstar/tools/dplus -a $# -eq 0 ]; then
	if [ $releasename == "AlmaLinux" ]; then
		curl -s -O http://www.opendstar.org/.admins/dplus-al.md5sum
		mv dplus-al.md5sum dplus.md5sum
	fi
	if [ $releasename == "CentOS" ]; then
		curl -s -O http://www.opendstar.org/.admins/dplus.md5sum
	fi
	cd /dstar/tools

	md5sum --status -c /tmp/dplus.md5sum
	if [ $? -eq 0 ]; then
		echo "dplus is current"
		rm /tmp/dplus.md5sum
		exit
	fi
fi

cd /tmp
curl -O http://www.opendstar.org/.admins/dplus-install.sh
sh ./dplus-install.sh

exit
