#!/bin/sh
NO_COLOR="\033[0m"
GREEN="\033[38;5;010m"
ORANGE="\033[38;5;214m"

printf "\n${GREEN}post merge hook start${NO_COLOR}\n"

# we check if vendor/bin/phpunit is present.
# If yes this means we are in dev mode.
PHPUNIT="vendor/bin/phpunit"

if [ -f ".NO_AUTO_COMPOSER_MIGRATE" ]; then
    printf "\n${ORANGE}composer and database migration skipped as per requested.${NO_COLOR}\n"
else
    if [ -x "$PHPUNIT" ]; then
        printf "\n${ORANGE}Dev mode detected${NO_COLOR}\n"
        echo "composer install"
        composer install
        if command -v npm > /dev/null; then
        	echo "npm install --no-audit --no-fund"
        	npm install --no-audit --no-fund
        fi
    else
        printf "\n${ORANGE}--no-dev mode detected${NO_COLOR}\n"
        echo "composer install --no-dev --prefer-dist"
        composer install --no-dev --prefer-dist
    fi
    if [ -f ".env" ]; then
        echo "php artisan optimize --clever --dont-confirm=assume-no"
        php artisan optimize --clever --dont-confirm=assume-no
        echo "php artisan migrate --force"
        php artisan migrate --force
    fi
fi

printf "\n${GREEN}post merge hook finish${NO_COLOR}\n"
