#!/bin/sh

SERVICE=cgrulesengd

cgconfigparser -l /etc/cgconfig.conf || true
if update-service --check "$SERVICE"; then
    true
else
    update-service --add "/etc/sv/$SERVICE" || true
fi
if [ x$(readlink "/etc/init.d/$SERVICE") != x"/usr/bin/sv" ]; then
    rm -fv "/etc/init.d/$SERVICE" || true
    ln -sv /usr/bin/sv "/etc/init.d/$SERVICE" || true
fi

ok_file_path="/etc/service/$SERVICE/supervise/ok"
printf "waiting for %s\n" "$ok_file_path"
timeout 6 sh -c 'while [ ! -e '"$ok_file_path"' ]; do sleep 1; done'
sv 2 "$SERVICE" || true # перечитать /etc/cgrules.conf
pgrep . | xargs -n 10 cgclassify 2>&1 | grep -v 'Error in determining euid/egid of pid [0-9]\+' || true
