#!/usr/bin/env bash

## Debug?
set -x

## Fail if any of the steps fails.
set -e

## We assume configure.out is sourced already.
if [ -z "${MONGOOSEIM_CONFIGURED}" ]; then
    echo "Is configure.out sourced?" > /dev/fd/2
    exit 1
fi

## We assume that current user will be the owner if not specified
if [ -z "${RUNNER_USER}" ]; then
    RUNNER_USER=`whoami`
fi

# A separate RUNNER_GROUP is handy for testing on a Mac,
# where users don't have a group of the same name by default.
if [ -z "${RUNNER_GROUP}" ]; then
    RUNNER_GROUP=${RUNNER_USER}
fi

if [ "${SYSTEM}" == "yes" ]; then
    ## we are good, sourcing configure.out has already set everything up
    :
else
    LIB_DIR=$PREFIX/mongooseim
fi

## Install!
INSTALL_OPTS="-o ${RUNNER_USER} -g ${RUNNER_GROUP}"
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${BIN_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -m 555 _build/prod/rel/mongooseim/bin/mongooseimctl ${BIN_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${ETC_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -m 664 _build/prod/rel/mongooseim/etc/app.config ${ETC_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -m 664 _build/prod/rel/mongooseim/etc/mongooseim.toml ${ETC_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -m 664 _build/prod/rel/mongooseim/etc/vm.args ${ETC_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -m 664 _build/prod/rel/mongooseim/etc/vm.dist.args ${ETC_DIR}
install $INSTALL_OPTS -d ${LIB_DIR}
cp -R _build/prod/rel/mongooseim/* ${LIB_DIR}
chown -R ${RUNNER_USER}:${RUNNER_GROUP} ${LIB_DIR}
[ x"$SYSTEM" == x"yes" ] && {
    mv ${LIB_DIR}/etc/app.config   ${LIB_DIR}/etc/app.config.example
    mv ${LIB_DIR}/etc/mongooseim.toml ${LIB_DIR}/etc/mongooseim.toml.example
    mv ${LIB_DIR}/etc/vm.args      ${LIB_DIR}/etc/vm.args.example
    mv ${LIB_DIR}/etc/vm.dist.args ${LIB_DIR}/etc/vm.dist.args.example
}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${LOG_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${MDB_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${LOCK_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${PID_DIR}
[ x"$SYSTEM" == x"yes" ] && install $INSTALL_OPTS -d ${STATUS_DIR}

## Do the bookkeeping.
## TODO: How to automate this? If done manually, there's always space for a cock-up.
LOG=install.log
echo > $LOG
log () {
    local file=$1
    echo $file >> $LOG
}
[ x"$SYSTEM" == x"yes" ] && log ${BIN_DIR}
[ x"$SYSTEM" == x"yes" ] && log ${BIN_DIR}/mongooseimctl
[ x"$SYSTEM" == x"yes" ] && log ${ETC_DIR}
[ x"$SYSTEM" == x"yes" ] && log ${ETC_DIR}/app.config
[ x"$SYSTEM" == x"yes" ] && log ${ETC_DIR}/mongooseim.toml
[ x"$SYSTEM" == x"yes" ] && log ${ETC_DIR}/vm.args
[ x"$SYSTEM" == x"yes" ] && log ${ETC_DIR}/vm.dist.args
find ${LIB_DIR} >> $LOG
[ x"$SYSTEM" == x"yes" ] && log ${LOG_DIR}
[ x"$SYSTEM" == x"yes" ] && log ${MDB_DIR}
[ x"$SYSTEM" == x"yes" ] && log ${LOCK_DIR}
[ x"$SYSTEM" == x"yes" ] && log ${PID_DIR}
[ x"$SYSTEM" == x"yes" ] && log ${STATUS_DIR}

## Voila!
echo MongooseIM successfully installed to ${LIB_DIR} >&2
