#!/bin/bash
#########################################################
#                                                       #
# This is BashStyle-NG                                  #
#                                                       #
# Licensed under GNU GENERAL PUBLIC LICENSE v3          #
#                                                       #
# Copyright Christopher Roy Bratušek                    #
#                                                       #
#########################################################

_show_editmode=$(ini_get show_editmode)
check bt ${_show_editmode} && _prompt_prefix=' '

prompt_command() {
    local one=$(uptime | awk -F'load average:' '{print $2}' | cut -d, -f1 | tr -d ' ')
    local five=$(uptime | awk -F'load average:' '{print $2}' | cut -d, -f2 | tr -d ' ')

    [[ -z "$one" ]] && one=0.00
    [[ -z "$five" ]] && five=0.00

    local diff1_5
    diff1_5=$(echo "scale=2; x=$one - $five; if (x>0) print \"up\" else print \"down\"; print x" | bc 2>/dev/null)
    diff1_5=$(echo "$diff1_5" | tr -d '\n')
    loaddiff="$(echo -n "${one}${diff1_5}" | sed -e 's/down\-/down/g')"

    files=$(LANG=C ls -l | \grep -v "^total" | \grep -c "^-")
    hiddenfiles=$(LANG=C ls -a -l | \grep -v "^total" | \grep " \..*" | \grep -c "^-")
    executables=$(LANG=C ls -l | \grep -v "^total" | \grep -c "^-..x")
    directories=$(LANG=C ls -l | \grep -v "^total" | \grep -c "^d")

    local raw_hiddendirs=$(LANG=C ls -a -l | \grep -v "^total" | \grep " \..*" | \grep -c "^d")
    hiddendirectories=$(( raw_hiddendirs >= 2 ? raw_hiddendirs - 2 : 0 ))

    linktemp=$(LANG=C ls -l | \grep -v "^total" | \grep -c "^l")
    if [ "$linktemp" -eq 0 ]; then
        links=""
    else
        links=" ${linktemp}l"
    fi

    local devicetemp=$(LANG=C ls -l | \grep -v "^total" | \grep -c "^[bc]")
    if [ "$devicetemp" -eq 0 ]; then
        devices=""
    else
        devices=" ${devicetemp}bc"
    fi
}

PROMPT_COMMAND=prompt_command

PS1="\n\
${color_separator}${_prompt_prefix}[ ${color_time}\$(date +%T) ${color_font}-${color_date} \$(date +%D) ${color_separator}]\
${color_separator}[ ${color_user}\u ${color_font}@ ${color_host}\h ${color_separator}]\
${color_separator}[ ${color_wdir}\${files}.\${hiddenfiles}f-\${executables}x \$(systemkit dirsize) \
\${directories}.\${hiddendirectories}d\${links}\${devices}\
${color_separator} ][ ${color_uptime}\${loaddiff}${color_separator} ][ \
${color_ps}\$(ps ax | wc -l | sed -e \"s: ::g\")proc ${color_separator}]\
\n${color_separator}[ ${color_wdir}\$(truncpwd)${POW_1_COLOR}\$(POW_1) ${color_separator}]${color_font} \$(showuser) "

unset _show_editmode
