#!/usr/bin/env bash

bdsm_development_setup()
{
  local _component _separator _string

  printf -v _separator '%*s' "${_columns:-${COLUMNS:-80}}"

  true "${projects_path:="$HOME/projects"}"

  log "\nBDSM development environment setup"

  log "\n=> configuration\n${_separator// /=}"

  if file_contains "$HOME/.bash_profile" "projects_path="
  then
    log "  * projects_path is already set in $HOME/.bash_profile."
  else
    write 'export projects_path="$HOME/projects"' \
      append to "$HOME/.bash_profile"

    log "  * Setting projects_path in $HOME/.bash_profile."
  fi

  if file_contains "$HOME/.bash_profile" "modules/bash/project/interactive"
  then
    log "  * project functions are setup in $HOME/.bash_profile."
  else
    log "  * project functions are now setup in $HOME/.bash_profile."

    write "[[ -s \"${modules_path}/shell/project/interactive\" ]] && source \"${modules_path}/shell/project/interactive\""  \
      append to "$HOME/.bash_profile"
  fi

  ensure_paths_exist "${projects_path}/bdsm"

  ensure_files_exist "${HOME}/.bdsmrc"

  enter "${projects_path}/bdsm"

  for _component in core extensions site
  do
    log "\n=> ${_component}\n${_separator// /=}"
    if file_contains "$HOME/.bdsmrc" "${_component}_development_path"
    then
      log "  * ${_component}_development_path is already set in .bdsmrc"
    else
      write "export ${_component}_development_path=\"${projects_path}/bdsm/${_component}\"" \
        append to "$HOME/.bdsmrc"
      log "Setting ${_component}_development_path in .bdsmrc"
    fi

    if path_exists "${_component}"
    then
      log "  * Repository already exists at $PWD/${_component}/"
    else
      log "  * Cloning Core repository into $PWD/${_component}/"
      case ${_component} in
        core)
          _url="https://github.com/wayneeseguin/bdsm"
          ;;
        *)
          _url="https://github.com/wayneeseguin/bdsm-${_component}"
          ;;
      esac

      if git clone ${_url} ${_component}
      then
        log "  * ${_component} repository successfully cloned into $PWD/${_component}/"
      else
        error "There was an error cloning ${_component} repository from ${_url} into ${PWD}/${_component}"
      fi
    fi
  done

  log "\nBDSM development environment setup has completed."
}

bdsm_development_update()
{
  NIY
}

fetch_version()
{
  version=$(curl -s "${releases_url}/latest-version.txt" 2>/dev/null) ||
    fail "Unable to fetch version from ${releases_url}/latest-version.txt"
}

md5_match()
{ # TODO: switch this to using system module's os_* DSL
  local archive="$1"

  case "$(uname)" in
    Darwin|FreeBSD)
      archive_md5="$(/sbin/md5 -q "${archive}")"
      ;;

    OpenBSD)
      archive_md5="$(/bin/md5 -q "${archive}")"
      ;;

    Linux|*)
      archive_md5="$(md5sum "${archive}" | awk '{print $1}')"
      ;;
  esac

  [[ "${archive_md5}" == "${md5}" ]]

  return $?
}

install_release()
{
  archive="$archives_path/bdsm-${version}.tar.gz"

  md5=$(curl -s "${releases_url}/bdsm-${version}.tar.gz.md5" 2>/dev/null)

  if file_exists "${archive}" && ! md5_match
  then
    # Remove old installs, if they exist and have incorrect md5.
    file_exists "$archives_path/bdsm-${version}.tar.gz" &&
      rm -f "$archives_path/bdsm-${version}.tar.gz"
  fi

  ensure_paths_exist "${archives_path}"

  #TODO make use of fetch_uri
  curl -L "${releases_url}/bdsm-${version}.tar.gz" -o "$archive"

  if ! file_matches_md5 "${archive}" "${md5}"
  then
      fail "
Archive package downloaded does not match it's calculated md5 checksum ${md5}:

  $archives_path/bdsm-${version}.tar.gz

Retry the installation and/or check your networking setup.

Halting the installation.
"
  fi

  tar zxf "${archives_path}/bdsm-${version}.tar.gz" -C "$src_path/"

  enter "$src_path/bdsm-${version}"

  ensure_files_are_executable "install"

  ./install
}

install_remote()
{
  install_bdsm_scm "bdsm-$1" "$1"
}

install_bdsm_scm()
{
  local remote="origin" dir="${1}" owner="${2:-wayneeseguin}"

  modules ext/vcs

  # TODO: This should update to/from repos path and/or archives path...
  fetch_uri "${owner}/bdsm" "${src_path}/${dir}"

  if (( ${sense_of_humor_flag:=0} == 1 ))
  then
    log "Ahh... that's better...\nIt always feels so nice to get head.\nQuite a load this time eh?\nRemember to get head often!\n"
  fi

  enter "${src_path}/${dir}/"

  exec bash ./install --trace="$trace_flags" --debug="$debug_flags"
}

