#!/bin/bash
#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
THISDIR=$(cd $(dirname ${0}) && pwd)
WORKSPACE_DIR=$(cd ${THISDIR}/../.. && pwd)
WORKSPACE_NAME=$(basename ${WORKSPACE_DIR})

function die {
  echo 1>&2 "$0: ${1}"
  exit 1
}

function redis_port() {
  if [[ -z "${BUBBLE_REDIS_PORT}" ]] ; then
    # pick port based on hash of workspace name; there is a 1-in-4096 chance of collision
    # if you get a collision, just define BUBBLE_REDIS_PORT in jenkins to be < 6800 or > 10895 in the job's build config
    BUBBLE_REDIS_PORT=$(expr 6800 + $(echo $((16#$(echo -n ${WORKSPACE_NAME} | sha256sum | awk '{print $1}' | tail -c 4)))))
  fi
  echo ${BUBBLE_REDIS_PORT}
}

function start_redis() {
  port=$(redis_port)
  REDIS_DIR=/tmp/redis-${port}
  REDIS_CONF=${REDIS_DIR}/redis.conf

  echo "Creating redis dir: ${REDIS_DIR}"
  mkdir -p ${REDIS_DIR} || die "Error creating redis dir: ${REDIS_DIR}"

  echo "Writing redis config: ${REDIS_CONF}"
  cat ${THISDIR}/redis.conf.hbs | sed -e "s/{{BUBBLE_REDIS_PORT}}/${port}/g" > ${REDIS_CONF} || die "Error writing redis configuration to ${REDIS_CONF}"

  echo "Starting redis..."
  redis-server ${REDIS_CONF} || die "Error starting redis"
}

function stop_redis_by_dir() {
  REDIS_DIR=${1}
  PID_FILE=${REDIS_DIR}/redis.pid
  if [[ ! -f ${PID_FILE} ]] ; then
    echo "Redis pid file not found: ${PID_FILE}"
  else
    echo "Stopping redis on port ${port}"
    kill $(cat ${PID_FILE}) || die "Error killing redis using pid file: ${PID_FILE}"
  fi

  if [[ -d ${REDIS_DIR} ]] ; then
    echo "Removing redis dir: ${REDIS_DIR}"
    rm -rf ${REDIS_DIR} || die "Error removing redis dir: ${REDIS_DIR}"
  fi
}

function stop_redis() {
  port=$(redis_port)
  REDIS_DIR=/tmp/redis-${port}
  stop_redis_by_dir ${REDIS_DIR}
}

function redis_clean() {
  for REDIS_DIR in $(find /tmp -maxdepth 1 -type d -name "redis-*") ; do
    stop_redis_by_dir ${REDIS_DIR}
  done
}

if [[ -z ${1} ]] ; then
  die "expected one of: start stop clean port"
fi

case ${1} in
  "start")
    stop_redis
    start_redis
    echo "Redis successfully started"
    ;;
  "stop")
    stop_redis
    echo "Redis stopped and cleaned up"
    ;;
  "port")
    redis_port
    ;;
  "clean")
    redis_clean
    ;;
  *)
    die "invalid argument: ${1}"
    ;;
esac
