#!/bin/sh

set -eu

# standard usage
# run_command("CPU INFO", 10, "toolbox", "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
# run_command("PROCESSES", 10, "toolbox", "ps", "-P", NULL);
# expected columns: USER PID PPID VSIZE RSS PCY WCHAN PC NAME
# run_command("PROCESSES AND THREADS", 10, "toolbox", "ps", "-t", "-p", "-P", NULL)
# expected columns: USER PID PPID VSIZE RSS PRIO NICE RTPRI SCHED PCY WCHAN PC NAME

# From unit tests test/eurtest/tools/content_shell/content_shell_manager.py:415
# self._device.Cmd('toolbox ps cast_shell | tail -n +2 | wc -l')
# ps_count = int(self._device.GetCmdOutput())
# return ps_count == 0

cmd=$1
numarg=$#
if [ "$cmd" = "ps" ]; then
	if [ $numarg -eq 2 -a ${2:-x} = "-P" ]; then
		ps -eo user,pid,ppid,vsize,rss,state,wchan:20,comm
	elif [ $numarg -eq 4 -a ${2:-x} = "-t" -a ${3:-x} = "-p" -a ${4:-x} = "-P" ]; then
		ps -e H o user,pid,ppid,vsize,rss,nice,rtprio,state,wchan:20,comm
	elif [ $numarg -eq 2 ]; then
		ps u -C "$2"
	else
		ps aux
	fi
elif [ "$cmd" = "top" ]; then
	if [ $numarg -eq 8 -a ${2:-x} = "-n" -a ${4:-x} = "-d" -a ${6:-x} = "-m" -a ${8:-x} = "-t" ]; then
		TERM=dumb top -n "$3" -d "$5" -H
	else
		TERM=dumb "$@"
	fi
else
	"$@"
fi

