<commit_before>#!/bin/bash

set -e

readonly BASE_DOWNLOAD="https://www.hpi.uni-potsdam.de/hirschfeld/artefacts/\
smalltalkci"
readonly IMAGE_DOWNLOAD="${BASE_DOWNLOAD}"
readonly VM_DOWNLOAD="http://mirandabanda.org/files/Cog/VM/VM.r3427"
readonly SQUEAK_SSL_DOWNLOAD="${BASE_DOWNLOAD}/linux32.zip"

################################################################################
# Select Squeak image. Exit with '1' if smalltalk_name is unsupported.
# Arguments:
#   Smalltalk image name
# Prints:
#   Image filename string
################################################################################
squeak::get_image_filename() {
  local smalltalk_name=$1

  case "${smalltalk_name}" in
    "Squeak-trunk"|"Squeak-Trunk"|"SqueakTrunk")
      echo "Squeak-Trunk.tar.gz"
      ;;
    "Squeak-5.0"|"Squeak5.0")
      echo "Squeak-5.0.tar.gz"
      ;;
    "Squeak-4.6"|"Squeak4.6")
      echo "Squeak-4.6.tar.gz"
      ;;
    "Squeak-4.5"|"Squeak4.5")
      echo "Squeak-4.5.tar.gz"
      ;;
    *)
      print_error_and_exit "Unsupported Squeak version '${smalltalk_name}'."
      ;;
  esac
}

################################################################################
# Download image if necessary and extract it.
# Globals:
#   IMAGE_DOWNLOAD
#   SMALLTALK_CI_CACHE
#   SMALLTALK_CI_BUILD
#   SMALLTALK_CI_IMAGE
# Arguments:
#   smalltalk_name
################################################################################
squeak::prepare_image() {
  local smalltalk_name=$1
  local image_filename
  local download_url
  local target

  image_filename=$(squeak::get_image_filename "${smalltalk_name}")
  download_url="${IMAGE_DOWNLOAD}/${image_filename}"
  target="${SMALLTALK_CI_CACHE}/${image_filename}"

  if ! is_file "${target}"; then
    travis_fold start download_image "Downloading ${smalltalk_name} testing image..."
      timer_start

      set +e
      download_file "${download_url}" > "${target}"
      if [[ ! $? -eq 0 ]]; then
        print_error_and_exit "Download failed."
      fi
      set -e

      timer_finish
    travis_fold end download_image
  fi

  print_info "Extracting image..."
  tar xzf "${target}" -C "${SMALLTALK_CI_BUILD}"

  if ! is_file "${SMALLTALK_CI_IMAGE}"; then
    print_error_and_exit "Unable to prepare image at '${SMALLTALK_CI_IMAGE}'."
  fi
}

################################################################################
# Get vm filename and path according to build environment. Exit with '1' if
# environment is not supported.
# Globals:
#   SMALLTALK_CI_IMAGE
# Arguments:
#   os_name
#   require_spur: '1' for Spur support
# Prints:
#   'vm_filename|vm_path' string
################################################################################
squeak::get_vm_details() {
  local os_name=$1
  local require_spur=$2
  local vm_filename
  local vm_path

  case "${os_name}" in
    "Linux")
      if [[ "$require_spur" -eq 1 ]]; then
        vm_filename="cogspurlinux-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/cogspurlinux/bin/squeak"
      else
        vm_filename="coglinux-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/coglinux/bin/squeak"
      fi
      ;;
    "Darwin")
      if [[ "$require_spur" -eq 1 ]]; then
        vm_filename="CogSpur.app-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/CogSpur.app/Contents/MacOS/Squeak"
      else
        vm_filename="Cog.app-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/Cog.app/Contents/MacOS/Squeak"
      fi
      ;;
    *)
      print_error_and_exit "Unsupported platform '${os_name}'."
      ;;
  esac

  return_vars "${vm_filename}" "${vm_path}"
}

################################################################################
# Download and extract vm if necessary.
# Globals:
#   VM_DOWNLOAD
#   SMALLTALK_CI_CACHE
#   SMALLTALK_CI_VMS
################################################################################
squeak::prepare_vm() {
  local require_spur=0
  local vm_details
  local vm_filename
  local vm_path
  local download_url
  local target
  local squeakssl_target squeakssl_bin

  is_spur_image "${SMALLTALK_CI_IMAGE}" && require_spur=1
  vm_details=$(squeak::get_vm_details "$(uname -s)" "${require_spur}")
  set_vars vm_filename vm_path "${vm_details}"
  download_url="${VM_DOWNLOAD}/${vm_filename}"
  target="${SMALLTALK_CI_CACHE}/${vm_filename}"
  squeakssl_target="${SMALLTALK_CI_CACHE}/squeakssl.zip"
  squeakssl_bin="${SMALLTALK_CI_CACHE}/linux32/SqueakSSL"

  export SMALLTALK_CI_VM="${vm_path}"

  if ! is_file "${target}"; then
    travis_fold start download_vm "Downloading virtual machine..."
      timer_start

      set +e
      download_file "${download_url}" > "${target}"
      if [[ ! $? -eq 0 ]]; then
        print_error_and_exit "Download failed."
      fi
      set -e

      if [[ "$(uname -s)" = "Linux" ]] && ! is_file "${squeakssl_target}"; then
        travis_fold start download_squeak_ssl "Downloading SqueakSSL plugin..."
          timer_start

          set +e
          download_file "${SQUEAK_SSL_DOWNLOAD}" > "${squeakssl_target}"
          if [[ ! $? -eq 0 ]]; then
            print_error_and_exit "Download failed."
          fi
          unzip "${squeakssl_target}" -d "${SMALLTALK_CI_CACHE}"
          set -e

          timer_finish
        travis_fold end download_squeak_ssl
      fi

      timer_finish
    travis_fold end download_vm
  fi

  if ! is_file "${SMALLTALK_CI_VM}"; then
    print_info "Extracting virtual machine..."
    tar xzf "${target}" -C "${SMALLTALK_CI_VMS}"
    if ! is_file "${SMALLTALK_CI_VM}"; then
      print_error_and_exit "Unable to set vm up at '${SMALLTALK_CI_VM}'."
    fi

    print_info "Patching SqueakSSL plugin..."
    mv "${squeakssl_bin}" "$(dirname "{SMALLTALK_CI_VM}")/../lib/squeak/4.5-3427/"
  fi

  travis_fold start display_vm_version "Cog VM Information"
    "${SMALLTALK_CI_VM}" -version
  travis_fold end display_vm_version
}

################################################################################<commit_msg>Use patched Cog vms instead<commit_after>#!/bin/bash

set -e

readonly BASE_DOWNLOAD="https://www.hpi.uni-potsdam.de/hirschfeld/artefacts/\
smalltalkci"
readonly IMAGE_DOWNLOAD="${BASE_DOWNLOAD}"
readonly VM_DOWNLOAD="${BASE_DOWNLOAD}/vms"

################################################################################
# Select Squeak image. Exit with '1' if smalltalk_name is unsupported.
# Arguments:
#   Smalltalk image name
# Prints:
#   Image filename string
################################################################################
squeak::get_image_filename() {
  local smalltalk_name=$1

  case "${smalltalk_name}" in
    "Squeak-trunk"|"Squeak-Trunk"|"SqueakTrunk")
      echo "Squeak-Trunk.tar.gz"
      ;;
    "Squeak-5.0"|"Squeak5.0")
      echo "Squeak-5.0.tar.gz"
      ;;
    "Squeak-4.6"|"Squeak4.6")
      echo "Squeak-4.6.tar.gz"
      ;;
    "Squeak-4.5"|"Squeak4.5")
      echo "Squeak-4.5.tar.gz"
      ;;
    *)
      print_error_and_exit "Unsupported Squeak version '${smalltalk_name}'."
      ;;
  esac
}

################################################################################
# Download image if necessary and extract it.
# Globals:
#   IMAGE_DOWNLOAD
#   SMALLTALK_CI_CACHE
#   SMALLTALK_CI_BUILD
#   SMALLTALK_CI_IMAGE
# Arguments:
#   smalltalk_name
################################################################################
squeak::prepare_image() {
  local smalltalk_name=$1
  local image_filename
  local download_url
  local target

  image_filename=$(squeak::get_image_filename "${smalltalk_name}")
  download_url="${IMAGE_DOWNLOAD}/${image_filename}"
  target="${SMALLTALK_CI_CACHE}/${image_filename}"

  if ! is_file "${target}"; then
    travis_fold start download_image "Downloading ${smalltalk_name} testing image..."
      timer_start

      set +e
      download_file "${download_url}" > "${target}"
      if [[ ! $? -eq 0 ]]; then
        print_error_and_exit "Download failed."
      fi
      set -e

      timer_finish
    travis_fold end download_image
  fi

  print_info "Extracting image..."
  tar xzf "${target}" -C "${SMALLTALK_CI_BUILD}"

  if ! is_file "${SMALLTALK_CI_IMAGE}"; then
    print_error_and_exit "Unable to prepare image at '${SMALLTALK_CI_IMAGE}'."
  fi
}

################################################################################
# Get vm filename and path according to build environment. Exit with '1' if
# environment is not supported.
# Globals:
#   SMALLTALK_CI_IMAGE
# Arguments:
#   os_name
#   require_spur: '1' for Spur support
# Prints:
#   'vm_filename|vm_path' string
################################################################################
squeak::get_vm_details() {
  local os_name=$1
  local require_spur=$2
  local vm_filename
  local vm_path

  case "${os_name}" in
    "Linux")
      if [[ "$require_spur" -eq 1 ]]; then
        vm_filename="cogspurlinux-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/cogspurlinux/squeak"
      else
        vm_filename="coglinux-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/coglinux/squeak"
      fi
      ;;
    "Darwin")
      if [[ "$require_spur" -eq 1 ]]; then
        vm_filename="CogSpur.app-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/CogSpur.app/Contents/MacOS/Squeak"
      else
        vm_filename="Cog.app-15.33.3427.tgz"
        vm_path="${SMALLTALK_CI_VMS}/Cog.app/Contents/MacOS/Squeak"
      fi
      ;;
    *)
      print_error_and_exit "Unsupported platform '${os_name}'."
      ;;
  esac

  return_vars "${vm_filename}" "${vm_path}"
}

################################################################################
# Download and extract vm if necessary.
# Globals:
#   VM_DOWNLOAD
#   SMALLTALK_CI_CACHE
#   SMALLTALK_CI_VMS
################################################################################
squeak::prepare_vm() {
  local require_spur=0
  local vm_details
  local vm_filename
  local vm_path
  local download_url
  local target

  is_spur_image "${SMALLTALK_CI_IMAGE}" && require_spur=1
  vm_details=$(squeak::get_vm_details "$(uname -s)" "${require_spur}")
  set_vars vm_filename vm_path "${vm_details}"
  download_url="${VM_DOWNLOAD}/${vm_filename}"
  target="${SMALLTALK_CI_CACHE}/${vm_filename}"
  squeakssl_target="${SMALLTALK_CI_CACHE}/squeakssl.zip"
  squeakssl_bin="${SMALLTALK_CI_CACHE}/linux32/SqueakSSL"

  export SMALLTALK_CI_VM="${vm_path}"

  if ! is_file "${target}"; then
    travis_fold start download_vm "Downloading virtual machine..."
      timer_start

      set +e
      download_file "${download_url}" > "${target}"
      if [[ ! $? -eq 0 ]]; then
        print_error_and_exit "Download failed."
      fi
      set -e

      timer_finish
    travis_fold end download_vm
  fi

  if ! is_file "${SMALLTALK_CI_VM}"; then
    print_info "Extracting virtual machine..."
    tar xzf "${target}" -C "${SMALLTALK_CI_VMS}"
    if ! is_file "${SMALLTALK_CI_VM}"; then
      print_error_and_exit "Unable to set vm up at '${SMALLTALK_CI_VM}'."
    fi
  fi

  travis_fold start display_vm_version "Cog VM Information"
    "${SMALLTALK_CI_VM}" -version
  travis_fold end display_vm_version
}

################################################################################
{'fraction_non_alphanumeric': 0.24293381037567083, 'fraction_numerical': 0.012790697674418604, 'mean_word_length': 4.016150740242261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6221827', 'n_tokens_mistral': 3905, 'n_tokens_neox': 3693, 'n_words': 681}