#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
#
#  OpenELEC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  OpenELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

# create logfile

DATE=`date -u +%Y-%m-%d-%H.%M.%S`
BASEDIR="/tmp"
LOGDIR="log-$DATE"
RELEASE="`cat /etc/release`"
GIT="`cat /etc/issue |grep git`"

getlog_cmd() {
  if which $1 >/dev/null; then
    echo "################################################################################" >> $BASEDIR/$LOGDIR/$LOGFILE
    echo "# ... output of $@" >> $BASEDIR/$LOGDIR/$LOGFILE
    echo "# OpenELEC release: $RELEASE" >> $BASEDIR/$LOGDIR/$LOGFILE
    echo "# $GIT" >> $BASEDIR/$LOGDIR/$LOGFILE
    echo "################################################################################" >> $BASEDIR/$LOGDIR/$LOGFILE
    $@ >> $BASEDIR/$LOGDIR/$LOGFILE 2>/dev/null
    echo "" >> $BASEDIR/$LOGDIR/$LOGFILE
  fi
}

rm -rf $BASEDIR/$LOGDIR
mkdir -p $BASEDIR/$LOGDIR

# kodi.log
  LOGFILE="01_KODI.log"
  for i in `find /storage/.kodi/temp/ -type f -name "*.log"`; do
    getlog_cmd cat $i
  done

# System.log
  LOGFILE="02_System.log"
  getlog_cmd dmesg
  getlog_cmd lsmod
  getlog_cmd ps xa
  for i in /storage/.config/hwdb.d/*.hwdb \
      /storage/.config/modprobe.d/*.conf \
      /storage/.config/modules-load.d/*.conf \
      /storage/.config/sleep.d/*.power \
      /storage/.config/sysctl.d/*.conf \
      /storage/.config/udev.rules.d/.rules \
  ; do
    if [ -f "$i" ] ; then
      getlog_cmd cat $i
    fi
  done
  if [ -f "/storage/.config/autostart.sh" ] ; then
    getlog_cmd cat /storage/.config/autostart.sh
  fi
  if [ -f "/storage/.config/shutdown.sh" ] ; then
    getlog_cmd cat /storage/.config/shutdown.sh
  fi
  getlog_cmd ls -laR /storage/.config/system.d
  # note: we dont add .mount units here as they may contan
  # login credentials
  for i in /storage/.config/system.d/*.service ; do
    if [ -f "$i" -a ! -L "$i" ] ; then
      getlog_cmd cat $i
    fi
  done

# Hardware.log
  LOGFILE="03_Hardware.log"
  getlog_cmd lspci -vvvvnn
  getlog_cmd lsusb -vvv
  getlog_cmd lsusb -t
  getlog_cmd cat /proc/cpuinfo
  getlog_cmd cat /proc/meminfo

# Audio.log
  LOGFILE="04_Audio.log"
  getlog_cmd aplay -l
  getlog_cmd aplay -L
  getlog_cmd amixer

# Network.log
  LOGFILE="05_Network.log"
  getlog_cmd ifconfig -a
  getlog_cmd netstat -rn
  getlog_cmd netstat -nalp
  getlog_cmd connmanctl services
  getlog_cmd cat /etc/resolv.conf

# varlog.log
  LOGFILE="06_varlog.log"
  for i in `find /var/log -type f`; do
    getlog_cmd cat $i
  done

# Input.log
  LOGFILE="07_input.log"
  getlog_cmd cat /proc/bus/input/devices
  # make RPi users happy
  if [ -e /proc/acpi/wakeup ] ; then
    getlog_cmd cat /proc/acpi/wakeup
  fi

# Filesystem.log
  LOGFILE="08_Filesystem.log"
  getlog_cmd cat /proc/mounts
  getlog_cmd df -h
  getlog_cmd blkid

# Journal (current)
  LOGFILE="09_Journal-cur.log"
  getlog_cmd journalctl --no-pager -b -0

# Journal (prev)
  LOGFILE="10_Journal-prev.log"
  getlog_cmd journalctl --no-pager -b -1

# addons
  LOGFILE="11_Addons.log"
  for i in /storage/.kodi/userdata/addon_data/*/*.log \
      /storage/.kodi/userdata/addon_data/*/log/* \
  ; do
    if [ -f "$i" ] ; then
      getlog_cmd cat $i
    fi
  done

# pack logfiles
  mkdir -p /storage/logfiles
  zip -jq /storage/logfiles/log-$DATE.zip $BASEDIR/$LOGDIR/*

# remove logdir
  rm -rf $BASEDIR/$LOGDIR
