#!/bin/bash

echo "START: installing Scala"
sudo yum -y update
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 [ "${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

   PKG=scala-${VERSION}

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

   rpm -Uhv ${URL}/${PKG}.rpm
fi

echo "END: installing Scala"

