#!/bin/bash
if [[ -f /var/www/html/backend-box/dcu/storage/e.key ]]
then
    exit 0
fi

if [ "$APP_ENV" == "development" ]; then
    branch='DEV-MAIN'
else
    branch='master'
fi

BRANCH_FILE=/var/www/html/backend-box/dcu/storage/branch.key
if [ -f $BRANCH_FILE ]; then
    branch=$(head -n 1 $BRANCH_FILE)
fi

echo '0|Starting updating…' > /var/www/html/backend-box/dcu/storage/e.key

sleep 5
cd /var/www/html/backend-box

echo '10|Repository fetching…' > /var/www/html/backend-box/dcu/storage/e.key
sleep 3
#git checkout -b DEV-MAIN
#git reset --hard HEAD
#git pull origin DEV-MAIN
git checkout -B $branch HEAD
git reset --hard origin/$branch
git pull --force

echo '20|Repository fetching complete' > /var/www/html/backend-box/dcu/storage/e.key

sleep 2
cd dcu

echo '30|Apply migrations…' > /var/www/html/backend-box/dcu/storage/e.key
sleep 2
php artisan migrate
echo '42|Migrations complete' > /var/www/html/backend-box/dcu/storage/e.key

echo '45|Create symlinks…' > /var/www/html/backend-box/dcu/storage/e.key
sleep 2
php artisan storage:link
echo '65|Symlinks created' > /var/www/html/backend-box/dcu/storage/e.key

echo '74|Apply config…' > /var/www/html/backend-box/dcu/storage/e.key
sleep 3
php artisan config:clear
echo '85|Config apply complete' > /var/www/html/backend-box/dcu/storage/e.key

echo '90|Flush cache…' > /var/www/html/backend-box/dcu/storage/e.key
sleep 4
php artisan cache:clear
echo '95|Cache cleared' > /var/www/html/backend-box/dcu/storage/e.key

sleep 5
echo '100|Updating complete!' > /var/www/html/backend-box/dcu/storage/e.key
