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

echo '0|Starting updating…' > /var/www/html/backend/dcu/storage/e.key
sleep 5
cd /var/www/html/backend;

echo '8|Checkout credentials…' > /var/www/html/backend/dcu/storage/e.key
git_username=$(printf 'host=gitlab.com' | git credential-store get | grep '^username' | cut -d '=' -f2);
echo $git_username > /var/www/html/backend/dcu/storage/git.key
sleep 1

echo '10|Repository fetching…' > /var/www/html/backend/dcu/storage/e.key
sleep 3
    git reset --hard HEAD;
    git pull;
echo '20|Repository fetching complete' > /var/www/html/backend/dcu/storage/e.key
sleep 1
cd dcu;

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

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

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

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

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