#!/bin/bash

usage()
{
    echo -e "\e[33mUsage:\e[0m"
    echo -e "  deploy/run [options] [arguments]"

    echo ""

    echo -e "\e[33mOptions:\e[0m"
    echo -e "  \e[32m-h, --help            \e[39mDisplay this help message"
    echo -e "  \e[32m-d, --di              \e[39mGenerated code and dependency injection configuration (bin/magento setup:di:compile)"
    echo -e "  \e[32m-s, --static-content  \e[39mDeploy static content (bin/magento setup:static-content:deploy)"
    echo -e "  \e[32m-u, --upgrade         \e[39mUpdate modules database data and schema (bin/magento setup:upgrade)"
    echo -e "  \e[32m-a, --all             \e[39mRun all of the options"
    echo -e "  \e[32m-m, --modules         \e[39mIf you install \e[1m\e[4mnew extensions\e[0m then run this command with \e[90mVendor_Module1 Vendor_Module2\e[0m as argument"

    echo ""

    echo -e "\e[94mNote that the -all and --modules commands puts Magento 2 store into maintenance mode for a few seconds while setup:upgrade is running.\e[0m"

    echo ""

    exit 1
}

CURRENT_PATH="$PWD"
DEPLOY_DIR="$PWD/deploy"

if [ ! -f "$CURRENT_PATH/bin/magento" ]; then
    echo -e "\e[41mWrong path for executing\e[0m"

    exit 1
fi

if [ ! -f "$DEPLOY_DIR/app/env.php" ]; then
    echo -e "\e[41mPlease create the deploy/app/env.php file and make sure that cache and session are stored to a folder\e[0m"

    exit 1
fi

if [ ! -f "$DEPLOY_DIR/app/static-content-deploy.sh" ]; then
    echo -e "\e[41mPlease create the deploy/app/static-content-deploy.sh file and specify the static-content deploy commands\e[0m"

    exit 1
fi

if [ $# -eq 0 ]; then
    SETUP_ALL=1
fi


LAST_OPTION='';
SETUP_MODULES=''
while [ "$1" != "" ]; do
    case $1 in
        -a | --all )
            SETUP_ALL=1
            ;;
        -u | --upgrade )
            SETUP_UPGRADE=1
            ;;
        -d | --di )
            SETUP_DI=1
            ;;
        -s | --static-content )
            SETUP_STATIC_CONTENT=1
            ;;
        -m | --modules )
            #do nothing here
            ;;
        -h | --help )
            usage
            exit
            ;;
        * )
            if [ "$LAST_OPTION" != -m ] && [ "$LAST_OPTION" != --modules ]; then
                usage
                exit 1
            else
                SETUP_MODULES="$SETUP_MODULES $1"
                shift
                continue
            fi
    esac

    LAST_OPTION=$1
    shift
done

if [ "$SETUP_MODULES" ]; then
    SETUP_ALL=1
fi

echo -e "\e[42mDeploy script started\e[0m"

cd $DEPLOY_DIR
INSTANCE="$PWD/instance"

echo -e "\e[33m- Remove old files\e[0m"

rm -rf $INSTANCE
echo -e "\e[33m- Create new instance folders and files\e[0m"
mkdir -p $INSTANCE;
cd $INSTANCE

mkdir -p generated;
mkdir -p pub;
mkdir -p pub/static;
mkdir -p var;

cp -rf $CURRENT_PATH/app app
cp -rf $CURRENT_PATH/bin bin
cp -rf $CURRENT_PATH/setup setup
cp -rf $CURRENT_PATH/vendor vendor

# temporary copy env.php file with save cache local
rm app/etc/env.php
cp -rf $DEPLOY_DIR/app/env.php app/etc/env.php


ln -sfn $CURRENT_PATH/deploy deploy
ln -sfn $CURRENT_PATH/dev dev
ln -sfn $CURRENT_PATH/lib lib
ln -sfn $CURRENT_PATH/phpserver phpserver


ln -sfn $CURRENT_PATH/update update
ln -sfn $CURRENT_PATH/composer.json composer.json
ln -sfn $CURRENT_PATH/composer.lock composer.lock


if [ "$SETUP_MODULES" ]; then
    echo -e "\e[33m- Enabling modules\e[0m"
    $INSTANCE/bin/magento module:enable $SETUP_MODULES
fi

echo -e "\e[33mStart deploying\e[0m"

if [ $SETUP_ALL ] || [ $SETUP_DI ]; then
    $INSTANCE/bin/magento setup:di:compile
fi

if [ $SETUP_ALL ] || [ $SETUP_STATIC_CONTENT ]; then
    . "$DEPLOY_DIR/app/static-content-deploy.sh"
fi


if [ $SETUP_ALL ] || [ $SETUP_UPGRADE ]; then
    $CURRENT_PATH/bin/magento maintenance:enable
    $CURRENT_PATH/bin/magento setup:upgrade
fi


if [ $SETUP_ALL ]; then
    GENERATED_FOLDERS=( "generated" "pub/static/adminhtml" "pub/static/frontend" "pub/static/deployed_version.txt" "var/view_preprocessed" )
else
    GENERATED_FOLDERS=()
    if [ $SETUP_DI ]; then
        GENERATED_FOLDERS+=( "generated")
    fi
    if [ $SETUP_STATIC_CONTENT ]; then
        GENERATED_FOLDERS+=( "pub/static/adminhtml" "pub/static/frontend" "pub/static/deployed_version.txt" "var/view_preprocessed" )
    fi
fi


echo -e "\e[33m- Copy new files into Magento\e[0m"
for i in "${GENERATED_FOLDERS[@]}"
do
   :
   cp -rf $i "$CURRENT_PATH/$i-deploy"
done

echo -e "\e[33m- Replace files in Magento\e[0m"
for i in "${GENERATED_FOLDERS[@]}"
do
   :
   if [ -d "$CURRENT_PATH/$i" ]; then
      mv "$CURRENT_PATH/$i" "$CURRENT_PATH/$i-old-deploy"
   fi
   mv "$CURRENT_PATH/$i-deploy" "$CURRENT_PATH/$i"
done

if [ -d "$CURRENT_PATH/pub/static/_cache" ]; then
  echo -e "\e[33m- Remove magento pub/static/_cache\e[0m"
  rm -rf "$CURRENT_PATH/pub/static/_cache"
fi

echo -e "\e[33m- Replace old Magento files\e[0m"
for i in "${GENERATED_FOLDERS[@]}"
do
   :
   rm -rf "$CURRENT_PATH/$i-old-deploy"
done

if [ $SETUP_ALL ] || [ $SETUP_UPGRADE ]; then
    $CURRENT_PATH/bin/magento maintenance:disable
fi

echo -e "\e[33m- Flushing the cache\e[0m"
$CURRENT_PATH/bin/magento cache:flush

echo -e "\e[33m- Enabling the cache\e[0m"
$CURRENT_PATH/bin/magento cache:enable

echo -e "\e[42mDeploy script finished\e[0m"
