#!/bin/sh
set -e

echo "Deploying uat application ..."
    export NVM_DIR=~/.nvm
    source ~/.nvm/nvm.sh
    echo "Resetting uat_deploy branch to match uat..."
    # Checkout branch uat_deploy
    git fetch origin uat uat_deploy
    git checkout uat_deploy

    # Reset branch uat_deploy ke branch uat
    git reset --hard origin/uat
    
    # down laravel
    php artisan down
    # Install npm depedencies
    npm install
    # run npm 
    npm run dev
    # Update codebase
    git config --local user.email "action@github.com"
    git config --local user.name "GitHub Action"
    git checkout -B uat_deploy
    git add -f public/
    git commit -m "Build front-end assets"
    git push -f origin uat_deploy

    #composer install
    composer install
    # migrate db
    php artisan migrate --no-interaction
    # clear cache
    php artisan optimize:clear
    #up laravel
    php artisan up

echo "Application uat deployed!"