#!/bin/bash

enable_nginx () {
    
    # Preserve environment variables
    # https://github.com/phusion/passenger-docker#setting-environment-variables-in-nginx
    if [ ! -f /etc/nginx/main.d/app-env.conf ]; then
        echo "Setting environment variables for nginx..."
        cp /home/app/rails_app/docker/services/nginx/main.d/app-env.conf /etc/nginx/main.d/
        
    fi
    
    # Enable Nginx
    # https://github.com/phusion/passenger-docker#using-nginx-and-passenger
    if [ ! -f /etc/nginx/sites-enabled/app.conf ]; then
        echo "Enabling nginx..."
        rm -f /etc/service/nginx/down
        rm /etc/nginx/sites-enabled/default
        cp /home/app/rails_app/docker/services/nginx/sites-enabled/app.conf /etc/nginx/sites-enabled/
    fi
}

install_ruby_gems () {
    # Bundle install only if dependencies are not satisfied
    echo "Installing ruby gems..."
    bundle check || bundle install --system --jobs 4 --retry 6
}

precompile_assets () {
    echo "Precompiling Rails assets..."
    cd ${APP_HOME} && bundle exec rake assets:precompile
}
################################################
# PRODUCTION ENVIRONMENT
################################################
if [ "$ENV" = "production" ]
then
    
    if [ "$SERVICE" = "app" ]
    then
        # Do something
        echo "*** Production mode running - Init app..."
        #install_ruby_gems
        #precompile_assets
        enable_nginx
        
    else
        echo "Unknown service"
    fi
    
    ################################################
    # DEVELOPMENT ENVIRONMENT
    ################################################
elif [ "$ENV" = "development" ]
then
    
    if [ "$SERVICE" = "app" ]
    then
        # Do something
        echo "Development mode running - Init app..."
        # Must be called before install gems
        install_ruby_gems
        #precompile_assets
        enable_nginx
        #setup_backups
        
        # Ok
        echo "################################"
        echo "#                              #"
        echo "#    APP CONTAINER IS READY    #"
        echo "#                              #"
        echo "################################"
        echo "User: $USER"
        echo "Home: $HOME"
    else
        echo "Unknown service"
    fi
    
else
    # Do something
    echo "No ENV variable set, you must add it..."
fi
