#!/usr/bin/env bash

# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml

# ---- Start unofficial bash strict mode boilerplate
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit  # always exit on error
set -o errtrace # trap errors in functions as well
set -o pipefail # don't ignore exit codes when piping output
set -o posix    # more strict failures in subshells
# set -x          # enable debugging

# ---- End unofficial bash strict mode boilerplate

package_name=$1
package_path=$2
yalc_packages_file_name="yalc-packages"
container_id="$(docker-compose ps -q api)"
container_copied_packages_path="/home/node/copied-packages"

# validate input
IFS='/'
read -a pkgarr <<< "$1"
org_name=${pkgarr[0]}
package_name_without_org=${pkgarr[1]}
if [ -z "$package_name_without_org" ] || [ -z "$org_name" ]; then
  echo "ERROR: Incorrect input. Please check the format of the first argument. '@<org-name>/<api-plugin-name>'"
  exit 0
fi

#check if the organization name is valid by cross referencing with node_modules
is_package=$(find node_modules -type d -name $org_name)
if [ -z "$is_package" ]; then
  echo "$org_name does not exist"
  exit 0
fi


IFS="$(printf "\n\t")"

if [[ -z "${package_name}" ]]; then
  if ! [[ -f "$yalc_packages_file_name" ]]; then
    echo "Yalc packages file doesn't exist. Creating..."
    cp "${yalc_packages_file_name}.example" ${yalc_packages_file_name}
  fi

  echo "Reading package list from ${yalc_packages_file_name}"

  while IFS== read -r path val; do
    package_path="$path"
    package_name="$(node -p -e "JSON.parse(process.argv[1]).name" "$(cat ${package_path}/package.json)")"
    is_enabled="$val"

    if [[ $package_name ]]; then
      if [[ $is_enabled == "true" ]]; then
        container_destination_path="${container_copied_packages_path}/${package_name}"

        echo "Copying `${package_path}` package code into container to ${container_destination_path}..."
        docker exec ${container_id} sh -c "mkdir -p ${container_destination_path}"
        docker cp "${package_path}/." "${container_id}:${container_destination_path}"

        # Then yalc link into this project
        echo "Linking package ${package_name} into API..."
        docker exec ${container_id} sh -c "cd ${container_destination_path} && yalc push"
        docker exec ${container_id} sh -c "cd /usr/local/src/app && yalc link ${package_name}"
      else
        echo "Unlinking package ${package_name} from API..."
        docker exec ${container_id} sh -c "cd /usr/local/src/app && yalc remove ${package_name}"
        docker exec ${container_id} sh -c "rm -rf /usr/local/src/app/node_modules/${package_name}"
      fi
    fi
  done < "$yalc_packages_file_name"

  echo "Updating packages..."
  docker exec ${container_id} sh -c "cd /usr/local/src/app && npm i"
  docker exec ${container_id} sh -c  "cd /usr/local/src/app && yalc update"
else
  container_destination_path="${container_copied_packages_path}/${package_name}"

  if [[ -z "${package_path}" ]]; then
    package_path="../api-plugins/${package_name_without_org}"
    full_package_path="$(cd ${package_path} && pwd)"
    echo "\nUsing local package path ${full_package_path}"
    echo "If this is not correct, specify the correct path as the second argument.\n"
  fi

  echo "Copying `${package_path}` package code into container..."
  docker-compose exec api sh -c "mkdir -p ${container_destination_path}"
  docker cp "${package_path}/." "${container_id}:${container_destination_path}"

  # Then yalc link into this project
  echo "Linking package into API..."
  docker-compose exec api sh -c "cd ${container_destination_path} && yalc push"
  docker-compose exec api sh -c "cd /usr/local/src/app && yalc link ${package_name}"
fi

# Fool nodemon into thinking something has changed so that it restarts.
# Touch first file found in /src with .js extension
echo "Restarting API..."
docker-compose exec api sh -c "touch -c $(ls ./src/*.js | head -n1)"
