# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2005-2016 System76, Inc.

"""
Collect logs and other info for support.
"""

import os
from os import path
import shutil
import tempfile
import distro
import subprocess

from .model import determine_model_new

def dump_command(base, name, args):
    fp = open(path.join(base, name), 'xt')
    output = subprocess.run(" ".join(args), capture_output=True, shell=True, text=True)
    fp.write(output.stdout + "\n" + output.stderr)

def dump_path(base, name, src):
    if path.exists(src):
        dst = path.join(base, name)
        dst_dir = path.dirname(dst)
        if not path.isdir(dst_dir):
            os.makedirs(dst_dir)
        assert not path.exists(dst)
        if path.isdir(src):
            shutil.copytree(src, dst)
        else:
            shutil.copy(src, dst)

def dump_logs(base):
    fp = open(path.join(base, 'systeminfo.txt'), 'x')
    fp.write('System76 Model: {}\n'.format(determine_model_new()))
    fp.write('OS Version: {}\n'.format(distro.name(pretty=True)))
    fp.write('Kernel Version: {}\n'.format(os.uname().release))
    fp.write('Kernel Revision: {}\n'.format(os.uname().version))

    dump_command(base, "boot-process-times", ["systemd-analyze", "blame"])
    dump_command(base, "free-disk-space", ["df", "-h"])
    dump_command(base, "dmesg", ["dmesg"])
    dump_command(base, "dmidecode", ["dmidecode"])
    dump_command(base, "efibootmgr", ["efibootmgr", "-v"])
    dump_command(base, "journalctl", ["journalctl", "--since", "yesterday"])
    dump_command(base, "lsblk", ["lsblk", "-o", "NAME,MODEL,FSTYPE,FSVER,SIZE,FSUSE%,MOUNTPOINTS,LABEL,UUID"])
    dump_command(base, "lsmod", ["lsmod"])
    dump_command(base, "lspci", ["lspci", "-vv"])
    dump_command(base, "lsusb", ["lsusb", "-vv"])
    dump_command(base, "reboot-history", ["last"])
    dump_command(base, "sensors", ["sensors"])
    dump_command(base, "upower", ["upower"])
    dump_command(base, "uptime", ["uptime"])
    dump_command(base, "xinput", ["xinput"])
    dump_path(base, "crypttab", "/etc/crypttab")
    dump_path(base, "kernelstub", "/etc/kernelstub/configuration")
    dump_path(base, "fstab", "/etc/fstab")
    dump_path(base, "syslog", "/var/log/syslog")
    dump_path(base, "Xorg.log", "/var/log/Xorg.0.log")
    dump_path(base, "apt/sources.list", "/etc/apt/sources.list")
    dump_path(base, "apt/sources.list.d", "/etc/apt/sources.list.d")
    dump_path(base, "apt/history", "/var/log/apt/history.log")
    dump_path(base, "apt/history-rotated.gz", "/var/log/apt/history.log.1.gz")
    dump_path(base, "apt/term", "/var/log/apt/term.log")
    dump_path(base, "apt/term-rotated.gz", "/var/log/apt/term.log.1.gz")

def create_tmp_logs(func=dump_logs):
    tmp = tempfile.mkdtemp(prefix='logs.')
    base = path.join(tmp, 'system76-logs')
    os.mkdir(base)
    if func is not None:
        func(base)
    tgz = path.join(tmp, 'system76-logs.tgz')
    cmd = [
        'tar', '-czv',
        '-f', tgz,
        '-C', tmp,
        'system76-logs',
    ]
    subprocess.run(cmd)
    return (tmp, tgz)

def create_logs(homedir, func=dump_logs):
    (tmp, src) = create_tmp_logs(func)
    assert path.isdir(homedir)
    dst = path.join(homedir, path.basename(src))
    shutil.copy(src, dst)
    shutil.rmtree(tmp)
    return dst
