#!/bin/bash

##/local/bin/quick-system-info-mx
##wrapper script for inxi to do quick "inxi -F" and copy report to clipboard
##part of mx-goodies package
## changes to optimze inxi run

#requires xsel package

##locale stuff

TEXTDOMAINDIR=/usr/share/locale/ 
export TEXTDOMAIN="mx-goodies"

NOTFND=$(gettext 'Required inxi app not found')
COPIED=$(gettext 'Report copied to system clipboard')
ANYKEY=$(gettext 'Press any key to close')

#setup

main(){

    #check for existing of inxi

    command -v inxi  >/dev/null || quit_error "$NOTFND"

    run_report

    quit

}

run_report(){

    local snapshot_created=""
    if [ -e "/etc/snapshot_created" ]; then
        snapshot_created="Snaphot created on: "$(cat /etc/snapshot_created)
    fi
    local inxi_color="$(( echo VIRT_TERM_COLOR_SCHEME=10; 
                          grep -sh '^VIRT_TERM_COLOR_SCHEME=' /etc/inxi.conf ~/.config/inxi.conf) | \
                          sed 's/^VIRT_TERM_COLOR_SCHEME=//' | tail -1)"
    local ansi='\x1b\[[0-9;]+[mK]'
    local host_filter="^(.+Host:(${ansi}|[[:space:]])+)([[:alnum:]]+)"
    local video_tweaks=/live/config/video-tweaks 

    ( [ -n "$snapshot_created" ] && echo $snapshot_created
             inxi -Fxxxrza -c$inxi_color | \
             sed -r "/Host:/{s/${host_filter}(.*)/\1<filter>\4/}"
             [ -r $video_tweaks ] && echo 'Video Tweaks:' && cat $video_tweaks 2>/dev/null
             ) | \
             tee >( 
             ( echo "[code]"; 
             sed -r 's/\x1b\[([0-9]{1,2}(;[0-9]{1,2})?)?m//g'; 
             echo "[/code]" ) | \
             xclip -selection clipboard 2>/dev/null )

    xclip -o -selection clipboard | xclip -i -selection primary
    echo 
    echo "$COPIED"

}

quit(){
    echo
    read -n 1 -s -r -p "$ANYKEY"
    exit 0
}

quit_error(){
    local msg
    msg=$1
    echo "$msg"
    exit 1
}

main "$@"
