#!/bin/bash
set -e

# Initialize variables used by Huginn at installation time

# Huginn is 12factor aware, embrace that fact for use inside of docker
ON_HEROKU=${ON_HEROKU:-true}

# Shallow clone the huginn project repo
git clone --depth 1 https://github.com/cantino/huginn /app

cd /app

# add a huginn group and user
adduser --group huginn
adduser --disabled-login --ingroup huginn --gecos 'Huginn' --no-create-home --home /app huginn
adduser huginn sudo
passwd -d huginn

# Change the ownership to huginn
chown -R huginn:huginn /app

# create required tmp and log directories
sudo -u huginn -H mkdir -p tmp/pids tmp/cache tmp/sockets log
chmod -R u+rwX log tmp

# install gems required by Huginn, use local cache if available
if [ -d "/scripts/cache" ]; then
  mv /scripts/cache vendor/
  chown -R huginn:huginn vendor/cache
fi
sudo -u huginn -H bundle install --deployment --without development test

# silence setlocale message (THANKS DEBIAN!)
cat > /etc/default/locale <<EOF
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
EOF