#!/usr/bin/env bash
date | tee -a /tmp/network.txt
> /tmp/tail_network.txt
# get the interface
INTERFACE=$(route | awk '/^default/ {print $NF}')
if [ -z "$INTERFACE" ]; then
    echo No Interface Found | tee -a /tmp/network.txt
    echo No IP Found | tee -a /tmp/network.txt
    echo NONE >> /tmp/tail_network.txt
    echo NONE >> /tmp/tail_network.txt
else
    ifconfig $INTERFACE | tee -a /tmp/network.txt
    IP=$(ifconfig $INTERFACE | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
    echo $INTERFACE >> /tmp/tail_network.txt
    echo $IP >> /tmp/tail_network.txt
fi
# see if we can connect to HWSD
traceroute hwsd.zooxlabs.com 2>/dev/null | tee -a /tmp/network.txt
if [ ${PIPESTATUS[0]} -ne 0 ]; then
    echo No connection to the Zoox internal network. | tee -a /tmp/network.txt
    echo This is probably caused by a bad ethernet cable or disconnected cables.
    echo Is eth0 \(Zoox Internal Network\) configured?
fi
# run a speed test
speedtest-cli 2>/dev/null | grep -E 'Download|Upload' | tee -a /tmp/network.txt /tmp/tail_network.txt
if [ ${PIPESTATUS[0]} -ne 0 ]; then
    echo No Connection
    echo Download: 0.0 Mbit/s | tee -a /tmp/tail_network.txt /tmp/network.txt
    echo Upload: 0.0 Mbit/s | tee -a /tmp/tail_network.txt /tmp/network.txt
fi
echo "" >> /tmp/network.txt
