#!/bin/bash
GF_ROOT="/opt/grassfish"
DATA="$GF_ROOT/data"
WD_ROOT="$GF_ROOT/cache"
WD="$WD_ROOT/troubleshoot"
CONTENTSFILE="contents.txt"
ARCHIVE="`hostname`-`date "+%Y-%m-%d"`.tar"
ARCHIVE_ZIPPED="$ARCHIVE.gz"
export DISPLAY=:0

set -e

rm -rf $WD
mkdir -p "$WD" || ( sudo mkdir -p "$WD" && sudo chown -R 1000:1000 $WD_ROOT ) || true
mkdir -p $DATA || ( sudo mkdir -p $DATA && sudo chown -R 1000:1000 $DATA ) || true


echo "--------------------------------------------------------------------------------";
echo "-- Checking if filesystem if writable";
touch $WD_ROOT/rw && { rm $WD_ROOT/rw; } || { 
TMPDIR=$XDG_RUNTIME_DIR
#TODO mount tmp fs if TMPDIR is empty
if [ -z "$TMPDIR" ]; then
    echo "No writable folder found. Exiting."
    exit 1
fi
WD_ROOT="$TMPDIR/gf"
WD="$WD_ROOT/troubleshoot"
mkdir -p "$WD"
echo "Detected read-only filesystem. Setting working directory to $WD";
}

cd $GF_ROOT


function printFile {
    FILE=$1
    md5sum "$FILE"  | awk '{printf "%s ", $1}' >> $WD/$CONTENTSFILE;
    ls -1al "$FILE" | awk '{printf "%s %s %-10s %-10s %12s %s %2s %s  ", $1, $2, $3, $4, $5, $6, $7, $8}' >> $WD/$CONTENTSFILE;
    echo "$FILE" >> $WD/$CONTENTSFILE;
}

echo "--------------------------------------------------------------------------------";
echo "-- Collecting permissions and checksums of all files in /opt/grassfish";
touch $WD/$CONTENTSFILE
find -type f | while read file; do printFile "$file"; done


echo "--------------------------------------------------------------------------------";
echo "-- Collecting contents of data folder";


cd $GF_ROOT
tar --exclude="data/media" --exclude="data/customcontent" --exclude="data/updates" --exclude="data/tmp" -cvf $WD/$ARCHIVE data
tar -uvf $WD/$ARCHIVE data/media/*.qml 2>/dev/null || true
tar -uvf $WD/$ARCHIVE data/media/*.json 2>/dev/null || true
tar -uvf $WD/$ARCHIVE data/media/*.xml 2>/dev/null || true
cd $WD
tar -uf $WD/$ARCHIVE $CONTENTSFILE


echo "--------------------------------------------------------------------------------";
echo "-- Collecting system logs";
cd $WD
mkdir syslogs
cp /var/log/kern.log syslogs/ 2>/dev/null || true
cp /var/log/gf*.log syslogs/ 2>/dev/null || true
cp /var/log/Xorg.0.log syslogs/ 2>/dev/null || true
cp /var/log/syslog syslogs/ 2>/dev/null || true
dmesg > syslogs/dmesg 2>&1 || true
journalctl -b > syslogs/journal.log 2>&1 || true
tar -uf $WD/$ARCHIVE syslogs

echo "--------------------------------------------------------------------------------";
echo "-- Collecting information about current system state";
mkdir status
ps -aux > status/ps.txt 2>&1 || true
env > status/env.txt 2>&1 || true
glxinfo > status/glxinfo.txt 2>&1 || true
top -n 1 -b > status/top.txt 2>&1 || true
uptime > status/uptime.txt 2>&1 || true
df -h > status/df.txt 2>&1 || true
sensors > status/sensors.txt 2>&1 || true
lsusb > status/lsusb.txt 2>&1 || true
lspci > status/lspci.txt 2>&1 || true
lscpu > status/lscpu.txt 2>&1 || true
lsblk > status/lsblk.txt 2>&1 || true
lsmod > status/lsmod.txt 2>&1 || true
lsb_release -a > status/lsb-release.txt 2>&1 || true
lshw -html > status/lshw.html 2>&1 || true
uname -a > status/uname.txt 2>&1 || true
pstree > status/pstree.txt 2>&1 || true
mount > status/mount.txt 2>&1 || true
xset -q > status/xset.txt 2>&1 || true
xrandr -q > status/xrandr.txt 2>&1 || true
sysctl -a > status/sysctl.txt 2>&1 || true

echo  > status/modparam.txt || true
cat /proc/modules | cut -f 1 -d " " | while read module; do
 echo "Module: $module" >> status/modparam.txt || true;
 if [ -d "/sys/module/$module/parameters" ]; then
  ls /sys/module/$module/parameters/ | while read parameter; do
   echo -n "Parameter: $parameter --> " >> status/modparam.txt || true;
   cat /sys/module/$module/parameters/$parameter >> status/modparam.txt 2>&1 || true;
  done;
 fi;
 echo >> status/modparam.txt || true;
done

ip address > status/ipaddress.txt 2>&1 || true
route > status/route.txt 2>&1 || true
scrot status/screenshot.png 2>/dev/null || true
tar -uf $WD/$ARCHIVE status


echo "--------------------------------------------------------------------------------";
echo "-- Collecting information via Grassfish Client Webservices";
mkdir playerDetails
wget -qO- http://127.0.0.1:8080/REST/PlayerDetails/PlayerInfo > playerDetails/playerInfo.json 2>&1 || true
wget -qO- http://127.0.0.1:8080/REST/PlayerDetails/MasterDetails > playerDetails/masterDetails.json 2>&1 || true
wget -qO- http://localhost:8080/REST/PlayerDetails/MasterDetails > playerDetails/masterDetailsLocalhost.json 2>&1 || true
wget -qO- http://127.0.0.1:8080/REST/PlayerDetails/GetLiveTags > playerDetails/liveTags.json 2>&1 || true
wget -qO- http://127.0.0.1:8080/REST/PlayerDetails/GetProgramSchema > playerDetails/programSchema.json 2>&1 || true
wget -qO- http://127.0.0.1:8080/REST/PlayerDetails/GetCurrentPlaylistGet/?screenNr=1\&splitNr=0\&spotList=true > playerDetails/currentPlaylist.json 2>&1 || true
tar -uf $WD/$ARCHIVE playerDetails

mkdir hubinfo
wget -qO- http://127.0.0.1:8080/REST/hub/info/deviceList > hubinfo/deviceList.json 2>&1 || true
tar -uf $WD/$ARCHIVE hubinfo


echo "--------------------------------------------------------------------------------";
echo "-- Collecting information via dbus Interfaces";
mkdir dbus
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfInternetReceiver / com.grassfish.tools.gfInternetReceiver.DownloadInfo.getDownloadsJSON > dbus/getDownloadsJSON.txt 2>&1 || true
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfInternetReceiver /CommonDetails org.freedesktop.DBus.Properties.GetAll string:"" > dbus/commonDetails_InternetReceiver.txt 2>&1 || true
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfPlayer /CommonDetails org.freedesktop.DBus.Properties.GetAll string:"" > dbus/commonDetails_Player.txt 2>&1 || true
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfOverseer /CommonDetails org.freedesktop.DBus.Properties.GetAll string:"" > dbus/commonDetails_Overseer.txt 2>&1 || true
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfUpdater /CommonDetails org.freedesktop.DBus.Properties.GetAll string:"" > dbus/commonDetails_Updater.txt 2>&1 || true
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfServer /CommonDetails org.freedesktop.DBus.Properties.GetAll string:"" > dbus/commonDetails_Server.txt 2>&1 || true
dbus-send --system --type=method_call --print-reply --dest=com.grassfish.tools.gfScreenController /CommonDetails org.freedesktop.DBus.Properties.GetAll string:"" > dbus/commonDetails_ScreenController.txt 2>&1 || true
tar -uf $WD/$ARCHIVE dbus

echo "--------------------------------------------------------------------------------";
echo "-- Collecting information about current networking state";
mkdir networking
ifconfig > networking/ifconfig.txt 2>&1 || true
iwconfig > networking/iwconfig.txt 2>&1 || true
nmcli general status  > networking/nmcli_general.txt 2>&1 || true
nmcli connection show > networking/nmcli_connection.txt 2>&1 || true
nmcli -f all device wifi list > networking/nmcli_wifi_list.txt 2>&1 || true
nmcli device status > networking/nmcli_device_status.txt 2>&1 || true
tar -uf $WD/$ARCHIVE networking



gzip $ARCHIVE
mv $ARCHIVE_ZIPPED $WD_ROOT
rm -rf $WD


echo "--------------------------------------------------------------------------------";
echo "-- Done. Please send $WD_ROOT/$ARCHIVE_ZIPPED to support.";
echo "-- You can delete the file afterwards. It won't be deleted automatically.";
echo "--------------------------------------------------------------------------------";
