#!/bin/sh

# This script sets the priority of processes from specific applications.
# (muzso, 2012.01.21)

# collect PID, priority and command for all processes
psout="$(ps -A -o pid=,nice=,comm=)"

# filter for PIDs of all processes from a given app that are running with
# the default (zero) priority
pids="$(echo "$psout" | sed -e 's/^[[:space:]]*\([0-9]\{1,\}\)[[:space:]]\{1,\}0[[:space:]]\{1,\}\/Applications\/uTorrent\.app\/.*/\1/;t' -e 'd')"
# set nice value of these processes
[ -n "$pids" ] && echo "$pids" | xargs renice 19

# run only if root (since only root can set a negative priority for a process)
if [ $EUID -eq 0 ]; then
  pids="$(echo "$psout" | sed -e 's/^[[:space:]]*\([0-9]\{1,\}\)[[:space:]]\{1,\}0[[:space:]]\{1,\}\/Applications\/MPlayer OSX Extended\.app\/.*/\1/;t' -e 'd')"
  [ -n "$pids" ] && echo "$pids" | xargs renice -10
fi

exit 0


