#!/usr/bin/env zsh

s () {
    eval /Software/Sofin/bin/s ${@}
}

which psql >/dev/null
if [ "0" != "${?}" ]; then
    s i Postgresql13
fi
which cargo >/dev/null
if [ "0" != "${?}" ]; then
    s i Rust
fi
which diesel >/dev/null
if [ "0" != "${?}" ]; then
    cargo install diesel_cli --no-default-features --features "postgres"
    s export diesel Rust
fi
which patchelf >/dev/null
if [ "0" != "${?}" ]; then
    s i Patchelf
fi
which smartctl >/dev/null
if [ "0" != "${?}" ]; then
    s i Smartmontools
fi

s env +Postgresql13 +Patchelf +Rust +Smartmontools

igni dcollector stop
mv /Shared/Igniters/dcollector.igni /tmp/dcollector.igni

s up
s rm dcollector
s i dcollector
(
    cd /Services/Dcollector;
    diesel migration run;
)
mv /tmp/dcollector.igni /Shared/Igniters/dcollector.igni
igni dcollector start

s env reset
