#!/usr/bin/env sh
set -u

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
CONFIG_FILE=${CONFIG_FILE:-"$HOME/Saleculator.properties"}

show_intro() {
  cat <<EOF

Saleculator First Run Setup
===========================

This assistant prepares Saleculator for trial or live use.
Use option 1 for a single POS or the main POS/server in a network.
Use option 2 for extra POS machines that connect to an existing database.

Requirements:
- Java 8 or newer
- MySQL Server and MySQL client tools for standalone/main POS
- MySQL client tools for network clients

Note: this assistant configures Saleculator and MySQL access. It does not
silently install MySQL Server; install MySQL first for standalone/main POS.

EOF
}

launch_prompt() {
  printf 'Launch Saleculator now? [Y/n]: '
  read answer
  case "$answer" in
    n|N) exit 0 ;;
    *) exec "$ROOT_DIR/scripts/run.sh" "$CONFIG_FILE" ;;
  esac
}

show_intro

while :; do
  cat <<EOF
Select setup action:
  1. Standalone or main POS database on this machine
  2. Network client connecting to an existing POS database
  3. Test current database connection
  4. Run preflight check
  5. Launch Saleculator
  6. Collect support info
  7. Exit

EOF
  printf 'Enter 1-7: '
  read choice

  case "$choice" in
    1)
      printf 'Will other POS machines connect to this database? [y/N]: '
      read allow_remote
      if [ "$allow_remote" = "y" ] || [ "$allow_remote" = "Y" ]; then
        ALLOW_REMOTE_CLIENTS=true CONFIG_FILE="$CONFIG_FILE" "$ROOT_DIR/scripts/configure-db.sh" local || exit 1
      else
        CONFIG_FILE="$CONFIG_FILE" "$ROOT_DIR/scripts/configure-db.sh" local || exit 1
      fi
      "$ROOT_DIR/scripts/preflight.sh" "$CONFIG_FILE"
      printf '\nNext: start Saleculator and approve database creation if prompted.\n'
      printf 'For a 30 day trial, use the START TRIAL button on the activation screen,\n'
      printf 'then paste the trial serial key into Saleculator and activate it.\n\n'
      launch_prompt
      ;;
    2)
      printf 'Database server IP/name: '
      read db_host
      printf 'Database name [SALECULATOR]: '
      read db_name
      [ -n "$db_name" ] || db_name=SALECULATOR
      printf 'Database user [saleculator]: '
      read db_user
      [ -n "$db_user" ] || db_user=saleculator
      CONFIG_FILE="$CONFIG_FILE" "$ROOT_DIR/scripts/configure-db.sh" network "$db_host" "$db_name" "$db_user" || exit 1
      "$ROOT_DIR/scripts/preflight.sh" "$CONFIG_FILE"
      printf '\nNext: start Saleculator and login with the shop assigned user/PIN.\n'
      printf 'For a 30 day trial, use the START TRIAL button on the activation screen,\n'
      printf 'then paste the trial serial key into Saleculator and activate it.\n\n'
      launch_prompt
      ;;
    3)
      printf 'Database server IP/name [localhost]: '
      read db_host
      [ -n "$db_host" ] || db_host=localhost
      printf 'Database name [SALECULATOR]: '
      read db_name
      [ -n "$db_name" ] || db_name=SALECULATOR
      printf 'Database user [saleculator]: '
      read db_user
      [ -n "$db_user" ] || db_user=saleculator
      "$ROOT_DIR/scripts/configure-db.sh" test "$db_host" "$db_name" "$db_user" || exit 1
      ;;
    4)
      "$ROOT_DIR/scripts/preflight.sh" "$CONFIG_FILE"
      ;;
    5)
      exec "$ROOT_DIR/scripts/run.sh" "$CONFIG_FILE"
      ;;
    6)
      "$ROOT_DIR/scripts/support-info.sh"
      ;;
    7)
      exit 0
      ;;
    *)
      printf 'Invalid choice.\n'
      ;;
  esac
  printf '\n'
done
