#!/bin/bash

echo "START: installing Scala"
DEF_VERSION="2.11.6"

if [ $test_only -eq 0 ]; then
    RETURN_CODE="$(curl -s -L -o /dev/null -w "%{http_code}" https://www.scala-lang.org/)"
    if [ "$RETURN_CODE" != "200" ]; then
        echo "https://www.scala-lang.org is unreachable" && exit 1
    fi

    if [ $(lsb_release -c -s) == "trusty" ]; then
        VERSION=${DEF_VERSION}
    else
        if [ "${scala_version}" != "1" ]; then
           VERSION=$scala_version
        else
            VERSION="$(curl -s -L --fail https://www.scala-lang.org| tr -d '\n' | sed 's/^.*<div[^<]\+scala-version">[^0-9]\+\([0-9\.\?]\+\)<.\+$/\1/')"

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

    PKG=scala-${VERSION}

    URL="https://downloads.lightbend.com/scala/${VERSION}"

    wget -N ${URL}/${PKG}.deb
    dpkg -i ${PKG}.deb
    rm ${PKG}.deb
fi

echo "END: installing Scala"

