#!/bin/bash -e
#adduser --system --no-create-home --home /data --uid 1000  www-user
#sed -i "s/APACHE_RUN_USER=www-data/APACHE_RUN_USER=www-user/" /etc/apache2/envvars
sed -i "s#data_directory = '.*'#data_directory = '/data/run/postgres'#" /etc/postgresql/10/main/postgresql.conf

/etc/init.d/postgresql restart

/etc/init.d/apache2 start
tail -f /var/log/apache2/error.log &

export OER_SRC_DIR=/data/oer
export PLATFORM_PATH=/data/src

V="/data/run/virtualenv"
if [[ ! -e $V ]] ; then
  sudo -u postgres psql -c "CREATE USER hoou WITH PASSWORD 'hoohoohoo123';"
  sudo -u postgres psql -c "CREATE DATABASE hoou;"
  sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE hoou to hoou;"

  # psql -h localhost -U hoou hoou # should work

  mkdir -p $V
  virtualenv -p python3 $V
  cd $V
  source $V/bin/activate
  pip3 install -U -r  /data/dev/requirements.txt
  pip3 install --ignore-installed python-social-auth
  python3 ./manage.py migrate

  # prepare docker image for worker
  docker build -t kunkel/oer-worker /data/dev/worker
fi
source $V/bin/activate

/etc/init.d/docker start

/bin/bash
