#!/bin/bash

. ./test/helper.sh

function log() {
	if [[ -t 1 ]]; then
		printf "%b>>>%b %b%s%b\n" "\x1b[1m\x1b[32m" "\x1b[0m" \
		                          "\x1b[1m\x1b[37m" "$1" "\x1b[0m"
	else
		printf ">>> %s\n" "$1"
	fi
}

function error() {
	if [[ -t 1 ]]; then
		printf "%b!!!%b %b%s%b\n" "\x1b[1m\x1b[31m" "\x1b[0m" \
		                          "\x1b[1m\x1b[37m" "$1" "\x1b[0m" >&2
	else
		printf "!!! %s\n" "$1" >&2
	fi
}

function fail() {
	error "$1"
	exit -1
}

function download() {
	if command -v wget >/dev/null; then
		wget -c -O "$2" "$1"
	elif command -v curl >/dev/null; then
		curl -L -C - -o "$2" "$1"
	else
		error "Could not find wget or curl"
		return 1
	fi
}

function detect_system() {
  # adapted from RVM: https://github.com/wayneeseguin/rvm/blob/master/scripts/functions/utility_system#L3
  system_type="unknown"
  system_name="unknown"
  system_version="unknown"
  system_arch="$(uname -m)"

  case "$(uname)" in
    (Linux|GNU*)
      system_type="linux"

      if [[ -f /etc/lsb-release ]] &&
	 [[ "$(< /etc/lsb-release)" == *"DISTRIB_ID=Ubuntu"* ]]
      then
        system_name="ubuntu"
        system_version="$(awk -F'=' '$1=="DISTRIB_RELEASE"{print $2}' /etc/lsb-release)"
        system_arch="$(dpkg --print-architecture)"
      elif [[ -f /etc/lsb-release ]] &&
           [[ "$(< /etc/lsb-release)" == *"DISTRIB_ID=LinuxMint"* ]]
      then
        system_name="mint"
        system_version="$(awk -F'=' '$1=="DISTRIB_RELEASE"{print $2}' /etc/lsb-release)"
        system_arch="$( dpkg --print-architecture )"
      elif [[ -f /etc/lsb-release ]] &&
           [[ "$(< /etc/lsb-release)" == *"DISTRIB_ID=ManjaroLinux"* ]]
      then
        system_name="manjaro"
        system_version="$(awk -F'=' '$1=="DISTRIB_RELEASE"{print $2}' /etc/lsb-release)"
      elif [[ -f /etc/os-release ]] &&
           [[ "$(< /etc/os-release)" == *"ID=opensuse"* ]]
      then
        system_name="opensuse"
        system_version="$(awk -F'=' '$1=="VERSION_ID"{gsub(/"/,"");print $2}' /etc/os-release)" #'
      elif [[ -f /etc/SuSE-release ]]; then
        system_name="suse"
        system_version="$(
          awk -F'=' '{gsub(/ /,"")} $1~/VERSION/ {version=$2} $1~/PATCHLEVEL/ {patch=$2} END {print version"."patch}' < /etc/SuSE-release
        )"
      elif [[ -f /etc/debian_version ]]; then
        system_name="debian"
        system_version="$(< /etc/debian_version | awk -F. '{print $1"."$2}')"
        system_arch="$( dpkg --print-architecture )"
      elif [[ -f /etc/os-release ]] &&
           [[ "$(< /etc/os-release)" == *"ID=debian"* ]]
      then
        system_name="debian"
        system_version="$(awk -F'=' '$1=="VERSION_ID"{gsub(/"/,"");print $2}' /etc/os-release | awk -F. '{print $1"."$2}')" #'
        system_arch="$( dpkg --print-architecture )"
      elif [[ -f /etc/fedora-release ]]; then
        system_name="fedora"
        system_version="$(grep -Eo '[[:digit:]]+' /etc/fedora-release)"
      elif [[ -f /etc/centos-release ]]; then
        system_name="centos"
        system_version="$(grep -Eo '[[:digit:]\.]+' /etc/centos-release | awk -F. '{print $1"."$2}')"
      elif [[ -f /etc/redhat-release ]]; then
        if [[ "$(< /etc/redhat-release)" == *"CentOS"* ]]; then
		system_name="centos"
        else
		system_name="redhat"
        fi

        system_version="$(grep -Eo '[[:digit:]\.]+' /etc/redhat-release | awk -F. '{print $1"."$2}')"
      elif [[ -f /etc/system-release ]] &&
           [[ "$(< /etc/system-release)" == *"Amazon Linux AMI"* ]]
      then
        system_name="amazon"
        system_version="$(grep -Eo '[[:digit:]\.]+' /etc/system-release | awk -F. '{print $1"."$2}')"
      elif [[ -f /etc/gentoo-release ]]; then
        system_name="gentoo"
        system_version="base-$(< /etc/gentoo-release | awk 'NR==1 {print $NF}' | awk -F. '{print $1"."$2}')"
      elif [[ -f /etc/arch-release ]]; then
        system_name="arch"
        system_version="libc-$(ldd --version | awk 'NR==1 {print $NF}' | awk -F. '{print $1"."$2}')"
      else
        system_version="libc-$(ldd --version | awk 'NR==1 {print $NF}' | awk -F. '{print $1"."$2}')"
      fi
      ;;

    (SunOS)
      system_type="sunos"
      system_name="solaris"
      system_version="$(uname -v)"

      if [[ "${system_version}" == joyent* ]]; then
        system_name="smartos"
        system_version="${system_version#* }"
      elif [[ "${system_version}" == oi* ]]; then
        system_name="openindiana"
        system_version="${system_version#* }"
      fi
      ;;

    (OpenBSD)
      system_type="bsd"
      system_name="openbsd"
      system_version="$(uname -r)"
      ;;

    (Darwin)
      system_type="darwin"
      system_name="osx"
      system_version="$(sw_vers -productVersion)"
      system_version="${system_version%.*}" # only major.minor - teeny is ignored
      ;;

    (FreeBSD)
      system_type="bsd"
      system_name="freebsd"
      system_version="$(uname -r)"
      system_version="${system_version%%-*}"
      ;;

    (*)
      return 1
      ;;
  esac

  system_type="${system_type//[ \/]/_}"
  system_name="${system_name//[ \/]/_}"
  system_version="${system_version//[ \/]/_}"
  system_arch="${system_arch//[ \/]/_}"
  system_arch="${system_arch/amd64/x86_64}"
  system_arch="${system_arch/i[123456789]86/i386}"
}

detect_system || fail "Cannot auto-detect system type"
[[ "$system_name" == "unknown"    ]] && fail "Could not detect system name"
[[ "$system_version" == "unknown" ]] && fail "Could not detect system version"
[[ "$system_arch" == "unknown"    ]] && fail "Could not detect system arch"

test_ruby_archive="$test_ruby_engine-$test_ruby_version-p$test_ruby_patchlevel.tar.bz2"
test_ruby_url="http://rvm.io/binaries/$system_name/$system_version/$system_arch/$test_ruby_archive"
test_ruby_root="$test_ruby_engine-$test_ruby_version-p$test_ruby_patchlevel"

mkdir -p "$PREFIX/opt/rubies"
cd "$PREFIX/opt/rubies"

log "Downloading $test_ruby_url ..."
download "$test_ruby_url" "$test_ruby_archive" || fail "Download failed"

log "Unpacking $test_ruby_archive ..."
tar -xjf "$test_ruby_archive" || fail "Unpacking failed"

log "Cleaning up ..."
rm -f "$test_ruby_archive"
