#!/bin/bash -l
echo "*******************************************"
echo "*** Running Pixpalace entrypoint script ***"
echo "*******************************************"
# Keep bundle between container restarts (without rebuild):
#export BUNDLE_PATH=/home/pixpalace/.bundle
#export BUNDLE_APP_CONFIG=/home/pixpalace/.bundle
export BUNDLE_PATH=/home/pixpalace/.rvm/gems/ruby-2.1.9@pixpalace
export BUNDLE_APP_CONFIG=/home/pixpalace/.rvm/gems/ruby-2.1.9@pixpalace

cp ./docker/tasks/docker.tsdelta.tasks.rb \
${BUNDLE_PATH}/gems/ts-delayed-delta-2.0.2/lib/thinking_sphinx/deltas/delayed_delta/tasks.rb

cd /home/pixpalace/rails_app
# --

echo "SETUP: Copy the FTP and Processor config"
cp ./docker/config/docker.processor.yml.dist \
./tools/media_processor/config/processor.yml
cp ./docker/config/docker.ftp.yml.dist \
./tools/file_processor/config/ftp.yml
echo "SETUP OK: config files copied succesffuly"

bundle install
echo "***************************"
echo "*** Bundle install done ***"
echo "***************************"
echo

sudo service rabbitmq-server start
echo "********************************"
echo "*** Rabbit MQ server started ***"
echo "********************************"
echo

bundle exec rake ts:index
echo "******************************"
echo "*** Sphinx Index generated ***"
echo "******************************"
echo

bundle exec rake ts:restart
echo "******************************"
echo "*** Sphinx searchd started ***"
echo "******************************"
echo

./tools/media_processor/bin/media_processor start
echo "********************************"
echo "*** Media Processor started  ***"
echo "********************************"
echo
echo "--> ..................<--"
echo "-->        INFO       <--"
echo "--> ..................<--"
echo "INFO: At first you may need to download some images"
echo
echo "1:--> Host: Connect to the Rails container"
echo "docker exec -ti pixpalace_cs_pixpalace_rails_1 /bin/bash -l"
echo
echo "2:--> Container: Start the file processor"
echo "./tools/file_processor/bin/file_processor start"
echo
echo "3:-->/ Host: Open a new Terminal and check the log directory"
echo "tail -f log/file_processor.log"
echo
echo "4:--> Container: Stop the file processor once you have a few images"
echo "WARNING: if not stopped it may fill up your filesystem"
echo "./tools/file_processor/bin/file_processor stop"
echo
echo "5:--> Container: Run Indexer"
echo "rake ts:index"
echo
echo "Then you should see new images..."
echo
echo "--> ****************************"
echo "--> Go to http://localhost:3000 "
echo "--> ****************************"
echo
# Start Apache Passenger - Use restart in case of stale files/pids
sudo apachectl restart

echo
echo "--> ****************************"
echo "-->  Pixpalace App Rails Logs **"
echo "--> ****************************"
tail -f log/development.log

