#!/bin/sh

# prerm

# stop osqueryd service
if which systemctl >/dev/null 2>&1; then
    systemctl disable osqueryd-cleanup.timer
    systemctl stop osqueryd-cleanup.timer
    # systemctl disable osqueryd
    systemctl stop osqueryd.service
    rm "/etc/systemd/system/osqueryd.service.d/20-osquery-yandex-generic.conf"
    rmdir --ignore-fail-on-non-empty "/etc/systemd/system/osqueryd.service.d/"
elif which service >/dev/null 2>&1; then
    service osqueryd stop
elif which invoke-rc.d >/dev/null 2>&1; then
    invoke-rc.d osqueryd stop
else
    /etc/init.d/osqueryd stop
fi

kill "$(cat /var/run/osquery.pidfile)" > /dev/null 2>&1;

# erase database and other meta information
# database size over 500mb will drastically affect perfomance
miscdir='/var/osquery/*'
logdir='/var/log/osquery/*'
# confdir='/etc/osquery/'
dbdir='/usr/share/osquery/osquery.dbq/'
rm -rf $miscdir
rm -rf $logdir
rm -rf $dbdir

# delete links to custom decorators 
find /etc/osquery/osquery.conf.d -type l -delete

exit 0