#!/usr/bin/env bash
# to be included by install.sh


####                                                             ####
#                                                                   #
#                 DIASPORA* development setup                       #
#                                                                   #
####                                                             ####


# make ourselves comfy
prepare_install_env() {
  install_or_use_ruby
  load_rvmrc
  js_runtime_check

  log_inf "making sure the 'bundler' gem is installed"
  rvm_or_sudo "gem install bundler"
}


# do some sanity checking
sane_environment_check() {
  binaries_check
  rvm_check
}


# find or set up a working git environment
git_stuff_check() {
  printf "Where would you like to put the git clone, or, where is your existing git clone?\n"
  #printf "(please use a full path, not '~' or '.')\n"
  read -e -p "-> " D_GIT_CLONE_PATH
  printf "\n"

  sanitize_path "$D_GIT_CLONE_PATH" "_D_GCLONE_PATH_EXISTING" "_D_GCLONE_PATH_NEW"
  D_GIT_CLONE_PATH="$_D_GCLONE_PATH_EXISTING$_D_GCLONE_PATH_NEW"

  if [ -n "$_D_GCLONE_PATH_NEW" ] ; then
    # the path obviously doesn't exist yet
    printf "the folder you specified does not exist.\n"
    printf "create '$D_GIT_CLONE_PATH'?\n"
    read -p "Press [Enter] to create it and continue... "

    log_inf "creating '$D_GIT_CLONE_PATH' and cloning the git repo..."
    run_or_error "mkdir -p -v \"$D_GIT_CLONE_PATH\""
    _git_clone_diaspora_repo

  elif ! (cd "$D_GIT_CLONE_PATH" && git status) ; then
    # the path doesn't appear to contain a git clone
    printf "the folder you specified does not contain a git repo\n"
    read -p "Press [Enter] to create it and continue... "

    log_inf "cloning the git repo..."
    _git_clone_diaspora_repo

  else
    cd "$D_GIT_CLONE_PATH"

    log_inf "setting your git clone to '$D_GIT_BRANCH' branch..."
    run_or_error "git stash"
    run_or_error "git checkout \"$D_GIT_BRANCH\""
    run_or_error "git pull"
  fi
  printf "\n"
}

_git_clone_diaspora_repo() {
  run_or_error "git clone \"$D_REMOTE_REPO_URL\" -b \"$D_GIT_BRANCH\" \"$D_GIT_CLONE_PATH\""
}


# handle database decision
database_question() {
  printf "Which database type are you using? [1|2]\n"
  select choice in "MySQL" "PgSQL"; do
    case $choice in
      MySQL )
        D_DB="mysql"
        # we're done, mysql is default
        break
        ;;
      PgSQL )
        D_DB="postgres"
        # replace default with postgres
        run_or_error "sed -i'' -e 's/\(<<: \*mysql\)/#\1/g' \"$D_DB_CONFIG_FILE\""
        run_or_error "sed -i'' -e 's/\(#\(<<: \*postgres\)\)/\2/g' \"$D_DB_CONFIG_FILE\""
        break
        ;;
    esac
  done

  printf "\n"
}


# ask for database credentials
database_credentials() {
  printf "Please specify the database credentials\n(the user must be existent and allowed to create new databases)\n"
  read -e -p "hostname: " D_DB_HOST
  read -e -p "username: " D_DB_USER
  read -e -p "password: " D_DB_PASS

  run_or_error "sed -i'' -e \"s/\(host:\)[^\n]*/\1 $D_DB_HOST/g\" \"$D_DB_CONFIG_FILE\""
  run_or_error "sed -i'' -e \"s/\(username:\)[^\n]*/\1 $D_DB_USER/g\" \"$D_DB_CONFIG_FILE\""
  run_or_error "sed -i'' -e \"s/\(password:\)[^\n]*/\1 $D_DB_PASS/g\" \"$D_DB_CONFIG_FILE\""

  printf "\n"
}


# setup database
# (assume we are in the Diaspora directory)
database_setup() {
  log_inf "Database setup"
  run_or_error "cp config/database.yml.example \"$D_DB_CONFIG_FILE\""
  printf "\n"

  database_question
  database_credentials

  printf "$DATABASE_CHK_MSG"
  read -p "Press [Enter] to continue... "

  printf "\n"
}


# install all the gems with bundler
# (assume we are in the Diaspora directory)
prepare_gem_bundle() {
  log_inf "installing all required gems..."
  rvm_or_sudo "bundle install"
  printf "\n"
}


# main setup function, entry point
# all other functions will be called from here
diaspora_setup() {
  #interactive_check
  root_check

  # display a nice welcome message
  printf "$WELCOME_MSG"
  read -p "Press [Enter] to continue... "

  # check if we have everything we need
  sane_environment_check

  # check git stuff and pull if necessary
  git_stuff_check

  # goto working directory
  run_or_error "cd \"$D_GIT_CLONE_PATH\""
  prepare_install_env

  # configure database setup
  database_setup

  # diaspora config
  log_inf "copying diaspora.yml.example to diaspora.yml"
  run_or_error "cp config/diaspora.yml.example config/diaspora.yml"
  printf "\n"

  # bundle gems
  prepare_gem_bundle

  log_inf "creating the default database specified in config/database.yml. please wait..."
  run_or_error "bundle exec rake db:schema:load_if_ruby --trace"
  printf "\n"

  printf "$GOODBYE_MSG"

  exit 0
}
