<commit_before>#!/bin/bash

if [[ -z "$SETUP" ]]; then
    tput setab 1
    echo "Please do not run this script individually"
    tput sgr0
    exit 0
fi

CURL=`which curl`
NPM=`which npm`
GIT=`which git`
PHP=`which php`

ERROR_BKG=`tput setab 1` # background red
GREEN_BKG=`tput setab 2` # background green
BLUE_BKG=`tput setab 4` # background blue
YELLOW_BKG=`tput setab 3` # background yellow
MAGENTA_BKG=`tput setab 5` # background magenta

INFO_TEXT=`tput setaf 3` # yellow text
WHITE_TEXT=`tput setaf 7` # text white
BLACK_TEXT=`tput setaf 0` # text black
RED_TEXT=`tput setaf 1` # text red
NC=`tput sgr0` # reset

if [[ `echo "$@" | grep '\-v'` ]]; then
    VERBOSITY='-v'
fi

if [[ `echo "$@" | grep '\-vv'` ]]; then
    VERBOSITY='-vv'
fi

if [[ `echo "$@" | grep '\-vvv'` ]]; then
    VERBOSITY='-vvv'
fi

function labelText {
    echo -e "\n${BLUE_BKG}${WHITE_TEXT}-> ${1} ${NC}\n"
}

function errorText {
    echo -e "\n${ERROR_BKG}${WHITE_TEXT}=> ${1} <=${NC}\n"
}

function infoText {
    echo -e "\n${INFO_TEXT}=> ${1} <=${NC}\n"
}

function successText {
    echo -e "\n${GREEN_BKG}${BLACK_TEXT}=> ${1} <=${NC}\n"
}

function warningText {
    echo -e "\n${YELLOW_BKG}${RED_TEXT}=> ${1} <=${NC}\n"
}

function setupText {
    echo -e "\n${MAGENTA_BKG}${WHITE_TEXT}=> ${1} <=${NC}\n"
}

function writeErrorMessage {
    if [[ $? != 0 ]]; then
        errorText "${1}"
        errorText "Command unsuccessful"
        exit 1
    fi
}

function createDevelopmentDatabase {
    # postgres
    sudo createdb ${DATABASE_NAME}

    # mysql
    # mysql -u root -e "CREATE DATABASE DE_development_zed;"
}

function dumpDevelopmentDatabase {
    export PGPASSWORD=$DATABASE_PASSWORD
    export LC_ALL="en_US.UTF-8"

    if [[ -z "$1" ]]; then
          DATABASE_BACKUP_PATH=$DATABASE_NAME.backup;
    else
          DATABASE_BACKUP_PATH=$1
    fi

    pg_dump -i -h 127.0.0.1 -U $DATABASE_USER  -F c -b -v -f  $DATABASE_BACKUP_PATH $DATABASE_NAME
}

function restoreDevelopmentDatabase {
    read -r -p "Restore database ${DATABASE_NAME} ? [y/N] " response
    case $response in
        [yY][eE][sS]|[yY])
            dropAndRestoreDatabase
            ;;
        *)
            echo "Nothing done."
            ;;
    esac
}

function dropAndRestoreDatabase {
    if [[ -z "$1" ]]; then
          DATABASE_BACKUP_PATH=$DATABASE_NAME.backup;
    else
          DATABASE_BACKUP_PATH=$1
    fi

    export PGPASSWORD=$DATABASE_PASSWORD
    export LC_ALL="en_US.UTF-8"

    sudo pg_ctlcluster 9.4 main restart --force
    sudo dropdb $DATABASE_NAME
    sudo createdb $DATABASE_NAME
    pg_restore -i -h 127.0.0.1 -p 5432 -U $DATABASE_USER -d $DATABASE_NAME -v $DATABASE_BACKUP_PATH
}

function installDemoshop {
    labelText "Preparing to install Spryker Platform..."

    updateComposerBinary
    composerInstall

    installZed
    sleep 1
    installYves

    configureCodeception

    successText "Setup successful"
    infoText "\nYves URL: http://www.de.project.local/\nZed URL: http://zed.de.project.local/\n"
}

function installZed {
    setupText "Zed setup"

    setupSearch
    resetDataStores

    dropDevelopmentDatabase

    $CONSOLE setup:install $VERBOSITY
    writeErrorMessage "Setup install failed"

    labelText "Importing Demo data"
    $CONSOLE import:demo-data $VERBOSITY
    writeErrorMessage "DemoData import failed"

    labelText "Setting up data stores"
    $CONSOLE collector:search:export $VERBOSITY
    $CONSOLE collector:storage:export $VERBOSITY
    writeErrorMessage "DataStore setup failed"

    labelText "Setting up cronjobs"
    $CONSOLE setup:jenkins:generate $VERBOSITY
    writeErrorMessage "Cronjob setup failed"

    antelopeInstallZed

    labelText "Zed setup successful"
}

function installYves {
    setupText "Yves setup"

    antelopeInstallYves

    labelText "Yves setup successful"
}

function configureCodeception {
    labelText "Configuring test environment"
    vendor/bin/codecept build -q $VERBOSITY
    writeErrorMessage "Test configuration failed"
}

function optimizeRepo {
    labelText "Optimizing repository"
    git gc              # garbage collector
    git prune           # kills loose garbage
    writeErrorMessage "Repository optimization failed"
}

function setupSearch {
    labelText "Setup search"
    $CONSOLE setup:search
    writeErrorMessage "Setup search failed"
}

function resetDataStores {
    labelText "Flushing Elasticsearch"
    curl -XDELETE 'http://localhost:10005/de_search/'
    writeErrorMessage "Elasticsearch reset failed"

    labelText "Flushing Redis"
    redis-cli -p 10009 FLUSHALL<commit_msg>Reverts setting-up search during installation of demoshop<commit_after>#!/bin/bash

if [[ -z "$SETUP" ]]; then
    tput setab 1
    echo "Please do not run this script individually"
    tput sgr0
    exit 0
fi

CURL=`which curl`
NPM=`which npm`
GIT=`which git`
PHP=`which php`

ERROR_BKG=`tput setab 1` # background red
GREEN_BKG=`tput setab 2` # background green
BLUE_BKG=`tput setab 4` # background blue
YELLOW_BKG=`tput setab 3` # background yellow
MAGENTA_BKG=`tput setab 5` # background magenta

INFO_TEXT=`tput setaf 3` # yellow text
WHITE_TEXT=`tput setaf 7` # text white
BLACK_TEXT=`tput setaf 0` # text black
RED_TEXT=`tput setaf 1` # text red
NC=`tput sgr0` # reset

if [[ `echo "$@" | grep '\-v'` ]]; then
    VERBOSITY='-v'
fi

if [[ `echo "$@" | grep '\-vv'` ]]; then
    VERBOSITY='-vv'
fi

if [[ `echo "$@" | grep '\-vvv'` ]]; then
    VERBOSITY='-vvv'
fi

function labelText {
    echo -e "\n${BLUE_BKG}${WHITE_TEXT}-> ${1} ${NC}\n"
}

function errorText {
    echo -e "\n${ERROR_BKG}${WHITE_TEXT}=> ${1} <=${NC}\n"
}

function infoText {
    echo -e "\n${INFO_TEXT}=> ${1} <=${NC}\n"
}

function successText {
    echo -e "\n${GREEN_BKG}${BLACK_TEXT}=> ${1} <=${NC}\n"
}

function warningText {
    echo -e "\n${YELLOW_BKG}${RED_TEXT}=> ${1} <=${NC}\n"
}

function setupText {
    echo -e "\n${MAGENTA_BKG}${WHITE_TEXT}=> ${1} <=${NC}\n"
}

function writeErrorMessage {
    if [[ $? != 0 ]]; then
        errorText "${1}"
        errorText "Command unsuccessful"
        exit 1
    fi
}

function createDevelopmentDatabase {
    # postgres
    sudo createdb ${DATABASE_NAME}

    # mysql
    # mysql -u root -e "CREATE DATABASE DE_development_zed;"
}

function dumpDevelopmentDatabase {
    export PGPASSWORD=$DATABASE_PASSWORD
    export LC_ALL="en_US.UTF-8"

    if [[ -z "$1" ]]; then
          DATABASE_BACKUP_PATH=$DATABASE_NAME.backup;
    else
          DATABASE_BACKUP_PATH=$1
    fi

    pg_dump -i -h 127.0.0.1 -U $DATABASE_USER  -F c -b -v -f  $DATABASE_BACKUP_PATH $DATABASE_NAME
}

function restoreDevelopmentDatabase {
    read -r -p "Restore database ${DATABASE_NAME} ? [y/N] " response
    case $response in
        [yY][eE][sS]|[yY])
            dropAndRestoreDatabase
            ;;
        *)
            echo "Nothing done."
            ;;
    esac
}

function dropAndRestoreDatabase {
    if [[ -z "$1" ]]; then
          DATABASE_BACKUP_PATH=$DATABASE_NAME.backup;
    else
          DATABASE_BACKUP_PATH=$1
    fi

    export PGPASSWORD=$DATABASE_PASSWORD
    export LC_ALL="en_US.UTF-8"

    sudo pg_ctlcluster 9.4 main restart --force
    sudo dropdb $DATABASE_NAME
    sudo createdb $DATABASE_NAME
    pg_restore -i -h 127.0.0.1 -p 5432 -U $DATABASE_USER -d $DATABASE_NAME -v $DATABASE_BACKUP_PATH
}

function installDemoshop {
    labelText "Preparing to install Spryker Platform..."

    updateComposerBinary
    composerInstall

    installZed
    sleep 1
    installYves

    configureCodeception

    successText "Setup successful"
    infoText "\nYves URL: http://www.de.project.local/\nZed URL: http://zed.de.project.local/\n"
}

function installZed {
    setupText "Zed setup"

    resetDataStores

    dropDevelopmentDatabase

    $CONSOLE setup:install $VERBOSITY
    writeErrorMessage "Setup install failed"

    labelText "Importing Demo data"
    $CONSOLE import:demo-data $VERBOSITY
    writeErrorMessage "DemoData import failed"

    labelText "Setting up data stores"
    $CONSOLE collector:search:export $VERBOSITY
    $CONSOLE collector:storage:export $VERBOSITY
    writeErrorMessage "DataStore setup failed"

    labelText "Setting up cronjobs"
    $CONSOLE setup:jenkins:generate $VERBOSITY
    writeErrorMessage "Cronjob setup failed"

    antelopeInstallZed

    labelText "Zed setup successful"
}

function installYves {
    setupText "Yves setup"

    antelopeInstallYves

    labelText "Yves setup successful"
}

function configureCodeception {
    labelText "Configuring test environment"
    vendor/bin/codecept build -q $VERBOSITY
    writeErrorMessage "Test configuration failed"
}

function optimizeRepo {
    labelText "Optimizing repository"
    git gc              # garbage collector
    git prune           # kills loose garbage
    writeErrorMessage "Repository optimization failed"
}

function resetDataStores {
    labelText "Flushing Elasticsearch"
    curl -XDELETE 'http://localhost:10005/de_search/'
    writeErrorMessage "Elasticsearch reset failed"

    labelText "Flushing Redis"
    redis-cli -p 10009 FLUSHALL
{'fraction_non_alphanumeric': 0.11443332966912169, 'fraction_numerical': 0.012531603825436957, 'mean_word_length': 3.1714809720311785, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '87038', 'n_tokens_mistral': 3362, 'n_tokens_neox': 3114, 'n_words': 801}