#!/bin/sh
# Calm a process down
# NEEDS_WORK: cleanup
#  calm <pid> == only one process
#  calm <bin> == all processes that match query
#  calm * == if used in proc or a folder with just PID files, if not you will get an error
#  calm <pid> <bin> .... OK
# set's NICE value to 0
# need sudo

DAISY_INTERNAL=1
. $(dirname $(realpath $0))/daisy.source

PIDS=$@

errorFn()
{
  echo calm: Invalid operation or no such PID/process \(\"$(echo "$PIDS" | tr '\n' ' ' | cut -c -20)...\"\)
  exit
}

for pid in $PIDS
do
  # Process to PID in elegant way
  NEWPIDS=$(pidof "$pid")
  if [ "$NEWPIDS" ]
  then
    BINNY=$pid
    pid=$NEWPIDS
  else
    NEWBINS=$pid
    BINNY=$(ps -p "$pid" -o comm= 2>/dev/null )
    if [ $? != 0 ]
    then
      errorFn
    fi
  fi

  echo Calming down $pid \("$BINNY"\)...
  sudo renice -n 0 -p $pid;
done
