#!/bin/bash
# Script that contains some utilities.

STEPS_COUNT=1
STEP=0

# File name to store unity log
# Uncomment this line to enable -logFile Unity option
#LOG_FILENAME="log.txt"
LOG_FILENAME=""

ABSOLUTE_PROJECT_ROOT_PATH=""

calculate_paths()
{
  RELATIVE_PATH=$(dirname "$0")
  ABSOLUTE_PATH=$(echo "$(cd "$(dirname ${RELATIVE_PATH})"; pwd)/$(basename ${RELATIVE_PATH})")
  PATH_TO_TOOLS="Assets/Hotel/Editor/Tools"
  PATH_TO_TOOLS_PACKAGE_MANAGER="${PATH_TO_TOOLS}/com.ubisoft.hotel.packagemanager/macOS"
  ABSOLUTE_PROJECT_ROOT_PATH=${ABSOLUTE_PATH//${PATH_TO_TOOLS_PACKAGE_MANAGER}/}
  ABSOLUTE_PATH_TO_TOOLS="${ABSOLUTE_PROJECT_ROOT_PATH}${PATH_TO_TOOLS}"
  UNITY_PATH="$(cat $ABSOLUTE_PATH_TO_TOOLS/unity_editor_path.txt)"
}

prepare_script()
{
  calculate_paths
  cd "${ABSOLUTE_PROJECT_ROOT_PATH}"

  delete_file "$LOG_FILENAME"
}

run_unity_command()
{
  UNITY_COMMAND="${UNITY_PATH} -batchmode"

  if [[ -n $LOG_FILENAME ]]; then
    UNITY_COMMAND="${UNITY_COMMAND} -logFile ${LOG_FILENAME}"
  fi

  run_command "${UNITY_COMMAND} $1"
}

run_command()
{
  STEP=$((STEP+1))

  echo
  echo "-------------------------------------------------------------"
  bcho "Step ${STEP} out of ${STEPS_COUNT}" ":$1"
  echo "-------------------------------------------------------------"
  $1
}

# Usage: bcho <param_1> <param_2>
# Print out <param_1> in bold and $2 in plain text.
bcho()
{
  echo -e "\033[1m$1\033[0m"$2
}

print_warning()
{
  read -p "This command will reset your git project. Commit any unsaved work before proceeding with this command. Do you want to continue (y/n)? " -n 1 -r
  echo    # (optional) move to a new line
  if [[ $REPLY =~ ^[Yy]$ ]]
  then
    echo "continue"
    return 0
  else
    echo "skip"
    return 1
  fi
}

delete_file()
{
  if [[ -f $1 ]]; then
      rm $1
    fi
}

delete_directory()
{
  if [[ -d $1 ]]
  then
      rm -r "$1"
  fi
}

# Returns the amount of files of the directory passed in
# $1: Path to the directory which amount of files needs to be calculated
function get_total_files() 
{
  find $1 -type f | wc -l
}

# Clean up the project
clean_project()
{
  RESULT=$(print_warning)
  if [[ $? == 1 ]]
  then
    return 0
  fi

  # git clean ALL   (WARNING!!!!!!!)
  git reset --hard
  git clean -dfx
  git clean submodule foreach -f 'git reset --hard'
  git clean submodule foreach -f 'git clean -df'
}

function is_a_version_number
{
  local version_number=$1

  local re='^[0-9]+([.][0-9]+)?$'
  if ! [[ $version_number =~ $re ]] ; then
    result=false
  else
    result=true
  fi

  echo $result
}

# Compares two versions
# $1: First version to compare (v1)
# $2: Second version to compare (v2)
# Returns:
#     0: if v1 == v2
#     1: if v1 > v2
#    -1: if v1 < v2
function compare_versions()
{
  local result=0  
  if [[ $1 != $2 ]]; then
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
    do
        ver1[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++))
    do
        if [[ -z ${ver2[i]} ]]
        then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]}))
        then
            result=1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]}))
        then
            result=-1
        fi
    done
  fi

  echo $result
}

function print_compare_versions_result
{
  local op1=$1
  local op2=$2

  local result=$(compare_version $op1 $op2)
  echo $result
  if [[ $result -eq 0 ]]; then
    echo "${op1} == ${op2}"
  elif [[ $result -eq 1 ]]; then
    echo "${op1} > ${op2}"
  else
    echo "${op1} < ${op2}"
  fi
}

function print_header
{
  local message=$1
  echo
  echo
  echo "------------------------------------------------------------------ "
  echo "${message}"
  echo "------------------------------------------------------------------ "
}

function list_print
{
  local list=$1

  if [[ "$list" == "" ]]; then
  echo "[]"
  else
  echo "$list"
  fi
}

function list_add_item
{
  local list=$1
  local item=$2

  if [[ "$list" == "" ]]; then
  list=$item
  else
  list+="\n${item}"
  fi

  echo "$list"
}

function list_contains_item
{
  local list=$1
  local iten=$2

  result=false
  for i in ${list}; do
    if [[ "$i" == "$iten" ]]; then
      result=true
    fi
  done

  echo $result
}
