#!/bin/sh -e
#
#  Copyright (C) 2015 ScyllaDB

if [ "`id -u`" -ne 0 ]; then
    echo "Requires root permission."
    exit 1
fi

print_usage() {
    echo "scylla_install_pkg -l /home/scylla/rpms"
    echo "  -l	install locally built .rpm/.deb on specified directory"
    exit 1
}

LOCAL_PKG=
while getopts l:h OPT; do
    case "$OPT" in
        "l")
            LOCAL_PKG=$OPTARG
            ;;
        "h")
            print_usage
            ;;
    esac
done

. /etc/os-release

if [ "$ID" = "ubuntu" ]; then
    if [ "$LOCAL_PKG" = "" ]; then
        echo "deb http://s3.amazonaws.com/downloads.scylladb.com/deb/ubuntu trusty/scylladb multiverse" > /etc/apt/sources.list.d/scylla.list
        apt-get update
        apt-get install -y --force-yes scylla-server scylla-jmx scylla-tools
    else
        apt-get install -y --force-yes gdebi-core
        gdebi $LOCAL_PKG/scylla-server*.deb $LOCAL_PKG/scylla-jmx*.deb $LOCAL_PKG/scylla-tools*.deb
    fi
else
    if [ "$ID" = "fedora" ]; then
        curl https://s3.amazonaws.com/downloads.scylladb.com/rpm/fedora/scylla.repo > /etc/yum.repos.d/scylla.repo
    elif [ "$ID" = "centos" ] || [ "$ID" = "rhel" ]; then
        curl https://s3.amazonaws.com/downloads.scylladb.com/rpm/centos/scylla.repo > /etc/yum.repos.d/scylla.repo
	yum install -y epel-release
    else
        echo "Unsupported distribution"
        exit 1
    fi

    if [ "$LOCAL_PKG" = "" ]; then
        yum install -y scylla-server scylla-server-debuginfo scylla-jmx scylla-tools
    else
        yum install -y $LOCAL_PKG/scylla-server*.x86_64.rpm $LOCAL_PKG/scylla-jmx*.noarch.rpm $LOCAL_PKG/scylla-tools*.noarch.rpm
    fi
fi
