#!/bin/bash
# Author:      Louis Abel <label@rockylinux.org>
# Description: A CLI frontend for the rocky linux pastebin service
#              Based on fpaste from fedora, without the python.

# Compatible with paste bins with a similar API to pinnwand.

# Copyright 2021 Rocky Enterprise Software Foundation (https://resf.org)

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# ** Begin Script **

# Variables
export RPASTE_URL="https://rpa.st/curl"

# Defaults
export TYPE=text
export LIFE=1day
export SYSINFO=0

export ARG_LIST
ARG_LIST=(
  "life"
  "sysinfo"
)
export OPTS
OPTS=$(getopt -a -n rpaste -o x:t:s \
  -l life:,type:,sysinfo -- "$@"
)

export __usage
__usage="
Usage: $(basename $0) [OPTIONS] FILE

Options:
  -x, --life       # Time (1hour, 1day, 1week)
  -t, --type       # Type (bash, python, text, etc)
  -s, --sysinfo    # Spits out useful system information. File is ignored.
"

# Functions
function usage() {
  echo "$__usage"
}

verifyTextFile() {
  fileTYPE=$(file -iL "${FILE_TO_PASTE}" | cut -d' ' -f 2)
  if [[ ! $fileTYPE =~ "text".* ]]; then
    echo "NOT A TEXT FILE!"
    exit 33
  fi
}

function sysinfo() {
  FILENAME="/tmp/ss-`date +"%m%d%y%H%M%S"`"
  touch $FILENAME
  cat > $FILENAME <<EOF
# OS Release:
$(cat /etc/*-release | uniq)

# Desktop Environments
$(ps -eo comm= | grep -E '(gnome-session|startkde|startactive|xfce.?-session|fluxbox|blackbox|hackedbox|ratpoison|enlightenment|icewm-session|od-session|wmaker|wmx|openbox-lxde|openbox-gnome-session|openbox-kde-session|mwm|e16|fvwm|xmonad|sugar-session|mate-session|lxqt-session|cinnamon)')

# Desktop Installed
$(ls -m /usr/share/xsessions/ | sed 's/\.desktop//g')

# SELinux Status
$(getenforce)
$(grep -v '^#' /etc/sysconfig/selinux)

# SELinux Errors
$(journalctl --since yesterday |grep avc: | grep -Eo comm="[^ ]+" | sort |uniq -c |sort -rn)

# CPU Info
$(grep 'model name' /proc/cpuinfo | awk -F: '{print $2}' | uniq -c | sed -re 's/^ +//')

# 64-bit Support
$(grep -q ' lm ' /proc/cpuinfo && echo Yes || echo No)

# Virtualization Support
$(grep -Eq '(vmx|svm)' /proc/cpuinfo && echo Yes || echo No)

# Load Average
$(uptime)

# Memory usage
$(free -m)

# Top 5 CPU hogs
$(ps axuScnh | sort -rnk3 | head -5)

# Top 5 memory hogs
$(ps axuScnh | sort -rnk4 | head -5)

# Disk space usage
$(df -hT)

# Block Devices
$(/sbin/blkid)

# PCI devices
$(/sbin/lspci -nn)

# USB devices
$(/sbin/lsusb)

# DRM Information
$(journalctl -k -b | grep -o 'kernel:.*drm.*$' | cut -d ' ' -f 2- )

# Xorg modules
$(grep LoadModule /var/log/Xorg.0.log ~/.local/share/xorg/Xorg.0.log | cut -d \" -f 2 | xargs)

# GL Support
$(glxinfo | grep -E "OpenGL version|OpenGL renderer")

# Xorg errors
$(grep '^\[.*(EE)' /var/log/Xorg.0.log ~/.local/share/xorg/Xorg.0.log | cut -d ':' -f 2- )

# Kernel Buffer Tail
$(dmesg | tail)

# Last few reboots
$(last -x -n10 reboot runlevel)

# DNF Repos
$(dnf repolist)
$(ls -l /etc/yum.repos.d/)
$(grep -v '^#' /etc/yum.conf)

# Last 20 packages
$(rpm -qa --nodigest --nosignature --last | head -20)

# EFI Support
$(efibootmgr -v)
EOF
  echo "${FILENAME}"
}

function paste() {
  echo "Uploading..."
  curl -X POST \
    -F "expiry=${LIFE}" \
    -F "lexer=${TYPE}" \
    -F 'raw=<-' \
    "${RPASTE_URL}" < "${FILE_TO_PASTE}"
}

eval set -- "$OPTS"
while :; do
  case "$1" in
    -x | --life)     LIFE="$2"     ; shift 2 ;;
    -t | --type)     TYPE="$2"     ; shift 2 ;;
    -s | --sysinfo)  SYSINFO=1     ; shift   ;;
    --) shift ;;
    *) break ;;
  esac
done

if [ -z "$1" ] && [[ $SYSINFO == "0" ]]; then
  usage
elif [ -n "$1" ] && [ -f "$1" ]; then
  FILE_TO_PASTE="${1}"
  verifyTextFile
  paste
elif [[ $SYSINFO == "1" ]]; then
  FILE_TO_PASTE=$(sysinfo 2> /dev/null)
  paste
  /bin/rm "${FILE_TO_PASTE}"
fi
