#!/bin/bash

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# Replace "username" by your username
ME="mvoformazs"

echo ' '
echo "#######################################################"
echo "##################### PROD DEPLOY #####################"
echo "#######################################################"
echo ' '

# Go to site directory
echo "GOING TO /home/${ME}/www/prod"
cd /home/${ME}/www/prod
echo "*******************************************************"
echo ' '

# Shutdown the laravel app
echo "SHUTTING DOWN THE LARAVEL APP"
php artisan down
echo "*******************************************************"
echo ' '

# Pull changes from git dir
echo "PULLING CHANGES FROM GIT DIR"
unset GIT_DIR
git fetch origin master
git reset --hard FETCH_HEAD
echo "*******************************************************"
echo ' '

# Install new composer packages
echo "INSTALLING NEW COMPOSER PACKAGES"
php ~/composer.phar install --no-dev --prefer-dist --optimize-autoloader
echo "*******************************************************"
echo ' '

# Migrate database
# (If you don't use "--force" option,
# laravel will prompt you each time for confirmation)
echo "MIGRATING DATABASE"
php artisan migrate --force
echo "*******************************************************"
echo ' '

# Clear caches
echo "CLEARING CACHE"
php artisan cache:clear
echo "*******************************************************"
echo ' '

# Clear expired password reset tokens
echo "CLEARING EXPIRED PASSWORD RESET TOKENS"
php artisan auth:clear-resets
echo "*******************************************************"
echo ' '

# Clear and cache routes
echo "CLEARING AND CACHE ROUTES"
php artisan route:cache
echo "*******************************************************"
echo ' '

# Clear and cache config
echo "CLEARING AND CACHE CONFIG"
php artisan config:cache
echo "*******************************************************"
echo ' '

# Go live
echo "GOING LIVE!"
php artisan up
echo ' '
echo "#######################################################"
echo "###################### DEPLOYED! ######################"
echo "#######################################################"
echo ' '