#!/bin/sh

# Do NOT error on failed commands
set +e

# Don't error (or do anything) for no matching globs.
if [ -n "${ZSH_VERSION-}" ]; then
  setopt nullglob
fi


# Error if the output is a terminal
if [ -t 1 ]; then
  printf 'This script must be redirected to a file, or special characters will be lost
  Ex: %s > external-tool-results.txt\nAttach the file in a GitHub issue comment.\n' "$0"
  exit 2
fi

# Export needed variables
export LC_ALL=C

# Print OS info
printf -- '---Uname:\n'
uname -a
printf -- '---lpb_release:\n'
lsb_release -a 2>/dev/null || printf '<none>\n'
printf -- '---/etc/*release:\n'
cat /etc/*release 2>/dev/null </dev/null || printf '<none>\n'
printf -- '---/etc/issue*:\n'
cat /etc/issue* 2>/dev/null </dev/null || printf '<none>\n'
printf -- '---/proc/version:\n'
cat /proc/version 2>/dev/null </dev/null || printf '<none>\n'

# Sanity check to verify special characters have not been lost on upload
printf '\nSpecial character check: \a\b\t\001\r\n'

test_tool() {
  printf '\nCommand: "%s"\n--------stdout--------\n' "$*"
  { stderr="$( { "$@"; } 2>&1 1>&3 3>&- )"; } 3>&1
  printf '\n--------stderr--------\n%s\n----------------------\nReturn code: "%s"\n' "$stderr" "$?"
}

test_tool bash --version
test_tool zsh --version

test_tool uname
test_tool uname -s
test_tool uname -s -m
test_tool uname -o

hostname_cksum() {
  hostname | cksum
}
test_tool hostname_cksum

test_tool tput sgr0
test_tool tput me
test_tool tput bold
test_tool tput md
test_tool tput smul
test_tool tput us
test_tool tput colors
test_tool tput bel
test_tool tput bl
test_tool tput setaf 0
test_tool tput AF 0
test_tool tput AF 0 0 0
test_tool tput setab 0
test_tool tput AB 0
test_tool tput AB 0 0 0

test_tool ps -o comm= -p "$PPID"
test_tool logname

test_tool screen -ls
test_tool shpool list
test_tool tmux list-sessions

for power_supply in "/sys/class/power_supply/"*; do
  for interface in "${power_supply}/"*; do
    test_tool cat "$interface"
  done
done
test_tool acpi --battery
test_tool pmset -g batt

test_tool nproc
# Not actually a command used, we read directly from the file
test_tool cat /proc/loadavg
test_tool cat /proc/meminfo
test_tool cat /proc/net/wireless
test_tool cat /var/run/dmesg.boot
test_tool /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport --getinfo
test_tool sysctl -n hw.ncpu
test_tool sysctl -n vm.loadavg
test_tool kstat -m cpu_info
test_tool uptime
test_tool df -k -P
test_tool vm_stat
test_tool cat /proc/meminfo
test_tool cat /var/run/dmesg.boot

test_tool sensors -u
test_tool acpi -t
for interface in \
    /sys/class/hwmon/hwmon*/temp*_input \
    /sys/class/hwmon/hwmon*/device/temp*_input \
    /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_input \
    /sys/class/thermal/thermal_zone*/temp \
  ; do
  test_tool cat "$interface"
done

test_tool date '+%I %M'
test_tool tty
test_tool basename -- /dev/pts/0

test_tool perbrew use
test_tool plenv version-name
test_tool rvm-prompt i v g s
test_tool rbenv version-name

# shellcheck disable=SC2016
printf 'Tests complete.\nMake sure to upload the file directly, do not `cat` and copy paste!\n' >&2

# vim: ft=sh et sts=2 sw=2 tw=120
