#!/bin/bash

if [ -z ${ONCE} ]; then ONCE=0; else echo "ONCE is set to $ONCE"; fi

if [ ! -d node_modules ]; then
    if [[ $(rpm -q nodejs | grep -o 'is not installed') != '' ]]; then
        curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
        yum install -y nodejs
    fi
    if [[ $(rpm -q yarn | grep -o 'is not installed') != '' ]]; then
        curl --silent -o /etc/yum.repos.d/yarn.repo https://dl.yarnpkg.com/rpm/yarn.repo
        yum install -y yarn
    fi
    yarn install
fi

if [ -f /usr/local/cpanel/whostmgr/docroot/cgi/CloudLinux.cgi ]; then
    ./scripts/prepare_protractor_env.sh #turn off repairing of source
    ./scripts/time.sh restore
    if [[ ONCE -eq 1 ]]; then
        yarn run build:cpanel:onmachine
    else
        yarn run watch:cpanel:onmachine
    fi
elif [ -d /usr/local/psa/admin/plib/modules/plesk-lvemanager ]; then
    if [[ ONCE -eq 1 ]]; then
        yarn run build:plesk:onmachine
    else
        yarn run watch:plesk:onmachine
    fi
elif [ -d /usr/local/directadmin/plugins/lvemanager_spa ]; then
    if [[ ONCE -eq 1 ]]; then
        yarn run build:da:onmachine
    else
        yarn run watch:da:onmachine
    fi
else
    if [[ ONCE -eq 1 ]]; then
        yarn run build
    else
        yarn run watch
    fi
fi
