#!/bin/bash

input=".env"
while IFS= read -r line
do
    if [ "APP_ENV" == "${line%=*}" ]; then
        env="${line##*=}"
    elif [ "DB_DATABASE" == "${line%=*}" ]; then
        database="${line##*=}"
    elif [ "DB_USERNAME" == "${line%=*}" ]; then
        user="${line##*=}"
    elif [ "DB_PASSWORD" == "${line%=*}" ]; then
        pass="${line##*=}"
    fi
done < "$input"

year=$(date +"%Y")
month=$(date +"%m")
day=$(date +"%d")
hour=$(date +"%H")
minutes=$(date +"%M")

if [ -d "storage/logs/database" ];
then
    echo "storage/logs/database/ exist"
else
    mkdir database
fi

mysqldump --user="$user" --password="$pass" -B "$database" > storage/logs/database/save-"$database"-"$year""$month""$day"-"$hour""$minutes".sql

if [ "production" == "$env" ];
then
    echo "deploy in production"
    git pull origin production

    composer install
    php artisan migrate --force
    php artisan optimize

    npm install
    npm run prod
fi
