#!/bin/sh


# Zerocat Coreboot Machines --- Create very satisfying free software devices.
#
# Copyright (C) 2019, 2020, 2022  Kai Mertens <kmx@posteo.net>
#
# This file is part of Zerocat Coreboot Machines.
#
# Zerocat Coreboot Machines 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 3 of the
# License, or (at your option) any later version.
#
# Zerocat Coreboot Machines 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 Zerocat Coreboot Machines.  If not, see <http://www.gnu.org/licenses/>.


# Purpose
# =======
#
# Retrieve system info.


# Usage
# =====
#
#     ./get-sysinfo.sh --usage


# check interpreter
if [ -z $BASH_VERSION ]; then
  echo "Wrong interpreter, please use bash (https://www.gnu.org/software/bash)."
  exit 1
fi

# functions
get_dmesg() {
  $BIN_DMESG > dmesg.txt
}

get_lshw() {
  $BIN_LSHW -numeric > lshw.txt
}

get_lshw_network() {
  $BIN_LSHW -numeric -class network > lshw-network.txt
}

get_lscpu() {
  $BIN_LSCPU > lscpu.txt
}

get_lspci() {
  $BIN_LSPCI > lspci.txt
}

get_lspci_nnvv() {
  $BIN_LSPCI -nnvv > lspci_nnvv.txt
}

get_lspci_tv() {
  $BIN_LSPCI -tv > lspci_tv.txt
}

get_dmidecode() {
  $BIN_DMIDECODE > dmidecode.txt
}

get_dmidecode_u() {
  $BIN_DMIDECODE -u > dmidecode_u.txt
}

get_edid() {
  # Bug: works with Trisquel7 only
  $BIN_CAT /sys/class/drm/card0/card0-LVDS-1/edid | strings > edid.txt
}

usage() {
  echo "$BASH_SOURCE: $FUNCNAME(): display usage information ..."
  $BIN_CAT<<EOF

Name:
  $BASH_SOURCE – retrieve system information

Usage:
  $BASH_SOURCE [-h|--help|-u|--usage]
  sudo $BASH_SOURCE [-s|--scan]

Options:
  -h    | --help
  -u    | --usage
    Display usage information.

  -s    | --scan
    Scan system, retrieve system information.
    This option requires root priviledges.
    A temporary folder called “system-info.XXXXXXXXXX” will be created.
    Please run this script twice on your target machine,
    once _before_ and once _after_ the firmware replacement procedure.
    Then compare the outputs and look for differences.

License:
  For Copyright and License Note, see header within source file.

EOF

  echo "$BASH_SOURCE: $FUNCNAME(): ... usage information displayed."
}

parse_cmdline() {
  echo "$BASH_SOURCE: $FUNCNAME(): parsing command line ..."

  declare -i n=
  declare opt=

  while [ $# -gt 0 ]; do
    opt=${1:?${FG_RED}missing parameter${FG_DEFAULT}}
    n=1
    opt="${opt,,}"
    case "$opt" in
      ('-h'|'--help')
        ;&
      ('-u'|'--usage')
        usage
        ;;
      ('-s'|'--scan')
        if [ $(id -u) -eq 0 ]; then
          scan
        else
          echo "  Option $opt must be used with root priviledges."
        fi
        ;;
      (*)
        echo -e "  $opt:\tunknown option"
        ;;
    esac
    shift $n
  done
  if [ $# -ne 0 ]; then
    echo "$BASH_SOURCE: $FUNCNAME(): ... $# parameters were skipped."
  else
    echo "$BASH_SOURCE: $FUNCNAME(): ... command line parsed."
  fi
}

scan() {
  echo "$BASH_SOURCE: $FUNCNAME(): scan system ..."

  declare cwd=$(pwd)
  declare tmp_dir=$(mktemp -d system-info.XXXXXXXXXX)

  chmod go=+rx $tmp_dir
  cd $tmp_dir
  get_dmesg
  get_dmidecode
  get_dmidecode_u
  get_lshw
  get_lshw_network
  get_lscpu
  get_lspci
  get_lspci_nnvv
  get_lspci_tv
  get_edid
  cd $cwd
  echo -e "  Check folder ${SET_BOLD}$tmp_dir${SET_NORMAL} to see retrieved infos."

  echo "$BASH_SOURCE: $FUNCNAME(): ... system scanned."
}

main()
{
  echo "$BASH_SOURCE: $FUNCNAME(): starting ..."

  # tools
  # i.e. lspci lshw dmidecode lscpu
  declare -r BIN_CAT="$(command -v cat)"
  declare -r BIN_SED="$(command -v sed)"
  declare -r BIN_STRINGS="$(command -v strings)"
  declare -r BIN_LSHW="$(command -v lshw)"
  declare -r BIN_LSCPU="$(command -v lscpu)"
  declare -r BIN_LSPCI="$(command -v lspci)"
  declare -r BIN_DMESG="$(command -v dmesg)"
  declare -r BIN_DMIDECODE="$(command -v dmidecode)"
  [ -x $BIN_CAT ] || go_exit 'not available: $BIN_CAT'
  [ -x $BIN_SED ] || go_exit 'not available: $BIN_SED'
  [ -x $BIN_STRINGS ] || go_exit 'not available: $BIN_STRINGS'
  [ -x $BIN_LSHW ] || go_exit 'not available: $BIN_LSHW'
  [ -x $BIN_LSCPU ] || go_exit 'not available: $BIN_LSCPU'
  [ -x $BIN_LSPCI ] || go_exit 'not available: $BIN_LSPCI'
  [ -x $BIN_DMESG ] || go_exit 'not available: $BIN_DMESG'
  [ -x $BIN_DMIDECODE ] || go_exit 'not available: $BIN_DMIDECODE'

  declare -r SET_BOLD="\e[1m"
  declare -r CLR_INTENSITY="\e[21m"
  declare -r SET_NORMAL="\e[0m"

  parse_cmdline "$@"

  echo "$BASH_SOURCE: $FUNCNAME(): ... done."
}

# $1: optional message
go_exit() {
  echo "$BASH_SOURCE: $FUNCNAME(): error${1:+:} $1"
  exit 1
}

# script
main "$@"
