#!/usr/bin/env bash


# Copyright (C) 2021 Blau Araujo <blau@debxp.org>.
# License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.

version='1.0'

help="
${0##*/} ($version) - Prints short info about *BSD.

Usage: ${0##*/} [OPTIONS]

OPTIONS:

    -d   No *BSD logo
    -l   No line
    -p   No package info
    -h   This help

Copyright (C) 2021 Blau Araujo <blau@debxp.org>,
Filipe da Silva Santos <ports@shiori.com.br>.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
"

usage() {
    echo "$help"
    exit 0
}

while getopts ":hdlp" opt; do
    case $opt in
        h) usage     ;;
        d) nologo=' ';;
        l) noline=' ';;
	p) nopkg=' ' ;;
        *) usage ;;
    esac
done

# Get terminal col width...
cols=$(tput cols)
printf -v line "%${cols}s" " "
line=${line// /-}$'\n'

# Styles...
red="$(tput setaf 1)"
bold="$(tput bold)"
reset="$(tput sgr0)"

# Packages...

pkg_total=$(pkg inf -q | wc -l | tr -d ' \t')
last_update=$(date -r /var/db/pkg/local.sqlite +'%b %d, %Y %R')

packages="
${nologo:-           } ${bold}Packages:${reset} $pkg_total
${nologo:-           } ${bold}Last update:${reset} $last_update
"

# Uptime...
time=$(sysctl -n kern.boottime)
time=${time#*=}
time=${time%,*}
time=$(($(date +%s) - time))
d=$((time / 60 / 60 / 24))
h=$((time / 60 / 60 % 24))
m=$((time / 60 % 60))
case "$d" in ([!0]*) uptime="${uptime}${d}d "; esac
case "$h" in ([!0]*) uptime="${uptime}${h}h "; esac
case "$m" in ([!0]*) uptime="${uptime}${m}m "; esac

# Main...

clear

. /etc/os-release

echo "
${nologo:-"$red ⢻⢗⣴⣶⣾⣿⡶⣴⣾⡟"}$reset ${bold}$PRETTY_NAME${reset}
${nologo:-"$red ⢰⣿⣿⣿⣿⣿⣿⣮⣭⣦"}$reset ${bold}Kernel:${reset} $(uname -rp)
${nologo:-"$red ⠸⣿⣿⣿⣿⣿⣿⣿⣿⡟"}$reset ${bold}Uptime:${reset} $(echo $uptime)
${nologo:-"$red  ⠙⠻⢿⣿⣿⣿⠿⠋ "}$reset ${bold}$USER@$HOSTNAME${reset}"

echo ${nopkg:-"$packages"}
echo ${noline:-"$line"}

exit 0
