#!/bin/zsh
set -eu
function SystemProfilerProperty()
{
local result=$2
local local_result=$(system_profiler $3| grep -i $1 |\
cut -d ":" -f 2 | awk '{$1=$1};1')
eval $result="'$local_result'"
}
function GetPowerProperty()
{
SystemProfilerProperty $1 $2 "SPPowerDataType"
}
function GetDisplayProperty()
{
SystemProfilerProperty $1 $2 "SPDisplaysDataType"
}
function CompareValue()
{
if [ "$1" != "$2" ]; then
echo $3
exit 127
fi
}
CheckPowerValue()
{
GetPowerProperty $1 VALUE
VALUE=$(echo $VALUE|tr -d '\n')
CompareValue $VALUE $2 $3
}
CheckDisplayValue()
{
GetDisplayProperty $1 VALUE
VALUE=$(echo $VALUE|tr -d '\n')
CompareValue $VALUE $2 $3
}
function CheckProgramNotRunning(){
if pgrep -x "$1" > /dev/null; then
echo "$2"
exit 127
fi
}
function CheckEnv()
{
CheckPowerValue "charging" "NoNo" "Laptop cannot be charging during test."
CheckPowerValue "connected" "No" "Charger cannot be connected during test."
CheckDisplayValue "Automatically adjust brightness" "No"\
"Disable automatic brightness adjustments and unplug external monitors"
CheckProgramNotRunning "Terminal" "Do not have a terminal opened. Use SSH.";
CheckProgramNotRunning "iTerm2" "Do not have a terminal opened. Use SSH.";
}
CheckEnv