#!/bin/bash

if [ -n "$( grep -R "yum.loway.ch" /etc/yum.repos.d )" ]
then
	echo "yum.loway.ch already present in /etc/yum.repos.d. Please investigate manually"
	exit 1
fi

if [ -n "$( find /etc/yum.repos.d -type f -iname "loway.repo*" )" ]
then
	echo "loway.repo already present in /etc/yum.repos.d. Please investigate manually"
	exit 1
fi

if [ "$( which curl > /dev/null 2>&1 ; echo "${?}" )" != "0" ]
then
	echo "Missing curl. Please install it and re-launch the script (yum install curl)"
	exit 1
fi

if [ "$( curl -s -q https://yum.loway.ch > /dev/null 2>&1 ; echo "${?}" )" = "0" ]
then
	curl -s -q https://yum.loway.ch/loway.repo --output /etc/yum.repos.d/loway.repo && \
	echo "SUCCESS"
else
	curl -s -q http://yum.loway.ch/loway.repo --output /etc/yum.repos.d/loway.repo && \
	sed "s:https\::http\::" -i /etc/yum.repos.d/loway.repo && \
	sed '/# If/d' -i /etc/yum.repos.d/loway.repo && \
	sed '/# baseurl/d' -i /etc/yum.repos.d/loway.repo && \
	echo "SUCCESS"
fi
