#!/bin/bash

SRCPATH="/etc/apt/sources.list.d"


if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

DISTRO=$1
COMPONENT=$2
PACKAGE=$3

if [ "x$DISTRO" = "x" ];then
	echo "No distro defined"
	exit 1
fi
if [ "x$COMPONENT" = "x" ];then
	echo "No componente defined"
	exit 1
fi

if [ "x$PACKAGE" = "x" ];then
	echo "No package defined"
	exit 1
fi



IQHSRCFILE=$SRCPATH"/iqhome.list"

# Add source list
if ! [ -f "$IQHSRCFILE" ]; then
	echo "deb http://repo.iqhome.org $DISTRO $COMPONENT" > $IQHSRCFILE
fi

SOURCESDIR="/etc/apt/sources.list.d/"

if [ $DISTRO = "jessie" ]; then

	MOSQLISTFILE="mosquitto-jessie.list"
	# Mosquitto 
	if ! [ -f "$SOURCESDIR$MOSQLISTFILE" ]; then
		wget -P $SOURCESDIR http://repo.mosquitto.org/debian/$MOSQLISTFILE
	fi

	wget -q http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key -O- | apt-key add -

fi


# Add key
wget -q http://repo.iqhome.org/repo.iqhome.org.gpg.key -O- | apt-key add -

apt-get update
apt-get install $PACKAGE -y
