#!/usr/bin/env bash

# Output lists of versions
plug_list_versions() {
    echo $(curl -s http://scala-lang.org/download/all.html | \
            grep "download" | \
            egrep -o 'Scala [0-9]+\.[0-9]+\.[0-9]+(\.final|[^\<]*)' | \
            sed 's/Scala\s//' | tail -n+2 | tac)
}

# Return full url for tarball for download
plug_url_for_download() {
    local version=$1
    local arch=$(nv_get_system_arch "x86" "x64")
    local system=$(nv_get_system "linux" "darwin")
    echo "http://scala-lang.org/files/archive/scala-${version}.tgz"
}

# Checks before compilation
plug_check_deps() {
    if [ "`which java`" = "" ]; then
        echo "  To use scala you need some Java version."
        echo "  Please, install system-wide Java."
        echo "  For example, in ubuntu:"
        echo ""
        echo "    $ sudo aptitude install openjdk-7-jdk"
    fi
}

# Like we build
plug_build() {
    local src_path=$1
    local build_path=$2
    cp -r "$src_path"/*/* "$build_path"
}

plug_post_install_actions() {
    plug_install_sbt "$@"
}

plug_install_sbt() {
    local env_name=$1
    local env_name_full=$2
    if [ ! -f "$env_name_full/bin/sbt" ]; then
    (
        local tarball_url="http://dl.bintray.com/sbt/native-packages/sbt/0.13.2/sbt-0.13.2.zip"
        local tarball=$(nv_download_file $tarball_url)
        unzip "$tarball" -d "$env_name_full"
        ln -s `find "${env_name_full}/sbt/bin" -executable -type f` "$env_name_full/bin"
    )
    fi
}
