#! /bin/sh

# Keep this POSIX, no bash-isms

# make it easy to pipe/save stderr
exec 2>&1

if [ 0 != "$(id -u)" ]; then
    echo "ERROR: Please run as root"
    exit 1
fi
if [ -n "${SUDO_COMMAND+1}" ]; then
    echo "WARNING: Please run as root"
    echo "WARNING: Running under sudo confuses the results."
fi

# print what we do
set -x

id
uname -a
# get sh version
sh --version
cat /etc/*release
if command -v gpsd; then
    # get version
    gpsd -V
    # get as-built options
    gpsd -h | grep '^#.*enabled'
fi
if command -v python; then
    # get gps module version
    python -c "import gps; print(gps.__version__)"
elif command -v python3; then
    # get gps module version
    python3 -c "import gps; print(gps.__version__)"
fi

# check the netwaork status of the gpsd TCP port.
# use sockstat, lsof, netstat or ss, depending, for owner of port 2947
if command -v sockstat; then
    # sockstat for FreeBSD
    sockstat -l | sed -nE '1p;/:2947 |gpsd/p'
elif command -v lsof; then
    lsof -iTCP:2947 -s TCP:LISTEN
    # gpsd open files
    lsof -c gpsd | sed -nE '1p;/CHR|LISTEN/p'
elif command -v netstat; then
    # FreeBSD does not support "-n"
    netstat -lp | sed -nE '2p;/:2947 |gpsd/p'
elif command -v ss; then
    ss -ltpn | sed -nE '1p;/:2947 |gpsd/p'
fi

# check how gpsd is running by looking at running processes
# busybox does not support ps a or x options, so gall back to 'l'
ps ax || ps l |  sed -nE '1p;/gpsd/p'

# check that host is not swapping, happens on Rasperry Pis, etc.
if command -v free; then
    free
fi

# check that host load factor is not too high
if command -v uptime; then
    uptime | grep -o   "load .*$"
fi

if command -v gpspipe; then
    gpspipe -V
    # try to get JSON header
    gpspipe -w -n 2 -x 20
fi
if command -v ipcs; then
    ipcs -m | sed -nE '/key/p;/KEY/p;/0x4e5450/p'
fi
if command -v ntpshmmon; then
    ntpshmmon -V
    # 6 lines, or 10 seconds, of ntpshmmon
    ntpshmmon -n 6 -t 10
fi
# check for /dev/pps*, /dev/ttyA*, ttyP* ttyS*, ttyT*, ttyU*, gps*
ls -l /dev/pps* /dev/tty[APSTU]* /dev/gps*
# check for USB receivers
if command -v lsusb; then
    lsusb
fi
echo PYTHONPATH "$PYTHONPATH"
if command -v gpscat; then
    head -n 1 "$(command -v gpscat)"
fi

# Check what gpsd module version that Python sees, and the paths
if command -v python; then
    python -V
    python -c "import gps;print(gps.__version__)"
    python -m site
fi
if command -v python3; then
    python3 -V
    python3 -c "import gps;print(gps.__version__)"
    python3 -m site
fi

# check systemd.  Almost never properly configured for gpsd.
if command -v systemctl; then
    cat /etc/*/gpsd
    systemctl cat gpsd.service
    systemctl cat gpsd.socket
    systemctl status gpsd.service
    systemctl status gpsd.socket
    journalctl -u gpsd.service --since today
fi

# check apparmor.  Often configured to break gpsd
if command -v aa-enabled; then
    aa-enabled
elif command -v aa-status; then
    aa-status
elif command -v apparmor_status; then
    apparmor_status
fi
# do not print the rest
set +x
echo "Please send the entire, untouched output."
exit 0
