#!/bin/bash

# Also a good place to ensure any data folders
# that are *not* supposed to be replaced on every deployment exist
# and create a symlink to them from the latest deployment directory.

# The real 'data' folder is shared. It lives two levels up and one over
# (we're in a deployment dir, which is a subdir of 'deployments', which
# is a subdir of the project's main dir)

HERE=`pwd`
mkdir -p ../data
ln -s ../data $HERE/data

# We also have a shared uploads folder which is convenient to keep
# in a separate place so we don't have to have two express.static calls

mkdir -p ../uploads
ln -s ../uploads $HERE/public/uploads

# Install any dependencies that can't just be rsynced over with
# the deployment. Example: node apps have npm modules in a
# node_modules folder. These may contain compiled C++ code that
# won't work portably from one server to another.

# This script runs after the rsync, but before the 'stop' script,
# so your app is not down during the npm installation.

# Make sure node_modules exists so npm doesn't go searching
# up the filesystem tree
mkdir -p node_modules

# If there is no package.json file then we don't need npm install
if [ -f './package.json' ]; then
  # Install npm modules
  # Use a suitable version of Python
  # export PYTHON=/usr/bin/python26
  npm install
  if [ $? -ne 0 ]; then
    echo "Error during npm install!"
    exit 1
  fi
fi

node app apostrophe-migrations:migrate --safe
# Generate new static asset files for this
# deployment of the app without shutting down
node app apostrophe:generation
