#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2023-present Team LibreELEC (https://libreelec.tv)

# create logfile

DATE=$(date -u +%Y-%m-%d-%H.%M.%S)
BASEDIR=$(mktemp -d)
LOGDIR="log-$DATE"
RELEASE=$(cat /etc/release)

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

get_governor_details() {
  (
    cat_all_files /sys/devices/system/cpu
    cat_all_files /sys/devices/system/cpu/cpufreq
    for cpun in /sys/devices/system/cpu/cpu[0-9]*; do
      cat_all_files ${cpun}/cpufreq
    done
  )
}

cat_all_files() {
  local adir=$1
  local afile var

  [ -d ${adir} ] || return 0

  echo "${adir}"

  cd ${adir}
  for afile in $(find . -maxdepth 1 -print | sort); do
    afile=${afile:2}
    if [ -n "${afile}" ]; then
      if [ -d ${afile} ]; then
        var="<dir>"
      else
        var=$(cat ${afile} 2>/dev/null)
      fi
      [ -n "${var}" ] && printf "    %-30s : %s\n" "${afile}" "${var}"
    fi
  done
}

rm -rf ${BASEDIR:?}/$LOGDIR
mkdir -p $BASEDIR/$LOGDIR

# kodi.log
  KODI_LOG_DIR=/storage/.kodi/temp

  LOGFILE="01_KODI.log"
  for i in kodi.log kodi.old.log; do
    [ -f ${KODI_LOG_DIR}/${i} ] && getlog_cmd cat ${KODI_LOG_DIR}/$i
  done

  LOGFILE="01_KODI_CRASH.log"
  for i in $(find ${KODI_LOG_DIR} -type f -name "kodi_crashlog_*.log" | sort -r); do
    getlog_cmd cat $i
  done

  LOGFILE="01_KODI_OTHER.log"
  for i in $(find ${KODI_LOG_DIR} -type f -name "*.log" | sort); do
    iname="${i#${KODI_LOG_DIR}/}"
    [ ${iname} = kodi.log ] && continue
    [ ${iname} = kodi.old.log ] && continue
    [ "${iname#kodi_crashlog_}" != "${iname}" ] && continue
    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 contain
  # login credentials
  for i in /storage/.config/system.d/*.service ; do
    if [ -f "$i" ] && [ ! -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 get_governor_details
  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 -name journal -prune -o -type f -print); 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)
# only exists if using persistent journal
  if [ -f "/storage/.cache/journald.conf.d/00_settings.conf" ]; then
    LOGFILE="10_Journal-prev.log"
    getlog_cmd journalctl --no-pager -b -1
  fi

# 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
