#!/bin/bash

if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

echo "START: installing Scala"

#Current available version
DEF_VERSION="2.11.6"

RETURN_CODE="$(curl -s -o /dev/null -w "%{http_code}" http://www.scala-lang.org/)"

if [ "$RETURN_CODE" != "200" ]; then
    echo "http://www.scala-lang.org is unreachable" && exit 1
fi

VERSION="$(curl -s --fail http://www.scala-lang.org|grep 'scala-version'|grep -Eo '([0-9]\.?)+')"

if [ $? != 0 -o -z "${VERSION}" ]; then
    echo "Installing default version $DEF_VERSION"
    VERSION=${DEF_VERSION}
fi

PKG=scala-${VERSION}

URL="http://downloads.typesafe.com/scala/${VERSION}"

if [ "$DISTRO_NAME" = "ubuntu" ]; then
    wget -N ${URL}/${PKG}.deb
    dpkg -i ${PKG}.deb
    rm ${PKG}.deb
elif [ "$DISTRO_NAME" = "centos" -o "$DISTRO_NAME" = "rhel" ]; then
    rpm -Uhv ${URL}/${PKG}.rpm
fi

echo "END: installing Scala"

