#!/bin/bash

/usr/sbin/nginx

mkdir -p /var/lib/shellinabox
chmod a+rwX /var/lib/shellinabox
/usr/bin/shellinaboxd --debug --no-beep --disable-peer-check \
  -t \
  -b \
  -c /var/lib/shellinabox \
  -s "/:root:root:/home/user:/bin/bash"

if [[ ! -d /home/user/logs/nginx ]]; then
  mkdir -p /home/user/logs/nginx
fi
if [[ ! -L /home/user/logs/nginx/access.log ]]; then
  ln -sf /var/log/nginx/access.log /home/user/logs/nginx/access.log
fi

if [[ "${CHAT_BASE}" != "" ]]; then
  echo "Setting ${CHAT_BASE} in client/public/tail-pre-html"
  sed -i -E "s#/ketr.chat#${CHAT_BASE}#g" client/public/tail-pre.html
fi

if [[ -z "${DEVELOPMENT}" ]]; then
  echo "Running in PRODUCTION mode."
  cd /home/user/server
  { while true; do npm start ; sleep 3 ; done ; }
else
  echo "Running in DEVELOPMENT mode."
  cd /home/user/server
  npm install
  { while true; do npm start ; sleep 3 ; done ; } &
  cd /home/user/client
  npm install
  { while true; do npm start ; sleep 3 ; done ; }
fi
