#!/bin/sh
#
# ufetch-openbsd - tiny system info for openbsd

## INFO

# user is already defined
host="$(hostname)"
os="$(uname -sr)"
kernel="$(uname -v)"
uptime="$(uptime | awk -F, '{sub(".*up ",x,$1);print $1}' | sed -e 's/^[ \t]*//')"
packages="$(ls -d /var/db/pkg/* | wc -l | sed -e 's/^[ \t]*//')"
shell="$(basename "${SHELL}")"

## UI DETECTION

parse_rcs() {
	for f in "${@}"; do
		wm="$(tail -n 1 "${f}" 2> /dev/null | cut -d ' ' -f 2)"
		[ -n "${wm}" ] && echo "${wm}" && return
	done
}

rcwm="$(parse_rcs "${HOME}/.xinitrc" "${HOME}/.xsession")"

ui='unknown'
uitype='UI'
if [ -n "${DE}" ]; then
	ui="${DE}"
	uitype='de'
elif [ -n "${WM}" ]; then
	ui="${WM}"
	uitype='wm'
elif [ -n "${XDG_CURRENT_DESKTOP}" ]; then
	ui="${XDG_CURRENT_DESKTOP}"
	uitype='de'
elif [ -n "${DESKTOP_SESSION}" ]; then
	ui="${DESKTOP_SESSION}"
	uitype='de'
elif [ -n "${rcwm}" ]; then
	ui="${rcwm}"
	uitype='wm'
elif [ -n "${XDG_SESSION_TYPE}" ]; then
	ui="${XDG_SESSION_TYPE}"
fi

ui="$(basename "${ui}")"

## DEFINE COLORS

# probably don't change these
if [ -x "$(command -v tput)" ]; then
	bold="$(tput bold)"
	black="$(tput setaf 0 0 0 2>/dev/null)"
	red="$(tput setaf 1 0 0 2>/dev/null)"
	green="$(tput setaf 2 0 0 2>/dev/null)"
	yellow="$(tput setaf 3 0 0 2>/dev/null)"
	blue="$(tput setaf 4 0 0 2>/dev/null)"
	magenta="$(tput setaf 5 0 0 2>/dev/null)"
	cyan="$(tput setaf 6 0 0 2>/dev/null)"
	white="$(tput setaf 7 0 0 2>/dev/null)"
	reset="$(tput sgr0)"
fi

# you can change these
c1="${reset}${bold}${white}"               # second color

## OUTPUT

cat <<EOF

${c1}       _____      ${USER}${c1}@${c1}${host}${reset}
${c1}     \-     -/    os  ${reset}${os}${reset}
${c1}  \_/         \   sys ${reset}${kernel}${reset}
${c1}  |        ${reset}O O${c1} |  up  ${reset}${uptime}${reset}
${c1}  |_  ${reset}<   )  3${c1} )  pkg ${reset}${packages}${reset}
${c1}  / \         /   sh  ${reset}${shell}${reset}
${c1}     /-_____-\    ${uitype}  ${reset}${ui}${reset}

EOF