#!/bin/bash

# Get script path to try to find Magento
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

# Try a few different locations
if [ -f "${SCRIPTPATH}/../app/etc/config.php" ]; then
    cd "${SCRIPTPATH}/.."
elif [ -f "${SCRIPTPATH}/../../app/etc/config.php" ]; then
    cd "${SCRIPTPATH}/../.."
elif [ -f "${SCRIPTPATH}/../../../app/etc/config.php" ]; then
    cd "${SCRIPTPATH}/../../.."
elif [ -f "${SCRIPTPATH}/../../../../app/etc/config.php" ]; then
    cd "${SCRIPTPATH}/../../../.."
elif [ -f "${SCRIPTPATH}/../../../../../app/etc/config.php" ]; then
    cd "${SCRIPTPATH}/../../../../.."
fi

# One more check for the right location
if [ ! -f "app/etc/env.php" ]; then
    echo "Cannot find Magento 2 install at $(pwd)"
    exit
fi

# Clean dirs and files to reset install
rm -rf var/cache var/page_cache var/log var/tmp var/view_preprocessed \
       var/*.lock var/*.log var/.*.lock var/*.flag var/.*.flag var/report \
       var/picklist var/bolt/cache var/autocomplete var/*.cookie
rm -rf pub/static/frontend pub/static/adminhtml pub/static/_cache pub/static/deployed_version.txt pub/error_log
rm -rf generated/code generated/metadata
rm -rf error_log error.log