#!/bin/bash

# This is a convenience script to help set up a Riak instance to which
# your Riak CS node is configured to connect. It does the following:
#
# 1. Modifies riak.conf and advanced.config (enables milti-backend,
#    with leveldb and bitcask for object data and manifests, resp.).
#
# 2. If Riak is installed on the same host as Riak CS, its ebin path
#    will be added to riak/advanced.config; otherwise, beams will be
#    rsync'ed to where Riak can load them (if the remote host is not
#    accessible, files will need to be copied in some other way).
#
# Relevant environment variables (with defaults):
# * RCS_CONFIG_DIR, "/etc/riak-cs".
# * RIAK_CONFIG_DIR, "/etc/riak".
# * RCS_LIB, "/usr/lib/riak-cs/lib" if it exists, else "/usr/lib64/riak-cs/lib".
# * RIAK_EBIN, "/usr/lib/riak/lib"
#
# For a devrel, assuming you are in the riak dir where you did `make
# devrel`, and have riak and riak_cs as sister dirs in .., you might
# use:
#
#   RCS_CONFIG_DIR=../riak_cs/dev/dev1/riak-cs/etc \
#   RIAK_CONFIG_DIR=dev/dev1/riak/etc \
#   RCS_LIB=../riak_cs/dev/dev1/riak-cs/lib/ \
#   RIAK_EBIN=dev/dev1/riak/lib/riak_kv-riak_kv-3.0.9+build.3962.ref1d4ee470/ebin \
#   ../riak_cs/misc/prepare-riak-for-cs

set -e

# 0. get riak host address
RCS_CONFIG_DIR=${RCS_CONFIG_DIR:-"/etc/riak-cs"}
RIAK_HOST=`sed -n 's/^riak_host = \(.*\):.*/\1/p' $RCS_CONFIG_DIR/riak-cs.conf`
RIAK_EBIN=${RIAK_EBIN:-/usr/lib/riak/lib}

# 1. Copy modules that are to be loaded on riak VM

if [ -d /usr/lib/riak-cs ]; then
    RCS_LIB_PKG_INST=/usr/lib/riak-cs/lib
elif [ -d /usr/lib64/riak-cs ]; then
    RCS_LIB_PKG_INST=/usr/lib64/riak-cs/lib
fi

RCS_LIB=${RCS_LIB:-$RCS_LIB_PKG_INST}

mods_to_add=`find -L $RCS_LIB \
   -name riak_cs_kv_multi_backend.beam \
-o -name riak_cs_riak_mapred.beam \
-o -name riak_cs_utils.beam \
-o -name rcs_common_manifest_utils.beam \
-o -name rcs_common_manifest_resolution.beam \
-o -name getopt.beam`

if [ $RIAK_HOST = "127.0.0.1" ]; then
    riak_dest=
else
    riak_dest=$RIAK_HOST:
fi

echo "* Copying CS modules to $riak_dest$RIAK_EBIN"
rsync $mods_to_add $riak_dest$RIAK_EBIN/


# 2. Modify riak.conf and advanced.config

RIAK_CONFIG_DIR=${RIAK_CONFIG_DIR:-"/etc/riak"}

echo "* Pulling riak.conf and advanced.config"

d=/tmp/$RIAK_HOST-riak-configs
mkdir -p $d
rsync $riak_dest$RIAK_CONFIG_DIR/* $d/

echo "* Modifying riak.conf"
sed -i \
    -e "s|storage_backend = bitcask|storage_backend = multi|" \
    $d/riak.conf

echo "
buckets.default.merge_strategy = 2" \
    >>$d/riak.conf

echo "* Modifying advanced.config"

sed -i -e \
    "s|]\\.|,\n\
    {riak_kv, [\n\
      $paths_option\
      {multi_backend,\n\
          [{be_default,riak_kv_eleveldb_backend,\n\
               [{max_open_files,20}]},\n\
           {be_blocks,riak_kv_bitcask_backend,\n\
               []}]},\n\
      {multi_backend_default,be_default},\n\
      {multi_backend_prefix_list,[{<<\"0b:\">>,be_blocks}]},\n\
      {storage_backend,riak_kv_multi_backend}\n\
     ]}\n\
   ].\n|" \
     $d/advanced.config

echo "* Pushing riak.conf and advanced.config"
rsync $d/* $riak_dest$RIAK_CONFIG_DIR

rm -rf $d
