#!/bin/bash

FEATURE=$1

if [ "$#" -gt 0 ]; then

        BASE_PATH=/var/www/html/omnichannel

        echo "--- SCRIPT DE ATUALIZACAO DA APP ---"
        echo $HOSTNAME

        echo "--- ATUALIZANDO API ---"

        cd $BASE_PATH
        git checkout -- .
        git clean -f -d
        git checkout $FEATURE
        git pull

        echo "--- LIMPANDO CACHE ---"
        rm -rf api/bootstrap/cache/*
        composer dump-autoload
        php artisan config:clear
        php artisan route:clear
        php artisan cache:clear
        php artisan view:clear
        systemctl restart apache2

        echo "--- AJUSTE PARA AMBIENTE DE DEV ---"
        sed -i 's/<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">/<!--&-->/g' $BASE_PATH/resources/views/layouts/head.blade.php
        sed -i 's/\(.*App\\Http\\Middleware\\EncryptCookies::class.*\)/\/\/\1/g' $BASE_PATH/app/Http/Kernel.php
        sed -i '15,42 s/^/\/\//' $BASE_PATH/app/Http/Middleware/VerifyUrl.php

        echo "--- ATUALIZACAO CONCLUIDA ---"
else
        echo "Nenhum parâmetro passado, favor informar a feature, exemplo: feature/job_x."
fi