#!/usr/bin/env bash

. /usr/share/dosis2/bin/shell-support-functions

OS="`getos`"

# if another value isn't given default to 32kBps. 
LIMIT="${1:-32}"

function error() {
    echo -e "\033[031m$1\033[0m"
}

if [ ${UID} != 0 ]; then
    error "This script must be ran as root"
    exit 1
fi


if [ "${OS}" = "Ubuntu" ]; then
echo "
Acquire
{
   Queue-mode \"access\";
   http
   {
      Dl-Limit \"${LIMIT}\";
   };
};
" > /etc/apt/apt.conf.d/75lowerspeed
cat /etc/apt/apt.conf.d/75lowerspeed
elif [ "${OS}" = "CentOS" ]; then
	if grep -q "#throttle" /etc/yum.conf; then
		sed -i "s/#throttle/throttle/" /etc/yum.conf
	fi

	if grep -q "throttle=" /etc/yum.conf; then
		sed -i "s/throttle=.*/throttle=${LIMIT}k/" /etc/yum.conf
	elif grep -q "throttle =" /etc/yum.conf; then
		sed -i "s/throttle = .*/throttle=${LIMIT}k/" /etc/yum.conf
	else
		sed -i "/^debuglevel.*/a throttle=${LIMIT}k" /etc/yum.conf
	fi
	cat /etc/yum.conf
else
	error "Unknown OS: ${OS}"
	exit 1
fi
