#!/usr/bin/env bash

rdo_header build

rdo_build() {
  local build_args=()
  local arg_repository="${RDO_REPOSITORY:?Not in rdo bash session.}"
  local arg_rebuild=
  local arg_tag_append=

  while true; do
    case "${1}" in
      --compiler|-c)
        build_args+=("--build-arg" "COMPILER=${2:?Missing compiler argument.}")
        shift 2 ;;
      --dry-run)
        build_args+=("--dry-run")
        shift ;;
      --rebuild)
        build_args+=("--rebuild")
        arg_rebuild="yes"
        shift ;;
      --help|-h|'')
        echo "Usage: rdo build COMMAND"
        echo
        echo "Build commands"
        echo
        echo "Options:"
        echo "  -c, --compiler  Select the compiler to use"
        echo "      --dry-run   Do not actually build the container"
        echo "      --rebuild   Rebuild all base images"
        echo
        echo "Commands"
        echo "  all             Compile everything"
        echo "  check           Compile tests"
        echo "  compile         Compile client"
        echo
        echo "Run 'rdo build COMMAND --help' for more information on a command."
        return
        ;;
      *)
        rdo__args__check_unknown_flag rdo_build "${@}" && break || exit 1 ;;
    esac
  done

  local cmd="${1}"; shift || :

  case "${cmd}" in
    all)
      rdo__call_empty rdo_build_all "${@}"
      ;;
    check)
      rdo__call_empty rdo_build_check "${@}"
      ;;
    compile)
      rdo__call_empty rdo_build_compile "${@}"
      ;;
    libtorrent)
      rdo__call_empty rdo_build__libtorrent "${@}"
      ;;
    libtorrent-compile)
      rdo__call_empty rdo_build__libtorrent_compile "${@}"
      ;;
    libtorrent-dev)
      rdo__call_empty rdo_build_libtorrent_dev "${@}"
      ;;
    rtorrent-check)
      rdo__call_empty rdo_build__rtorrent_check "${@}"
      ;;
    rtorrent-compile)
      rdo__call_empty rdo_build__rtorrent_compile "${@}"
      ;;
    rtorrent-dev)
      rdo__call_empty rdo_build_rtorrent_dev "${@}"
      ;;
    *)
      print_error "rdo build: unknown command: ${cmd}"
      return 1
      ;;
  esac
}

rdo_build_all() {
  rdo init verify
  rdo_build__init
  rdo_build__libtorrent_configure
  rdo_build__libtorrent_compile
  rdo_build__rtorrent_configure
  rdo_build__rtorrent_compile
  rdo_build__rtorrent_run
}

rdo_build_check() {
  rdo init verify
  rdo_build__libtorrent_check "${@}"
  rdo_build__rtorrent_check "${@}"
}

rdo_build_compile() {
  rdo init verify
  rdo_build__libtorrent_compile "${@}"
  rdo_build__rtorrent_compile "${@}"
}

rdo_build_libtorrent_dev() {
  rdo init verify

  local image_tag="${arg_repository}/build/libtorrent-stage:default"
  local container_name="rdo-build-libtorrent"

  if [[ "${arg_rebuild}" == "yes" ]] || [[ -z "$(rdo__running_container_id__name "${container_name}")" ]]; then
    rdo_build__init
    rdo_build__libtorrent_configure
    print_space
    rdo run init --rebuild "${image_tag}" "${container_name}"
    print_space
  fi

  docker exec "${container_name}" build-all libtorrent
}

rdo_build_rtorrent_dev() {
  rdo init verify

  local image_tag="${arg_repository}/build/rtorrent-stage:default"
  local container_name="rdo-build-rtorrent"

  if [[ "${arg_rebuild}" == "yes" ]] || [[ -z "$(rdo__container_id__name "${container_name}")" ]]; then
    rdo_build__init
    rdo_build__libtorrent_configure
    rdo_build__libtorrent_compile
    rdo_build__rtorrent_configure
    print_space
    rdo run init --rebuild "${image_tag}" "${container_name}"
    print_space
  fi

  docker exec "${container_name}" build-all rtorrent
}

# Helper functions

rdo_build__build() {
  local build_all_args=("${@}")
  local build_option_args=("${build_all_args[@]:0:${#@}-1}")
  local build_tag="${build_all_args[-1]}"

  if [[ -z "${arg_tag_append}" ]] && [[ ! "${build_tag}" =~ : ]]; then
    build_option_args+=("--tag-append" "default")
  fi

  rdo docker context build "${build_option_args[@]}" "${build_args[@]}" "${build_tag}"
}

rdo_build__compile_status() {
  local context_name="${1:?Missing context name argument.}"
  local image_tag="${arg_repository}/${2:?Missing image tag argument.}"

  if [[ ! "${build_tag}" =~ : ]]; then
    image_tag+=":default"
  fi

  local file_prefix="/deploy/${context_name}/output/status"

  if rdo__image_has_file "${image_tag}" "${file_prefix}.success"; then
    echo "success"
  elif rdo__image_has_file "${image_tag}" "${file_prefix}.failure"; then
    echo "failure"
  else
    print_error "could not get compile status for ${image_tag}"
    return 1
  fi
}

rdo_build__check_compile() {
  local context_name="${1:?Missing context name argument.}"

  if [[ "$(rdo_build__compile_status "libtorrent" "build/stage")" == "success" ]]; then
    print_progress "compile success"
  else
    print_warning "compile failed"
    return 1
  fi
}

rdo_build__init() {
  rdo docker pull "docker/dockerfile:experimental" "alpine:latest"

  rdo_build__build --context-type "ancestor" --context-name "ancestor" --no-rebuild --ancestor-project "rdo-pkg-cache" "ancestor/cache:global"
  rdo_build__build --empty --no-rebuild "cache/apk:global"

  rdo_build__build --context-type "ancestor" --context-name "ancestor" --cache --no-rebuild --ancestor-project "rdo-project" "ancestor:global"
  rdo_build__build --empty --no-rebuild "stage:global"
  rdo_build__build --empty --cache "base/build/compiler"
  rdo_build__build --empty --cache "base/build/rtorrent"

  rdo_build__build --context-type "autogen" --filename "build.autogen" --build-arg "STAGE_TAG=build/stage" "build/stage"
}

ordo_build__libtorrent() {
  rdo_build__libtorrent_configure
  rdo_build__libtorrent_compile
}

rdo_build__libtorrent_configure() {
  rdo_build__build --context-type "compile" --context-name "libtorrent" --filename "build.libtorrent.configure" "build/stage"
}

rdo_build__libtorrent_compile() {
  rdo_build__build --context-type "compile" --context-name "libtorrent" --filename "build.libtorrent.compile" --build-arg "IGNORE_BUILD_ERROR=yes" "build/stage"
  rdo_build__check_compile "libtorrent"
}

rdo_build__rtorrent_configure() {
  rdo_build__build --context-type "compile" --context-name "rtorrent" --filename "build.rtorrent.configure" "build/stage"
}

rdo_build__rtorrent_compile() {
  rdo_build__build --context-type "compile" --context-name "rtorrent" --filename "build.rtorrent.compile" --build-arg "IGNORE_BUILD_ERROR=yes" "build/stage"
  rdo_build__check_compile "rtorrent"
}

rdo_build__rtorrent_run() {
  rdo_build__build --context-type "run" --context-name "rtorrent" "run/rtorrent"
}

rdo_build__libtorrent_check() {
  rdo init verify

  local image_tag="${arg_repository}/build/libtorrent-stage:default"
  local container_name="rdo-check-libtorrent"

  if [[ "${arg_rebuild}" == "yes" ]] || [[ -z "$(rdo__running_container_id__name "${container_name}")" ]]; then
    rdo_build__init
    rdo_build__libtorrent_configure
    rdo_build__libtorrent_compile
    print_space
    rdo run init --rebuild "${image_tag}" "${container_name}"
    print_space
  fi

  docker exec "${container_name}" build-check libtorrent
}

rdo_build__rtorrent_check() {
  rdo init verify

  local image_tag="${arg_repository}/build/rtorrent-stage:default"
  local container_name="rdo-check-rtorrent"

  if [[ "${arg_rebuild}" == "yes" ]] || [[ -z "$(rdo__running_container_id__name "${container_name}")" ]]; then
    rdo_build__init
    rdo_build__libtorrent_configure
    rdo_build__libtorrent_compile
    rdo_build__rtorrent_configure
    rdo_build__rtorrent_compile
    print_space
    rdo run init --rebuild "${image_tag}" "${container_name}"
    print_space
  fi

  docker exec "${container_name}" build-check rtorrent
}

