#!/bin/bash
#
# This script is called manually by user
# who wish to provide Jetico support team
# with more technical details.

apt install -y curl || true

usage()
{
  cat <<EOF

 Usage:

  * make sure you have an internet connection on the machine (or just try to continue)

  * Enter command in the command prompt:

       send_logs_on_the_internet.sh

    (Hint: you may just type send_  and than press TAB- the rest will be autocompleted)

  * The script will concatenate several log files and will try to send them
    as single piece of text to https://clbin.com/ service

  If the try is successful you will be given by a link with uniq token for your information.

  Please provide us with that link otherwise we are unable to find it.

EOF
}


if ! command -v curl ; then
  echo "Error: Unable to operate on this system: curl is not installed." >&2
  exit 1
fi

echo "Collecting and sending data anonymously..."

append_file()
{
  local FILE="$1"

  if [ -e "$FILE" ] ; then
    echo
    echo "-XXX--- $FILE ------"
    cat "$FILE"
    echo "------ end of $FILE -XXX---"
    echo
  else
    echo
    echo "File $FILE is absent"
    echo
  fi
}

# start concatenation to stdout
(
  append_file /tmp/hwdrivers.failed

  echo "-XXX--- dmesg ------"
  dmesg
  echo "------ end of dmesg -XXX---"

  echo
  echo "-XXX--- Mounted filesystems ------"
  mount
  echo "------ end of mounted filesystems -XXX---"
  echo

  echo
  echo "-XXX--- list of modules ------"
  lsmod
  echo "------ end of list of modules -XXX---"
  echo


  echo
  echo "-XXX--- list of devices  ------"
  echo "lspci output:"
  lspci
  echo "lsusb output:"
  lsusb
  echo "------ end of list of devices -XXX---"
  echo

  if [ -x /usr/sbin/dmidecode ] ; then
    echo
    echo "-XXX--- DMI decode ------"
    dmidecode
    echo "------ end of DMI decode -XXX---"
    echo
  fi

) > /tmp/prepared_log_for_sending.txt

cat /tmp/prepared_log_for_sending.txt \
 | curl --insecure -F 'clbin=<-' https://clbin.com > /tmp/paste.link.url

if [ "$?" = "0" ] ; then
  cat <<EOF

  The information have been stored anonymously.

  Please provide us the following link
  otherwise we are unable to get this information:

    `cat /tmp/paste.link.url`

  Feel free to review the content before sending the link to us.

EOF

else
cat <<EOF

Error: unable to send data over the network.
Please check your internet connection and try again.

EOF
fi
