/ devlog / devscripts / check_env.sh
#!/bin/sh
# Says whether this sandbox holds everything a work clone needs, and
# names what is absent.
#
#   sh devlog/devscripts/check_env.sh
#
# WHY THIS EXISTS. Two things a session needs were absent from a fresh
# sandbox and neither said so plainly. PHP was simply not there, which at
# least stopped the first command run. The word list check_docwords.sh
# reads was also not there, and that one is worse: without it the check
# reports ordinary English words as unknown, so a file with one real
# fault printed ninety-one, and the ninety that were not real hid the one
# that was. A missing piece that changes what a check says, rather than
# stopping it, is the kind this is meant to catch.
#
# Run it at the start of a session, before the first patch is cut. It
# exits non-zero where something a check depends on is absent, and prints
# the command that puts it there.
fail=0
report()
{
    if [ "$2" = "yes" ]; then
        echo "check_env: PASS $1"
    else
        echo "check_env: FAIL $1"
        echo "    $3"
        fail=1
    fi
}
# PHP itself, and new enough for what the source uses.
if command -v php > /dev/null 2>&1; then
    version=`php -r 'echo PHP_VERSION;'`
    major=`php -r 'echo PHP_MAJOR_VERSION;'`
    minor=`php -r 'echo PHP_MINOR_VERSION;'`
    if [ "$major" -gt 8 ] || { [ "$major" -eq 8 ] && [ "$minor" -ge 1 ]; }
    then
        report "php $version" yes
    else
        report "php $version is older than 8.1" no \
            "apt-get install -y php-cli"
    fi
else
    report "php is not installed" no \
        "apt-get install -y php-cli php-sqlite3 php-mbstring php-curl php-gd"
fi
# The extensions Yioop reads a database and a page with.
for one in sqlite3 mbstring curl gd
do
    if php -m 2>/dev/null | grep -qi "^$one\$"; then
        report "php extension $one" yes
    else
        report "php extension $one" no "apt-get install -y php-$one"
    fi
done
# The word list check_docwords.sh reads. Without it that check calls
# every ordinary word unknown, which buries the faults that are real.
if python3 -c "from english_words import get_english_words_set" \
    > /dev/null 2>&1
then
    report "the english-words word list" yes
else
    report "the english-words word list" no \
        "pip install english-words --break-system-packages"
fi
# A work directory Yioop has been pointed at. Without one Config.php
# stops short of its later constants and Createdb dies on one that is
# not defined, which reads like a fault in the patch and is not.
if php -r 'require "src/configs/Config.php";
    exit(seekquarry\yioop\configs\PROFILE ? 0 : 1);' > /dev/null 2>&1
then
    report "a configured work directory" yes
else
    report "a configured work directory" no \
        "php src/configs/ConfigureTool.php work-dir <path>"
fi
# The two SQLite databases a page is drawn from.
if php -r 'require "src/configs/Config.php";
    echo seekquarry\yioop\configs\WORK_DIRECTORY;' > /tmp/work_dir.$$ \
    2>/dev/null
then
    work_dir=`cat /tmp/work_dir.$$`
    rm -f /tmp/work_dir.$$
    if [ -f "$work_dir/data/public_default.db" ] && \
        [ -f "$work_dir/data/private_default.db" ]; then
        report "public_default.db and private_default.db" yes
    else
        report "public_default.db and private_default.db" no \
            "php src/configs/Createdb.php"
    fi
fi
# A browser to drive, for anything that has to be measured on a page
# rather than reasoned about.
found=""
for one in "$HOME/.cache/puppeteer" /opt/pw-browsers
do
    if [ -d "$one" ]; then
        one_found=`find "$one" -name chrome -type f 2>/dev/null | head -1`
        if [ -n "$one_found" ]; then
            found="$one_found"
            break
        fi
    fi
done
if [ -n "$found" ]; then
    report "a chrome to drive at $found" yes
else
    report "a chrome to drive" no \
        "look under ~/.cache/puppeteer and /opt/pw-browsers"
fi
# npm looks up from where it runs, so the package may sit beside the
# clone or in the home folder above it.
if [ -d "$HOME/node_modules/puppeteer-core" ] || \
    [ -d "node_modules/puppeteer-core" ] || \
    node -e "require.resolve('puppeteer-core')" > /dev/null 2>&1; then
    report "puppeteer-core" yes
else
    report "puppeteer-core" no "npm install puppeteer-core"
fi
if [ "$fail" = "0" ]; then
    echo "check_env: everything a work clone needs is here"
fi
exit $fail
X